11237-Halloween treats
Question Link
Have c friends and N houses (C <= N ).AIA candy. You need to select a house so that the candy can be evenly distributed to your friends.
Idea: The condition C <= N is critical. If this condition exists, you can open a sum [I] to record the prefix of 0-I and the value of % C, in this way, two repeated values are bound to appear in the n-length array. Using sum [I]-sum [J] = 0 is the answer.
Code:
#include <cstdio>#include <cstring>const int N = 100005;int c, n, a[N], sum[N], vis[N];void solve() { memset(vis, -1, sizeof(vis)); vis[0] = 0; for (int i = 1; i <= n; i++) {sum[i] = (sum[i - 1] + a[i]) % c;if (vis[sum[i]] != -1) { for (int j = vis[sum[i]] + 1; j < i; j++)printf("%d ", j); printf("%d\n", i); return;}vis[sum[i]] = i; }}int main() { while (~scanf("%d%d", &c, &n) && c + n) {for (int i = 1; i <= n; i++) scanf("%d", &a[i]);solve(); } return 0;}