曹冲养猪

问题 S: 曹冲养猪

时间限制: 1 Sec  内存限制: 128 MB
提交: 5  解决: 2
[提交] [状态] [讨论版] [命题人:admin]

题目描述

自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干些事业,于是派他到中原养猪场养猪,可是曹冲满不高兴,于是在工作中马马虎虎,有一次曹操想知道母猪的数量,于是曹冲想狠狠耍曹操一把。举个例子,假如有16头母猪,如果建了3个猪圈,剩下1头猪就没有地方安家了。如果建造了5个猪圈,但是仍然有1头猪没有地方去,然后如果建造了7个猪圈,还有2头没有地方去。你作为曹总的私人秘书理所当然要将准确的猪数报给曹总,你该怎么办?

输入

第一行包含一个整数n (n <= 10) – 建立猪圈的次数,解下来n行,每行两个整数ai, bi( bi <= ai <= 1000), 表示建立了ai个猪圈,有bi头猪没有去处。你可以假定ai,aj互质.

输出

输出包含一个正整数,即为曹冲至少养母猪的数目。
 

样例输入

3
3 1
5 1
7 2

样例输出

16
#include<bits/stdc++.h>
#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
#define PER(i, a, b) for(int i = (a); i >= (b); -- i)
using namespace std;
const int maxn=1e5+5;
typedef long long ll;
template <class T>
inline void rd(T &ret){
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9'){
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
int n;
ll p[16],q[16];
ll tot;
void exgcd(ll a,ll b,ll &x,ll &y){
       if(!b){
            x=1,y=0;
            return;
       }
       exgcd(b,a%b,y,x);
       y-=x*(a/b);
}
int main()
{
    ll ans=0;
    rd(n),tot=1;
    REP(i,1,n)rd(p[i]),rd(q[i]),tot*=p[i];
    REP(i,1,n){
         ll tmp=tot/p[i],x,y;
         exgcd(tmp,p[i],x,y);
         ans=(ans+(x*q[i]*tmp)%tot+tot)%tot;
    }
    printf("%lld",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/czy-power/p/10460515.html