20140710 sequence

这道题当时拿着一看,想都没想就上了前缀和,但看了数据范围,还是必挂的节奏。

其实,应对每个前缀和取模,再桶排序一次。

还有 有负数

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 500005;
const int maxm = 500005;

int m,n;
int c[maxn];
int d[maxm];
long long ans;

int main() {
	//freopen("sequence.in","r",stdin);
	//freopen("sequence.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (int i = 1; i <= n; ++i) {
		scanf("%d",&c[i]);
		c[i] %= m;
		if (c[i]<0)
			c[i] += m;
		(c[i] += c[i-1]) %= m;
		ans += d[c[i]];
		++ d[c[i]];
	}
	ans += d[0];
	printf("%I64d\n", ans);
	return 0;
}

  

posted @ 2014-07-10 19:17  woshizyj  阅读(104)  评论(0编辑  收藏  举报