基于题目的--中国剩余定理

资料来源1:https://blog.csdn.net/

资料来源2:https://blog.csdn.net/

资料来源3:https://blog.csdn.net/

 资料来源4:https://blog.csdn.net/

资料来源5:https://blog.csdn.net/lianai911/article/details/39288703


    x ≡ a1 (mod m1)    x%m1 = a1     
    x ≡ a2 (mod m2)    x%m2 = a2
(S):x ≡ a3 (mod m3) -> x%m3 = a3
    ...                ...
    x ≡ an (mod mn)    x%mn = an

题目一:Hello KiKi

 Problem Description

One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一群鸭,快来快来 数一数,二四六七八". And then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.

 Input

The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi

 Output

For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.

 Sample Input

2 2 14 57 5 56 5 19 54 40 24 80 11 2 36 20 76

 Sample Output

Case 1: 341 Case 2: 5996

AC代码:

typedef long long ll;
ll e_gcd (ll a, ll b, ll& x, ll& y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return a;
    }
    ll ans = e_gcd (b, a % b, y, x);
    y -= a / b * x; //这个和前面用的方法不一样,不过是对的,写起来更快、
    return ans;
}
ll CR(int a[],int m[],int n)
{
    ll M = 1;
    for (int i = 1;i <= n;++i) M*=m[i];
    ll ans = 0;
    for (int i = 1;i <= n;++i)
    {
        ll Mi = M/m[i];ll x,y;
        ll t = e_gcd(m[i],Mi,x,y);
        ans = (ans + y*Mi*a[i])%M;
    }
    return (M+ans%M)%M;
}

题目二:Biorhythms 

Description

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier. 
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak. 

Input

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form: 

Case 1: the next triple peak occurs in 1234 days. 

Use the plural form ``days'' even if the answer is 1.

Sample Input

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Sample Output

Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

Source

East Central North America 1999

AC代码1:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
ll e_gcd(ll a,ll b,ll &x,ll &y)
{
    if (b == 0)
    {
        x = 1,y = 0;
        return a;
    }
    ll ans = e_gcd(b,a%b,y,x);
    y -= a/b*x;
    return ans;
}
ll CR(int a[],int m[],int n)
{
    ll M = 1;
    for (int i = 1;i <= n;++i) M*=m[i];
    ll ans = 0;
    for (int i = 1;i <= n;++i)
    {
        ll Mi = M/m[i]; ll x,y;
        ll t = e_gcd(m[i],Mi,x,y);
        ans = (ans+y*Mi*a[i])%M;
    }
    ans = (M+ans%M)%M;
    if (ans == 0) //当余数都为0
    {
        ans = 1;
        for (int i = 1; i <= n; ++i)
        {
            ans = ans*m[i]/__gcd(ans,(ll)m[i]);
        }
    }
    return ans;
}
int main()
{
    int a[4];
    int m[4] = {0,23,28,33};int d;int kas = 0;
    while (~scanf("%d %d %d %d",a+1,a+2,a+3,&d))
    {
        if (a[1]==-1&&a[2]==-1&&a[3]==-1&&d==-1) break;
        for (int i = 1;i <= 3;++i)
        {
            a[i] = a[i] - d;
            while (a[i]<0) a[i]+=m[i];
        }
        ll ans = CR(a,m,3);
        printf("Case %d: the next triple peak occurs in %lld days.\n",++kas,ans);
    }
    return 0;
}

AC代码2:

#include <cstdio>
using namespace std;
int m[4],a[4],M;
void Ext_Gcd(int a,int b,int &d,int &x,int &y)
{
    if(!b){
        x=1;y=0;d=a;
        return ;
    }
    else
    {
        Ext_Gcd(b,a%b,d,y,x);
        y-=a/b*x;
    }
}
int China(int r)
{
    M=1;
    int i,Mi,x0,y0,d,ans=0;
    for(int i=1;i<=r;i++)
        M*=m[i];
    for(int i=1;i<=r;i++){
        Mi=M/m[i];
        Ext_Gcd(Mi,m[i],d,x0,y0);
        ans=(ans+Mi*x0*a[i])%M;
    }
    if(ans<0)
        ans+=M;
    return ans;
}
int main()
{
    int Case=0;
    int p,e,i,d;
    while(~scanf("%d%d%d%d",&p,&e,&i,&d)&&p!=-1&&e!=-1&&i!=-1&&d!=-1){
            a[1]=p;a[2]=e;a[3]=i;
            m[1]=23;m[2]=28;m[3]=33;
            int ans=China(3);
            while(ans<=d)
                ans+=M;
            printf("Case %d: the next triple peak occurs in %d days.\n",++Case,ans-d);
    }
    return 0;
}

 


