POJ2767 Reduced ID Numbers-Congruence Theorem

Keywords: iOS

Title Link This is the link

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

Source

Northwestern Europe 2005

Main idea: Solve a minimum positive integer so that other numbers are different from each other.

Train of thought: enumerate each factor, judge the factor and judge whether it is congruent or not.

// Violence enumeration time can go by, //367MS, pay attention to the subtraction

This is the link

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios.sync_with_stdio(false);
// So cin can't be used with C's scanf,sscanf,getchar,fgets and so on.
#define N 1000000
int a[N+5];
bool p[N+5];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;++i)
            scanf("%d",&a[i]);
        int find;
        for(int i=1; ; ++i)
        {
            find=i;
            for(int j=0;j<=i;++j)//Zero the remainder
                p[j]=false;
            for(int j=0;j<n;++j)
            {
                int mod=a[j]%i;
                if(p[mod])//Representing that i is congruent
                {
                    find=0;
                    break;
                }
                p[mod]=true;;//Represents the remainder of this number
            }
            if(find)
                break;
        }
        printf("%d\n",find);
    }
    return 0;
}

// Another way of thinking is to use congruence theorem.

Congruence Formula: a_b (mod m) (i.e. a% M = b% m)

To put it simply and crudely, if A-B = m, then a% M = b% M

We use the congruence theorem sha to screen out some of them and then enumerate them one by one to judge whether they are different or not.

// Feeling time is 782MS longer than the previous one

This is the link

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios.sync_with_stdio(false);
// So cin can't be used with C's scanf,sscanf,getchar,fgets and so on.
#define N 1000000
bool vis[N+5];
int a[N+5];
bool temp[N+5];
int main()
{
    int T;cin>>T;
    while (T--)
    {
        int n;
        scanf("%d",&n);
        memset(vis,0,sizeof(vis));
        for (int i=1;i<=n;++i)
            scanf("%d",a+i);
        for (int i=1;i<=n;++i)
        {
            for (int j=i+1;j<=n;++j)
            {
                int d=abs(a[i]-a[j]);//A module that records congruences is only part of the record.
                vis[d] = 1;
            }
        }
        bool fd=true;
        for (int m=1;fd;++m)
        {
            if (!vis[m])//It means that there is no congruence in this number at present.
            {
                memset(temp,0,sizeof(temp));
                bool ok=true;
                for (int i = 1;ok&&i<=n;++i)//Verify that there is really no congruence
                {
                    if (temp[a[i]%m])
                        ok=false;
                    else
                        temp[a[i]%m]=true;
                }
                if (ok)
                {
                    printf("%d\n",m);
                    fd=false;
                }
            }
        }
    }
    return 0;
}

 

Posted by Pilli on Mon, 04 Feb 2019 00:30:16 -0800