Poj 2769 Reduced ID Number

Reduced ID Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7882   Accepted: 3181

Description

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
987651

Sample Output

1
8

此题完全可以用暴力的解法,我用了188ms。同样是暴力的解法,不注意优化就会TLE的。

①memset的问题:标记数组开了10的6次方大,循环内如果每次都将标记数组完全初始化,浪费的时间过多。可以要多少初始化多少

②循环变量i初始值可以为n,因为n个不同的数对小于n的某个数 取模运算 是一定会有重复值的。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 1050
#define INF 0x7FFFFFFF
# define eps 1e-5
using namespace std;
bool visit[1000005];
int main()
{
    int t,n,i,a[500],j,tmp;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        memset(visit,0,sizeof(bool)*n);//要多少初始多少
        bool ok;
        for(i=n; ; i++)//从n开始
        {
            ok = 1;
            for(j=0; j<n; j++)
            {
                tmp = a[j] % i;
                if(visit[tmp] == 0)
                    visit[tmp] = 1;
                else
                {
                    ok = 0;
                    memset(visit,0,sizeof(bool)*i);//要多少初始多少
                    break;
                }
            }
            if(ok == 1)
            {
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}


你可能感兴趣的:(优化,Integer,input,each,output)