Cod sursa(job #2201765)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 5 mai 2018 21:55:20
Problema Divk Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <vector>
#define LIM 1<<17
/// TONI BO$$ was here
/// #MLC

using namespace std;
char BUF[LIM];
int poz;

inline char getChar(){
    poz++;
    if(poz>=LIM){
    	fread(BUF,LIM,1,stdin);
    	poz=0;
    }
    return BUF[poz];
}

inline int getNr(){
    int r=0, semn=1;
    char ch=getChar();
    while(isdigit(ch)==0 && ch!='-') ch=getChar();
    if(ch=='-'){
        semn=-1;
        ch=getChar();
    }
    while(isdigit(ch)!=0){
        r=r*10+semn*(ch-'0');
        ch=getChar();
    }
    return r;
}

int v[500001];
vector <int> s[100001];

int main()
{
    int n,k,a,b,i,sum,p,u;
    freopen("divk.in","r",stdin);
    freopen("divk.out","w",stdout);
    n=getNr();
    k=getNr();
    a=getNr();
    b=getNr();
    for(i=1; i<=n; i++)
        v[i]=getNr();
    sum=0;
    s[0].push_back(0);
    for(i=1; i<=n; i++)
    {
        sum+=v[i];
        sum%=k;
        s[sum].push_back(i);
    }
    long long rez=0,nra,nrb;
    for(i=0; i<k; i++)
    {
        p=u=0;
        nra=0;
        while(u<s[i].size() && s[i][u]-s[i][p]<a)
            u++;
        u--;
        nra=1LL*u*(u+1)/2;
        u++;
        while(u<s[i].size())
        {
            while(p<=u && s[i][u]-s[i][p]>=a-1)
                p++;
            if(p>u || s[i][u]-s[i][p-1]==a-1)
                p--;
            nra+=(u-p);
            if(s[i][u]-s[i][p]==a-1)
                nra++;
            u++;
        }
        p=u=0;
        nrb=0;
        while(u<s[i].size() && s[i][u]-s[i][p]<=b)
            u++;
        u--;
        nrb=1LL*u*(u+1)/2;
        u++;
        while(u<s[i].size())
        {
            while(p<=u && s[i][u]-s[i][p]>=b)
                p++;
            if(p>u || s[i][u]-s[i][p-1]==b)
                p--;
            nrb+=(u-p);
            if(s[i][u]-s[i][p]==b)
                nrb++;
            u++;
        }
        rez+=(nrb-nra);
    }
    printf("%lld",rez);

    return 0;
}