题目链接:http://codeforces.com/contest/982/problem/E
https://www.cnblogs.com/zhouzhendong/p/9055728.html
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll exgcd(ll a,ll b,ll &x,ll &y)//扩展gcd { ll r,t; if(b==0) { x=1;y=0; return a; } r=exgcd(b,a%b,x,y); t=x;x=y; y=t-a/b*y; return r; } ll cal(ll a,ll b,ll c)//求解ax+by=c的x的最小整数解 { ll x,y,ret,gcd; gcd=exgcd(a,b,x,y); if(c%gcd!=0) { puts("-1"); exit(0); } x*=c/gcd;b/=gcd; if(b<0) b=-b; ret=x%b; if(ret<=0) ret+=b; return ret; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n,m,x,y,vx,vy; scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&vx,&vy); if(vx==0) { if(x==0||x==n) return 0*printf("%d %d\n",x,vy>0?m:0); else return 0*puts("-1"); } if(vy==0) { if(y==0||y==m) return 0*printf("%d %d\n",vx>0?n:0,y); else return 0*puts("-1"); } int fx=0,fy=0; if(vx==-1) fx=1,x=n-x; if(vy==-1) fy=1,y=m-y; ll a=cal(n,m,(x-y)); ll b=-((x-y)-a*n)/m; ll ansx=n,ansy=m; if(a%2==0) ansx=n-ansx; if(b%2==0) ansy=m-ansy; if(fx) ansx=n-ansx; if(fy) ansy=m-ansy; printf("%lld %lld\n",ansx,ansy); return 0; }