好久没做codevs啦
1 #include<cstdio> 2 #include<cstdlib> 3 #include<algorithm> 4 #include<cstring> 5 #include<vector> 6 #define MAXN 100000+10 7 #define ll long long 8 #define pb push_back 9 #define ft first 10 #define sc second 11 #define mp make_pair 12 using namespace std; 13 ll M=1LL,X; 14 ll c[15],m[15]; 15 int n; 16 void extgcd(ll a,ll b,ll &x,ll &y){ 17 if(!b){ 18 x=1,y=0; 19 } 20 else{ 21 ll xx,yy; 22 extgcd(b,a%b,xx,yy); 23 x=yy,y=xx-a/b*yy; 24 } 25 } 26 ll Inv(ll x,ll p){ 27 //(x,p)=1 28 ll a,b; 29 extgcd(x,p,a,b); 30 return (a%p+p)%p; 31 } 32 ll a,b; 33 int main() 34 { 35 36 scanf("%d%lld%lld",&n,&a,&b); 37 for(int i=1;i<=n;i++){ 38 scanf("%lld%lld",&m[i],&c[i]); 39 M*=m[i]; 40 } 41 for(int i=1;i<=n;i++){ 42 X+=c[i]*M/m[i]*Inv(M/m[i],m[i]); 43 } 44 X%=M; 45 ll s=(a-X)/M-5; 46 while(X+s*M<a)s++; 47 ll t=(b-X)/M+5; 48 while(X+t*M>b)t--; 49 ll ans=t-s+1; 50 printf("%lld %lld ",ans,(ans?X+M*s:0LL)); 51 return 0; 52 }