Pagini recente » Cod sursa (job #1997209) | Cod sursa (job #2434332) | Cod sursa (job #1340634) | Cod sursa (job #686582) | Cod sursa (job #65672)
Cod sursa(job #65672)
#include <stdio.h>
#include <stdlib.h>
#define NMAX 500020
int n, k, a, b;
int s[NMAX];
int count;
int r[NMAX];
int *list[NMAX];
void read()
{
int i;
scanf("%d %d %d %d", &n, &k, &a, &b);
list[0] = (int *) realloc(list[0], sizeof(int));
list[0][0] = 0;
for(i = 1; i <= n; ++i)
{
scanf("%d", s+i), s[i] += s[i-1];
if(i < k)
{
list[i] = (int *) realloc(list[i], sizeof(int));
list[i][0] = 0;
}
}
}
void solve()
{
int i, j, aux;
int p1, p2;
for(i = 1; i <= n; ++i)
{
aux = s[i] % k;
list[aux] = (int *) realloc(list[aux], (++list[aux][0]+1)*sizeof(int));
list[aux][list[aux][0]] = i;
}
for(i = 1; i <= list[0][0]; ++i)
if(list[0][i] <= b && list[0][i] >= a)
++count;
for(i = 0; i < k; ++i)
{
if(list[i][0] > 1)
{
for(p1 = 1, p2 = 2; p2 <= list[i][0]; ++p2)
{
while(p1 < p2)
{
aux = list[i][p2] - list[i][p1];
if(aux <= b && aux >= a)
{
count += p2-p1;
break;
}
else if(aux < a)
break;
++p1;
}
}
}
}
}
int main()
{
freopen("divk.in", "r", stdin);
freopen("divk.out", "w", stdout);
read();
solve();
printf("%d\n", count);
fclose(stdin);
fclose(stdout);
return 0;
}