POJ 2769 Reduced ID Numbers 同余定理

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;
}
时间: 2024-12-27 01:16:12

POJ 2769 Reduced ID Numbers 同余定理的相关文章

POJ 2769 Reduced ID Numbers (同余)

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

poj 2769 Reduced ID Numbers(memset使用技巧)

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 larg

POJ 2769 Reduced ID Numbers

思路: 枚举 Reduced ID Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8847 Accepted: 3552 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s

解题报告 之 POJ2769 Reduced ID Numbers

解题报告 之 POJ2769 Reduced ID Numbers 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. Chu

POJ2769 Reduced ID Numbers【同余定理】

题目链接: http://poj.org/problem?id=2769 题目大意: 有N个学生,每个学生有一个唯一的学生证号(0~10^6),但是为了学生证号的范围有点大, 所以希望找到一个最小的正整数M,使得所有学生的学生证号对模M均不同余.那么问题来 了:这个M是多少. 思路: 将M从1开始测试,把所有学生证号对M取余的结果存起来,如果发现有相同的结果,就增 大M的值继续测试,直到满足所有学生的学生证号对M均不同余. AC代码: #include<iostream> #include&l

Reduced ID Numbers (memset 的关键用处)

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 larg

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM

poj 1845 Sumdiv (同余定理,快速幂取余)

链接:poj 1845 题意:求A^B的所有因子的和对9901取余后的值 如:2^3=8,8的因子有 1,2,4,8,所有和为15,取余后也是15 应用定理主要有三个: (1)整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)   其中pi均为素数 (2)约数和公式: 对于已经分解的整数A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn) 有A的所有因子之和为 S = 

poj 2635 The Embarrassed Cryptographer (同余定理,筛选法)

链接:poj 2635 题意:给定一个大数k,k是两个大素数的乘积的值,再给定一个int内的数L 问这两个大素数中最小的一个是否小于L,如果小于则输出这个素数. 分析:因为k达到了10^100,只能用字符串读入,再转化为千进制,用int数组存储, 然后枚举小于L的素数,看是否能被整除,即判断k%L是否为0, 这样就得先用筛选法求素数打表,但是注意要打表到大于10^6 关于高精度取余,就需要用到同余定理 如 123456%N=(((123%N)*1000%N)+456)%N #include<st