題目鏈接:點擊打開鏈接
解題思路:
求組內最小的m使得組內各個數對m求餘所的值均不同。暴力的判斷吧。發現一個有趣的規律,G++省時間廢內存,C++費時間省內存。另外此題的數組不宜開的過大,我開了個10^6的數組,接下來各種超時,換成10^5就過了。
另外,我試了下用set結果還是超時。
完整代碼:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 100001;
int a[maxn] , b[maxn];
int n;
inline bool check(int m)
{
memset(b , 0 , sizeof(b));
for(int i = 0 ; i < n ; i ++)
{
if(b[a[i] % m])
return false;
else
b[a[i] % m] = 1;
}
return true;
}
int main()
{
#ifdef DoubleQ
freopen("in.txt" , "r" , stdin);
#endif;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = 0 ; i < n ; i ++)
scanf("%d",&a[i]);
int m;
for(m = 1 ; m < maxn ; m ++)
{
if(check(m))
{
printf("%d\n" , m);
break;
}
}
}
return 0;
}
更多精彩請訪問:點擊打開鏈接