自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干些事业,于是派他到中原养猪场养猪,可是曹冲满不高兴,于是在工作中马马虎虎,有一次曹操想知道母猪的数量,于是曹冲想狠狠耍曹操一把。举个例子,假如有16头母猪,如果建了3个猪圈,剩下1头猪就没有地方安家了。如果建造了5个猪圈,但是仍然有1头猪没有地方去,然后如果建造了7个猪圈,还有2头没有地方去。你作为曹总的私人秘书理所当然要将准确的猪数报给曹总,你该怎么办?
第一行包含一个整数n (n <= 10) – 建立猪圈的次数,解下来n行,每行两个整数ai, bi( bi <= ai <= 1000), 表示建立了ai个猪圈,有bi头猪没有去处。你可以假定ai,aj互质.
输出包含一个正整数,即为曹冲至少养母猪的数目。
题目大意:
找出最小的x使得x%m[0]=r[0],x%m[1]=r[1]....参考代码:
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const int MAXN=50;
typedef long long LL;
int n,m[MAXN],r[MAXN];
LL exgcd(LL a,LL b,LL& x,LL& y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL r=exgcd(b,a%b,x,y);
LL t=y;
y=x-a/b*y;
x=t;
return r;
}
LL china(int* m,int* r,int n)
{
LL M=1,re=0,x,y;
for(int i=0;i<n;i++)
M*=m[i];
for(int i=0;i<n;i++)
{
LL w=M/m[i];
exgcd(w,m[i],x,y);
re=(re+x*w*r[i])%M;
}
return (re+M)%M;
}
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d%d",&m[i],&r[i]);
printf("%lld\n",china(m,r,n));
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/noooooorth/article/details/47776891