您的位置:首页 > 其它

51Nod 1079:中国剩余定理

2017-09-29 14:40 337 查看
题目链接:http://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=354280

AC代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;

const int maxn = 15;
typedef long long LL;
LL m[maxn]; ///模
LL a[maxn];
void extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
extend_gcd(b,a%b,x,y);
int temp = x;
x = y;
y = temp - (a/b)*y;
}
LL mod_inverse(LL b,LL n)
{
LL x,y;
extend_gcd(b,n,x,y);
return (x%n+n)%n;
}
int main()
{
int N;
LL M=1;
while(~scanf("%d",&N))
{
for(int i = 1; i <= N; i++)
{
//x = a[i] % m[i];
scanf("%d%d",&m[i],&a[i]);
M *= m[i];
}
LL X = 0;
for(int i = 1; i <= N; i++)
{
LL Mi = M/m[i];
///求Mi模m[i]的乘法逆元
X = (X+(a[i]*Mi*mod_inverse(Mi,m[i])%M))%M;
}
printf("%lld\n",(X+M)%M);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: