标签:printf pre 同余 int ssi space using style namespace
题解:
要求s+px=t+qx (mod L)
移项 (p-q)x=t-s (mod L)
等价于 (p-q)x+Ly=t-s
即ax+by=c的方程最小非负根
exGcd后乘个C
1 #include<cstdio> 2 typedef long long ll; 3 using namespace std; 4 ll s,t,p,q,L; 5 ll gcd (ll x,ll y) 6 { 7 return y==0?x:gcd(y,x%y); 8 } 9 ll exGcd(ll a,ll b,ll &x,ll &y) 10 { 11 if (b==0) return x=1,y=0,a; 12 ll r=exGcd(b,a%b,y,x); 13 y-=(a/b)*x; 14 return r; 15 } 16 int main() 17 { 18 scanf("%lld%lld%lld%lld%lld",&s,&t,&p,&q,&L); 19 ll a=(p-q+L)%L,b=L,c=(t-s+L)%L,x,y,G=gcd(a,b); 20 if (c%G!=0) 21 { 22 puts("Impossible"); 23 return 0; 24 } 25 a/=G,b/=G,c/=G; 26 exGcd(a,b,x,y); 27 x=(x%b+b)%b; 28 x=x*c%b; 29 printf("%lld\n",x); 30 return 0; 31 }
标签:printf pre 同余 int ssi space using style namespace
原文地址:http://www.cnblogs.com/mrsheep/p/7898952.html