poj 2769 Reduced ID Numbers 同余定理

链接:http://poj.org/problem?id=2769

题意:寻找数m,是的对于n个数的余数不同

思路:暴力,优化:同余部分不用测试

代码:

 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 int ans[1000001],vis[1000001];
 5 int main() {
 6     ios::sync_with_stdio(false);
 7     //freopen("in.txt","r",stdin);
 8     //freopen("out.txt","w",stdout);
 9     int t,n,i,j,ok;
10     cin>>t;
11     while(t--) {
12         cin>>n;
13         for(i=1; i<=n; ++i) cin>>ans[i];
14         for(i=1; i<1000001; ++i) {
15             ok=1;
16             for(j=0; j<=i-1; ++j) vis[j]=0;
17             for(j=1; j<=n; ++j) {
18                 if(vis[ans[j]%i]) {
19                     ok=0;
20                     break;
21                 }
22                 vis[ans[j]%i]=1;
23             }
24             if(ok) break;
25         }
26         cout<<i<<endl;
27     }
28     return 0;
29 }
View Code

 

posted @ 2017-11-16 18:31  lemonsbiscuit  阅读(195)  评论(0编辑  收藏  举报