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<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

bool p[100010];
int Num[100010];

int main()
{
    int T,M,N;
    cin >> T;
    while(T--)
    {
        cin >> N;
        for(int i = 0; i < N; ++i)
            cin >> Num[i];
        int flag;
        int M = 1;
        while(1)
        {
            flag = 1;
            memset(p,0,sizeof(p));
            for(int i = 0; i < N; ++i)
            {
                if(p[Num[i]%M])
                {
                    flag = 0;
                    break;
                }
                p[Num[i]%M] = 1;
            }
            if(flag)
                break;
            M++;
        }
        cout << M << endl;
    }

    return 0;
}
时间: 2024-10-06 23:21:49

POJ2769 Reduced ID Numbers【同余定理】的相关文章

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

解题报告 之 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

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

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--1465--Multiple【BFS+同余定理】

链接:http://poj.org/problem?id=1465 题意:给一个数字n,和m个数字,找一个由这些数字组成的最小的n的倍数,如果不存在输出0. 思路:这题怎么想都想不到bfs上去,看了别人的解题报告,其实是用bfs来枚举,但是加了一个牛逼的剪枝:同余.即如果A%X==B%X,则(A*10+K)%X==(B*10+K)%X. 我们枚举m中每一个数字做这个K,实际上是枚举了一个数,B*10是之前枚举的数字,如果这个数%X得到的值之前已经得到过,则没必要再往下计算,因为根据同余定理剩下的

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

LightOJ1214 Large Division 基础数论+同余定理

Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if there exists an integer c such that a = b * c. Input Input starts with an integer T (≤ 525), denot