同余概述

 例题一:

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. 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

题目的意思是:给一系列个数,要你求一个数,然后使他们的mod都不一样
数据范围也没有多大,直接暴力去算即可

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
bool p[100005];
int d[100005];
int main()
{
    int i,j,k;
    int t;
    int num;
    cin>>t;
    while(t--)
    {
        cin>>num;
        for(i=1;i<=num;i++)
            scanf("%d",&d[i]);
        bool find;
        for(i=1;;i++)
        {
            find=1;
            memset(p,0,sizeof(p));
            for(j=1;j<=num;j++)
            {
                if(p[d[j]%i])
                {
                    find=0;
                    break;
                }
                p[d[j]%i]=1;
            }
            if(find)
                break;
        }
        cout<<i<<endl;
    }
    return 0;
 }

 例题2 Fibonacci Again(hdu 1021 )

令有一种斐波那契数列,F(0)=7,F(1)=11,F(n)=F(n-1)+F(n-2)(n>=2),判别数列的任意一项能否被3整除;

输入:输入数据包含多组,每组一个正整数n;

输出:如果F(n)能被3整除,输出“yes”,否则输出“no”;

 分析:由同余式的基本性质(a+b)%c=(a%c+b%c)%c;

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 1000011
int F[N];
void solve()
{
    int i;
    F[0]=7%3;
    F[1]=11%3;
    for(i=2;i<=1000000;i++)
    {
        F[i]=(F[i-1]%3+F[i-2]%3)%3;
    }
}
int main()
{
    solve();
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(F[n]==0)
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值