題目連結:hdu 4647 Another Graph Game
解題思路
P[i]維護的為字首取模M為i最早出現的位置。
代碼
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = ;
int N, M, P[maxn];
int main () {
while (scanf("%d%d", &N, &M) == ) {
int s = , x;
memset(P, -, sizeof(P));
P[] = ;
int ans = ;
for (int i = ; i <= N; i++) {
scanf("%d", &x);
s = ((s + x) % M + M) % M;
if (P[s] == -) P[s] = i;
ans = max(ans, i - P[s]);
}
printf("%d\n", ans);
}
return ;
}