原题:
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 = 10 6-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.
题解:
同余定理的应用,给出一组n个数据,找出一个数m使得每一个数对m求余都不相同。思路就是从1开始枚举i,直到找到一个i使得集合中的任意两个数对i的余数都不相同。设置一个bool型的数组p来判断是否有相同的余数。
代码:AC
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
const int maxn= 500;
int main()
{
bool p[100010];
int arr[maxn];
int T;
cin>>T;
while(T--)
{
int n,i,j;
cin>>n;
for( i = 0; i < n; ++i)
cin>>arr[i];
for( i = 1; ; ++i)
{
bool flag = true;
memset(p, false, sizeof(p));
for(j = 0; j < n; ++j)
{
if(p[ arr[j]%i ])
{
flag = false;
break;
}
p[ arr[j]%i ] = true;
}
if(flag)
{
cout<<i<<endl;
break;
}
}
}
return 0;
}