1. 程式人生 > >poj 2769 Reduced ID Numbers 同余定理

poj 2769 Reduced ID Numbers 同余定理

clas ide names lap 鏈接 ++ syn 不同 ans

鏈接: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

poj 2769 Reduced ID Numbers 同余定理