码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2769 Reduced ID Numbers 同余定理

时间:2014-10-17 23:28:04      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:数论

Reduced ID Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8989 Accepted: 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;
}


 

POJ 2769 Reduced ID Numbers 同余定理

标签:数论

原文地址:http://blog.csdn.net/u013068502/article/details/40190769

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!