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;
}
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;
}
相关文章推荐
- 51NOD - 1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod 1079 中国剩余定理 (模板)
- 51nod-1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod:1079 中国剩余定理
- 51Nod-1079-中国剩余定理
- 51nod 1079 中国剩余定理
- 51NOD——T 1079 中国剩余定理
- 51Nod-1079-【数学】中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod--1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod-【1079 中国剩余定理】
- 51nod 1079 中国剩余定理
- 51nod - -1079 中国剩余定理
- 51Nod 1079中国剩余定理(孙子定理)