中国剩余定理两个模板

1:普通剩余定理,要求模数互质:

(三个心理周期那题)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define LL long long

using namespace std;
int a[4],r[4];
int p,e,i,d;
int exgcd(int a,int b,int &x,int &y){
    if(b==0){
        x=1,y=0;
        return a;
    }
    int x1,y1;
    int gc=exgcd(b,a%b,x1,y1);
    if(a*b<0){
        x=-y1;
        y=-(x1-a/b*y1);
    }
    else{
        x=y1;
        y=x1-a/b*y1;
    }
    return gc;
}
void solve(){
    int M=r[1]*r[2]*r[3];
    int ans=0;
    for(int i=1;i<=3;i++){
        int x,y;
        int Mi=M/r[i];
        int gc=exgcd(Mi,r[i],x,y);
        //cout<<a[i]<<endl;
        ans=(ans+Mi*x*a[i])%M;
    }
    if(ans<0) ans+=M;
    if(ans<=d) ans+=21252;
    printf("%d days.\n",ans-d);
    //cout<<ans<<endl;
}
int main()
{
    int cas=1;
    int t;
    scanf("%d",&t);
    while(scanf("%d %d %d %d",&p,&e,&i,&d)){
        if(p==-1) break;
        a[1]=p,a[2]=e,a[3]=i;
        r[1]=23,r[2]=28,r[3]=33;
        printf("Case %d: the next triple peak occurs in ",cas++);
        solve();

    }
    return 0;
}

2:模数不互质,此时要采用两两合并的思想,假设要合并如下两个方程

 

     

 

那么得到

 

      

 

在利用扩展欧几里得算法解出的最小正整数解,再带入

 

      

 

得到后合并为一个方程的结果为

 

       

 

这样一直合并下去,最终可以求得同余方程组的解。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define siz 101
#define LL long long int
using namespace std;
LL p[siz],n;
bool f[siz];
LL a[siz],r[siz];
LL exgcd(LL a,LL b,LL &x,LL &y){
    if(b==0){
        x=1;
        y=0;
        return a;
    }
    LL x1,y1;
    LL d=exgcd(b,a%b,x1,y1);
    if(a*b<0){
        x=-y1;
        y=-(x1-a/b*y1);
    }
    else{
        x=y1;
        y=x1-a/b*y1;
    }
    return d;
}
void solve(int t){
    LL b,a1,c1;
    a1=a[0];c1=r[0];
    int flag=1;
    for(LL i=1;i<n;i++){
        LL c=r[i]-c1;
        LL m=a1;
        LL x,y;
        LL d=exgcd(m,a[i],x,y);
        if(c%d!=0){
            flag=0;
        }
        LL t=a[i]/d;
        x=((x*(c/d))%t+t)%t;
        c1=m*x+c1;
        a1=m*(a[i]/d);
    }
    if(!flag) cout<<"Case "<<t<<": "<<-1<<endl;
    else{
        if(c1==0) c1=a1;
        cout<<"Case "<<t<<": "<<c1<<endl;
    }
}
int main()
{
    //LL n;
    int t,tx=0;
    scanf("%d",&t);
    while(t--){
        //scanf("%lld",&n);
        cin>>n;
    for(int i=0;i<n;i++){
        //scanf("%lld %lld",&a[i],&r[i]);
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        cin>>r[i];
    }
    solve(++tx);
   }

   //cin>>n;

    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值