POJ_2769同余问题

Reduced ID Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9310   Accepted: 3740

Description

T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN with MaxSIN = 106-1. T. Chur finds this range of SINs too large for identification within her groups. For each group, she wants to find the smallest positive integer m, such that within the group all SINs reduced modulo m are unique.

Input

On the first line of the input is a single positive integer N, telling the number of test cases (groups) to follow. Each case starts with one line containing the integer G (1 ≤ G ≤ 300): the number of students in the group. The following G lines each contain one SIN. The SINs within a group are distinct, though not necessarily sorted.

Output

For each test case, output one line containing the smallest modulus m, such that all SINs reduced modulo m are distinct.

Sample Input

2
1
124866
3
124866
111111
987651

Sample Output

1
8
/*poj2769 
 *题意:给出g个数,求出最小的数使得这g个数模上这最小的数的余数是唯一的
 *算法分析:看题目限制时间为2s,第一想法就是扫描了,从1开始到最大的数加1进行扫描,
 *判断余数是否唯一。是则结束 ,这里注意地方就是不能刚好到最大的数结束、而是到最大的数加1
 *结束,我试了下刚好扫到最大的数WA了、、、、 
*/ 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;

int a[310];
int b[310];					//保存余数 

int main() {
	int t;
	cin >> t;
	while (t --) {
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		int n;
		cin >> n;
		for (int i = 0; i<n; i++)
			cin >> a[i];
		sort(a, a+n);
		int p = a[n-1];
		int flag = 1, res, flag1 = 1;
		for (int i = 1; i<=p+1 && flag1; i++) {
			flag = 1;
			for (int j = 0; j<n; j++) {
				b[j] = a[j] % i;
			}
			sort(b, b+n);
			for (int j = 0; j<n-1 && flag; j++) {
				if (b[j] == b[j+1]) {
					flag = 0;
				}
			}
			if (flag) {
				res = i;
				flag1 = 0;
			}
			memset(b, 0, sizeof(b));
		}
		//cout << flag<< endl;
		//cout << b[0]<< endl<< b[1]<< endl<< b[2]<< endl;
		cout << res << endl;
	}
	
	return 0;
}

不怕脚下的路有多难走、就怕你压根不想走下去、、
原文地址:https://www.cnblogs.com/Tovi/p/6194837.html