码迷,mamicode.com
首页 > 其他好文 > 详细

51 Nod 1079 中国剩余定理(中国剩余定理)

时间:2017-10-11 10:37:42      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:type   点击   amp   pac   int   www.   ice   ret   http   

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#problemId=1079&noticeId=350893

题意:一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K。

例如,K % 2 = 1, K % 3 = 2, K % 5 = 3。符合条件的最小的K = 23。

题解:裸题,想看证明的可以点击传送门

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 typedef long long LL;
 6 const int N=12;
 7 LL a[N],m[N];
 8 
 9 LL e_gcd(LL a,LL b,LL &x,LL &y){
10     LL d=a;
11     if(b!=0){
12         d=e_gcd(b,a%b,y,x);
13         y-=(a/b)*x;
14     }    
15     else{x=1;y=0;}
16     return d;
17 }
18 
19 LL inv(LL t,LL p){
20     LL x,y;
21     if(e_gcd(t,p,x,y)==1) return (x%p+p)%p;
22     else return -1;
23 }
24 
25 
26 //x=a[i](mod m[i]) 
27 LL CRT(LL n,LL *a,LL *m){
28     LL M=1,ans=0;
29     for(int i=0;i<n;i++) M*=m[i];
30     for(int i=0;i<n;i++){
31         LL w=M/m[i];
32         ans=(ans+w*inv(w,m[i])*a[i])%M;
33     } 
34     return (ans+M)%M;
35 }
36 
37 int main(){
38     int n;
39     cin>>n;
40     for(int i=0;i<n;i++) cin>>m[i]>>a[i];
41     cout<<CRT(n,a,m)<<endl;
42     return 0;
43 }

 

51 Nod 1079 中国剩余定理(中国剩余定理)

标签:type   点击   amp   pac   int   www.   ice   ret   http   

原文地址:http://www.cnblogs.com/Leonard-/p/7648508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!