Poj 2769 reduced ID numbers homophone Theorem

Source: Internet
Author: User
Reduced ID numbers
Time limit:2000 ms Memory limit:65536 K
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 CED 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

211248663124866111111987651

Sample output

18


 

This question is an examination of the same remainder theorem. The idea is very simple. We enumerate J brute force until J satisfies the fact that any two numbers in the set are not equal to J's remainder. At this time, the loop is stopped;

Memset in the Code is faster than the for loop initialization to 0, but only when the array is large. When the array size is small, the for initialization is time-saving (I have timed out 4 or 5 times)

There are n students in total, so there must be at least N different values after the modulo is completed, and J in all programs should start from N. Of course, it won't be wrong from 1. It's just a small optimization.

The Code is as follows:

# 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 ++) // If memset is used here, it will time out! 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 homophone Theorem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.