HDU 2669 Romantic
扩展GCD
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; __int64 extgcd(__int64 a, __int64 b, __int64 & x, __int64 & y) { if (b == 0) { x=1; y=0; return a; } __int64 d = extgcd(b, a % b, x, y); __int64 t = x; x = y; y = t - a / b * y; return d; } int main() { __int64 a,b,ansx,ansy; while(cin>>a>>b) { extgcd(a,b,ansx,ansy); if(a*ansx==1-b*ansy) { while(ansx<0) { ansx=ansx+b; ansy=ansy-a; } cout<<ansx<<" "<<ansy<<endl; } else cout<<"sorry"<<endl; } return 0; }