Cod sursa(job #65675)

Utilizator peanutzAndrei Homorodean peanutz Data 11 iunie 2007 14:13:13
Problema Divk Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <stdio.h>
#include <stdlib.h>
#define NMAX 600000
#define KMAX 100010

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