Cod sursa(job #2015481)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 26 august 2017 13:31:41
Problema Divk Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
ifstream in ("divk.in");
ofstream out ("divk.out");
int n ,k ,a ,b;
int const nmax = 500000;
int sum[1 + nmax];
vector <int> v[1 + nmax];

///binary search
///gaseste in v[r] ,pos < v[r][i] && !(pos < v[r][i - 1]);
int binarysearch(int from, int to ,int pos ,int r){
  if(from < to){
    int mid = ((from + to) >> 1);
    if(pos < v[r][mid]){
      return binarysearch(from , mid , pos , r);
    } else{
      return binarysearch(mid + 1, to, pos ,r);
    }
  }
}
int main()
{
  in>>n>>k>>a>>b;
  v[0].push_back(0);
  for(int i = 1 ; i <= n ;i++){
    in>>sum[i];
    sum[i] = (sum[i - 1] + sum[i]) % k;
    v[sum[i] ].push_back(i);
  }

  int sum = 0;
  for(int i = 0 ; i < k ;i++){
    v[i].push_back( (n << 1) );
    for(int j = 0 ;j < v[i].size() - 1 ;j++){
      int x = binarysearch(0 , v[i].size() ,v[i][j] + a - 1, i);
      int y = binarysearch(0 , v[i].size() ,v[i][j] + b , i);
      sum += y - x;
    }
  }
  out<<sum;

  return 0;
}