Reduced ID Numbers(POJ 2769)

题目:

Reduced ID Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8895 Accepted: 3576

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

代码:

1.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
    int i,j,k,g[305],n,t;
    bool  v[100005];
    cin>>t;
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d",&g[i]);
        for(i=1;;i++)
        {
            memset(v,0,sizeof(v));
            for(j=0;j<n;j++)
            {
                k=g[j]%i;
                if(v[k]!=0) break;
                else v[k]=true;
            }
            if(j==n) {break;}
        }
       printf("%d\n",i);
   }
return 0;
}
测试数据实在是太弱了,最大的m没有超过100006的,这个说法是不确切的,应该是所有数据的最大的余数没有超过100005的,所以v【100005】就可以水过,但是要是开成v【1000005】就由于每次memset就妥妥的TLE了

2.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
    int i,j,k,g[305],n,t,min1;
    bool  v[10000005];
    cin>>t;
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            {
                scanf("%d",&g[i]);
                
            }
        for(i=1;;i++)
        {
            memset(v,0,sizeof(bool)*i);
            for(j=0;j<n;j++)
            {
                k=g[j]%i;
                if(v[k]!=0) break;
                else v[k]=true;
            }
            if(j==n) {break;}
        }
       printf("%d\n",i);
   }
return 0;
}
这里每次只把最大的余数以内的V初始化,由于数据很弱,所以比代码一稍好一点当然可以水过
3.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
    int i,j,k,g[305],n,t,min1,cnt;
    bool  v[1000005];
    cin>>t;
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            {
                scanf("%d",&g[i]);

            }
        memset(v,0,sizeof(v));
        for(i=1;;i++)
        {
           for(j=0;j<n;j++)
            {
                k=g[j]%i;
                if(v[k]!=0) break;
                else v[k]=true;
            }
            if(j==n) {break;}
            else
            {
                for(cnt=0;cnt<j;cnt++)
               v[g[cnt]%i]=0;
            }
        }
       printf("%d\n",i);
   }
return 0;
}
这里只针对已经修改过的值进行针对性的初始化,相对来说更好一点,但是这不如memset的效率高,虽然比代码2时间稍长,但总体还是不错的
反思:

这个题目,一直超时,没有发现每次初始化做了很多的无用功,找到了,就呵呵了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值