There must be a continuous K number, so that their abilities and abilities are divisible by N. Set a [I] As the prefix and
A [1] % N, a [2] % N ,..., the range of a [n] % N value is <n, so there are n numbers smaller than N. Two identical numbers will certainly appear, indicating, A part of the second number that is more than the first number must be divisible by N.
Pay attention to the handling of prefix and occurrence of 0 in the case.
#include<iostream>#include<cstdio>#include<cstring>#include<map>#include<vector>#include<stdlib.h>using namespace std;typedef long long LL;int main(){ int n; int a[22222]; int b[22222]; int vis[22222]; while (cin >> n){ memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i]; for (int i = 1; i <= n; i++){ b[i] += b[i - 1]; b[i] %= n; } int gg = 0; for (int i = 1; i <= n;i++) if (b[i] == 0){ cout << i << endl; for (int j = 1; j <= i; j++) cout << a[j] << endl; gg = 1; break; } if (gg) continue; int flag = 0; for (int i = 1; i <= n; i++){ if (flag) continue; if (!vis[b[i]]){ vis[b[i]] = i; } else{ cout << i - vis[b[i]] << endl; for (int j = vis[b[i]]+1; j <= i; j++) cout << a[j] << endl; break; } } } return 0;}
Poj2356find a multiple Pigeon nest Principle