"Title description"
Since Cao Chong finished the elephant, Caocao began to fathom let son do some career, so sent him to Zhongyuan pig farm pig, but Cao Chong full not happy, so in the work so-so, there was a time when Caocao want to know the number of sows, so Cao Chong want to play Caocao a. For example, if there are 16 sows, if 3 pigsty are built, there is no place for the 1 pigs left. If you build 5 pigsty, but there are still 1 pigs, there is no place to go, and if you build 7 pigsty, there are 2 heads to go. You as Cao General's personal secretary of course to the accurate number of pigs to amid, what should you do?
Exercises
Very naked Chinese remainder theorem, can not be naked again ... (note with long long)
1 /*************2 Vijos 11643 by Chty4 2016.11.15 *************/6#include <iostream>7#include <cstdio>8#include <cstring>9#include <cstdlib>Ten#include <ctime> One#include <cmath> A#include <algorithm> - using namespacestd; -typedefLong Longll; thell N,ans,m (1), a[ the],m[ the]; - inline ll read () - { -ll x=0, f=1;CharCh=GetChar (); + while(!isdigit (CH)) {if(ch=='-') f=-1; Ch=GetChar ();} - while(IsDigit (CH)) {x=x*Ten+ch-'0'; Ch=GetChar ();} + returnx*F; A } at voidEXGCD (ll a,ll b,ll &x,ll &y) - { - if(b==0) {x=1; y=0;return;} -EXGCD (b,a%b,x,y); -ll t=x;x=y;y=t-a/b*y; - } in intMain () - { to //freopen ("cin.in", "R", stdin); + //freopen ("Cout.out", "w", stdout); -n=read (); the for(LL i=1; i<=n;i++) M[i]=read (), A[i]=read (), m*=M[i]; * for(LL i=1; i<=n;i++) $ {Panax Notoginsengll x,y,mi=m/M[i]; - EXGCD (mi,m[i],x,y); theAns= (Ans+mi*x*a[i])%M; + } A if(ans<0) ans+=M; theprintf"%i64d\n", ans); + return 0; -}
Cao Chong Pig