这是一个显然错误很大的解法:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int exgcd(int a,int b,int &x,int &y){
if(b==0){
x=1,y=0;
return a;
}
int g=exgcd(b,a%b,y,x);
y-=a/b*x;
return g;
}
signed main(){
//freopen("xx.in","r",stdin);
//freopen("xx.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int x,y,m,n,L,s,t;
cin>>x>>y>>m>>n>>L;
int g=exgcd(m-n,L,s,t);
if((y-x)%g!=0){
cout<<"Impossible";
return 0;
}
s*=(y-x)/g;
cout<<(s+abs(L/g))%abs(L/g);
return 0;
}
错误在于 (s+abs(L/g))%abs(L/g),应改为((s*(x-y)/g)%abs(L/g)+abs(L/g))%abs(L/g)。
提供Hack数据一组:
输入:
12348798 1111011111 789966787 777779721 2099999999
正确输出:
1006281659