51Nod-1103 N的倍数

给定N,和N个数,求选出一个集合,使集合内的数和为N的倍数。
若解不存在,输出No Solution


其实解必存在,因为你任取N个集合,模N的结果一定有一个为0或者出现重复结果。
如果我们选取集合时是往集合内添加数,那么对于出现重复结果的情况,两种情况间加进的数的和为N的倍数

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

const int maxn = 5e4 + 10;
int A[maxn];
int pre[maxn];

int N;

void out(int x, int y) {
	printf("%d
", y - x);
	for (int i = x + 1; i <= y; i++) {
		printf("%d
", A[i]);
	}
}

int main() {
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) {
		scanf("%d", &A[i]);
	}
	int s = 0;
	for (int i = 1; i <= N; i++) {
		s = (s + A[i]) % N;
		if (pre[s] || s == 0) {
			out(pre[s], i);
			break;
		}
		pre[s] = i;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/xFANx/p/9532556.html