POJ2769(同餘 + 暴力)

題目鏈接:點擊打開鏈接


解題思路:

求組內最小的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;
}

更多精彩請訪問:點擊打開鏈接
發佈了403 篇原創文章 · 獲贊 20 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章