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.
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 987651Sample Output
1 8
题意:输入n,表示测试的组数,g表示学生个数,输入g个数表示学号。输出各个学号模i后,余数不相同的最小i.
我直接用map标记,结果超时,后来百度了下,map会慢,只能用数组标记,这就需要用到memset函数初始化
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[301]; int b[1000001]={0}; int main() { int n,g; int temp; int i,j; cin>>n; while(n--) { cin>>g; for(i=0;i<g;i++) { cin>>a[i]; } for(i=g;i<1000001;i++) { int flag=1; memset(b,0,sizeof(int)*(i+1)); for(j=0;j<g;j++) { temp=a[j]%i; if(b[temp]!=0) { flag=0; break; } else { b[temp]=1; } } if(flag==1) { cout<<i<<endl; break; } } } }