Cod sursa(job #1979552)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 10 mai 2017 20:31:12
Problema Divk Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

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

int n, k, a, b;
int v[nMax];
int rest[nMax];
int ap[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;
    rest[0] = 0;
    queue<int> q;
    for(int i = 1; i <= n; ++i)
    {
        rest[i] = (rest[i-1] + v[i]) % k;
        if(i >= a)
        {
            ap[rest[i-a]]++;
            q.push(rest[i-a]);
        }
        if(q.size() > b - a + 1)
        {
            ap[q.front()]--;
            q.pop();
        }

        rasp += ap[rest[i]];
    }

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

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