中国剩余定理

Thm 若m1,m2,...,mrThm 若m1,m2,...,mr是两两互素的正整数,则同余方程组(同上面的解一元线性同余方程组),x≡ai(modmi)x≡ai(modmi)有模M=m1m2...mnM=m1m2...mn的唯一解。

即左式为方程组的解

//解方程组x=ai(mod mi) mi之间两两互质
int China(int r)
{
    int M = 1, ans = 0;
    for (int i = 0; i < r; ++i)
        M *= m[i];
    for(int i = 0;i < r;i++)
    {
        int N = M/m[i];
        int x, y;
        extend_Euclid(N, m[i], x, y);
        ans = (ans+a[i]*N*x)%M;
    }
    ans = (ans - a[r])%M;
    return (ans+M)%M;
}

 


题目三: Chinese remainder theorem again

Problem Description

我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的:
假设m1,m2,…,mk两两互素,则下面同余方程组:
x≡a1(mod m1)
x≡a2(mod m2)

x≡ak(mod mk)
在0<=<m1m2…mk内有唯一解。
记Mi=M/mi(1<=i<=k),因为(Mi,mi)=1,故有二个整数pi,qi满足Mipi+miqi=1,如果记ei=Mi/pi,那么会有:
ei≡0(mod mj),j!=i
ei≡1(mod mj),j=i
很显然,e1a1+e2a2+…+ekak就是方程组的一个解,这个解加减M的整数倍后就可以得到最小非负整数解。
这就是中国剩余定理及其求解过程。
现在有一个问题是这样的:
一个正整数N除以M1余(M1 - a),除以M2余(M2-a), 除以M3余(M3-a),总之, 除以MI余(MI-a),其中(a<Mi<100 i=1,2,…I),求满足条件的最小的数。

Input

输入数据包含多组测试实例,每个实例的第一行是两个整数I(1<I<10)和a,其中,I表示M的个数,a的含义如上所述,紧接着的一行是I个整数M1,M1...MI,I=0 并且a=0结束输入,不处理。

Output

对于每个测试实例,请在一行内输出满足条件的最小的数。每个实例的输出占一行。

Sample Input

2 1

2 3

0 0

Sample Output

5

Author

lcy

Source

2007省赛集训队练习赛(10)_以此感谢DOOMIII

代码:

/*

由于题意:a<Mi<100 (i=1,2,…I )
所以 不要讨论为0到情况,
而且题目意思,没有说有不存在到情况,
所以也不需要去判断是否存在

*/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

__int64 m[12];

__int64 Ex_gcd(__int64 a,__int64 b,__int64 &x,__int64 &y)//扩展欧几里得
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    __int64 g=Ex_gcd(b,a%b,x,y);
    __int64 hxl=x-(a/b)*y;
    x=y;
    y=hxl;
    return g;
}

void make_ini(__int64 n,__int64 a)
{
    __int64 i,x,y,m1,m2,r1,r2,t,c,d;
    m1=m[1];r1=m1-a;
    for(i=2;i<=n;i++)
    {
       m2=m[i];r2=m2-a;

       d=Ex_gcd(m1,m2,x,y);
       c=r2-r1;
       x=c/d*x;
       t=m2/d;
       x=(x%t +t)%t;

       r1=m1*x+r1;
       m1=(m1*m2)/d;
    }
    printf("%I64d\n",r1);
}

int main()
{
    __int64 n,a,i;
    while(scanf("%I64d%I64d",&n,&a)>0)
    {
        if(n==0&&a==0)break;
        for(i=1;i<=n;i++)
        scanf("%I64d",&m[i]);
        make_ini(n,a);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值