Cod sursa(job #1979543)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 10 mai 2017 20:09:49
Problema Divk Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;

const int nMax = 500005;
const int kMax = 100005;

int n, k, a, b;
int v[nMax];
int rest[nMax];
vector<int> pos[kMax]; //pozitiile elementului i in sum

void citire()
{
    ifstream in("divk.in");
    in >> n >> k >> a >> b;
    for(int i = 1; i <= n; ++i)
        in >> v[i];
    in.close();
}

void rezolvare()
{
    int st, dr, x;
    int rasp = 0;
    pos[0].push_back(0);
    for(int i = 1; i <= n; ++i)
    {
        rest[i] = (rest[i-1] + v[i]) % k;
        st = i - b;
        dr = i - a;
        x = upper_bound(pos[rest[i]].begin(), pos[rest[i]].end(), dr) -
            lower_bound(pos[rest[i]].begin(), pos[rest[i]].end(), st);
        rasp += x;
        pos[rest[i]].push_back(i);
    }

    ofstream out("divk.out");
    out << rasp;
    out.close();
}

int main()
{
    citire();
    rezolvare();
    return 0;
}