Cod sursa(job #1979545)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 10 mai 2017 20:12:14
Problema Divk Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <cstdlib>
#include <fstream>
#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()
{
    freopen("divk.in", "r", stdin);
    scanf("%d %d %d %d", &n, &k, &a, &b);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &v[i]);
}

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;
}