POJ 2769 Reduced ID Numbers 同余定理

28 篇文章 0 订阅
Reduced ID Numbers
Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 8989Accepted: 3610

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

2
1
124866
3
124866
111111
987651

Sample Output

1
8


 

这道题是对同余定理的考查,思路很简单,暴力地枚举j,直到j满足集合中任意两个数对j取余都不相等,此时停止循环;

对于代码中的memset,比用for循环初始化为0快,只是在数组大的时候。在数组大小比较小时则for初始化比较省时(我在这超时了4、5次了)

一共是n个学生,所以去完模之后至少要有n个不同的值,所有程序里面的j要从n开始的。当然从1开始也不会错,只是一个小小的优化吧。

代码如下:

#include <stdio.h>
#include <string.h>
#include <math.h>

int a[10000001];

int main()
{
	int i,j,n;
	int cas,ans,t;
	int s[303];
	int f;
	scanf("%d",&cas);
	while(cas--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%d",&s[i]);
		for(j=n;j<1000000;j++)
		{
			f=0;
			for(i=0;i<=j;i++)		//这里用memset的话会超时的!
				a[i]=0;
			for(i=0;i<n;i++)
			{
				if(a[s[i]%j])
				{
					f=1;
					break;
				}
				a[s[i]%j]=1;	
			}
			if(!f)
				break;
		}
		printf("%d\n",j);
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值