标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 99567 | Accepted: 18976 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
题解:线性方程列出来:(n-m)x+y*L=x-y;
因为次数是正整数;所以转换为最小整数解;
代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<vector> 7 #define mem(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 typedef long long LL; 11 void e_gcd(LL a,LL b,LL &d,LL &x,LL &y){ 12 if(!b){ 13 d=a; 14 x=1; 15 y=0; 16 } 17 else{ 18 e_gcd(b,a%b,d,x,y); 19 LL temp=x; 20 x=y; 21 y=temp-a/b*y; 22 } 23 } 24 LL cal(LL a,LL b,LL c){ 25 LL d,x,y; 26 e_gcd(a,b,d,x,y); 27 if(c%d!=0)return -1; 28 x*=c/d; 29 if(b<0)b=-b; 30 b/=d; 31 LL ans=x%b; 32 if(ans<=0)ans+=b; 33 return ans; 34 } 35 int main(){ 36 LL x,y,m,n,L; 37 while(~scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)){ 38 LL ans=cal(n-m,L,x-y); 39 if(ans==-1)puts("Impossible"); 40 else printf("%lld\n",ans); 41 } 42 return 0; 43 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4908496.html