1190 Reduced ID Numbers 求更高效的方法

本文介绍了一个算法问题,即如何找到使学生标识号(SIN)模运算后保持唯一性的最小正整数m。通过分析输入输出样例及提供的C语言实现代码,探讨了暴力求解方法及其效率问题。

1190. Reduced ID Numbers
 
 
Total: 1418Accepted:581Rating:
3.5/5.0(4 votes)
 

   
 
Time Limit: 2s Memory Limit: 32MB
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 = 106-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
Copy sample input to clipboard
2
1
124866
3
124866
111111
987651
Sample Output
1
8
 
我的笨方法,暴力ac
#include<stdio.h>
int judge(int mod[300],int n);
int main()
{
int t,n,i,j,mod[300],mark=1;
long int sin[300];
scanf("%d",&t);
while(t--)
{

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&sin[i]);
if(n==1)
{printf("%d/n",1);
continue;}

for(i=2;mark==1;i++)
{
for(j=0;j<n;j++)
mod[j]=sin[j]%i;

if(judge(mod,n))
mark=0;
}
  
printf("%d/n",i-1);
mark=1;
for(i=0;i<n;i++)
mod[i]=sin[i]=0;

}

}
int judge(int mod[300],int n)
{
int k,l;
       for(k=0;k<n;k++)
for(l=k;l<n-1;l++)
{
if(mod[l+1]==mod[k])
return 0;
}
return 1;
}
 
 
 
太慢了,用了,0.36,真不知道前面的那些牛人怎样0.01s的,求方法啊
    

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值