Codeforces 724C Ray Tracing 扩展欧几里得

吐槽:在比赛的时候,压根就没想到这题还可以对称;

题解:http://blog.csdn.net/danliwoo/article/details/52761839

比较详细;

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iomanip>
#include<map>
#include<set>
#include<vector>
#include<ctime>
#include<cmath>
#define LL long long 
using namespace std;
#define LL long long 
#define up(i,j,n) for(int i=(j);(i)<=(n);(i)++)
#define max(x,y) ((x)<(y)?(y):(x))
#define min(x,y) ((x)<(y)?(x):(y))
#define FILE "1"
const int maxn=101000;
const LL inf=10000000000000LL;
int n,m,k;
void init(){
    scanf("%d%d%d",&n,&m,&k);
}
void gcd(LL a,LL b,LL &d,LL &x,LL &y){
    if(b==0){d=a;x=1;y=0;return;}
    gcd(b,a%b,d,x,y);
    int t=x;
    x=y;
    y=t-a/b*x;
    return;
}
LL t(LL a,LL b,LL c,LL &x,LL &y ){
    LL d;gcd(a,b,d,x,y);
    if(c%d)return -1;
    LL ran=b/d;if(ran<0)ran=-ran;
    x*=c/d;
    x=(x%ran+ran)%ran;
    return 0;
}
LL get(LL x,LL y,LL maxx){
    LL K,S;
    if(t(2*n,-2*m,y-x,K,S)==-1)return maxx+1;
    LL ans=2*K*n+x;
    if(ans<0||ans>maxx)return maxx+1;
    return ans;
}
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL work(LL x,LL y){
    LL maxx=1LL*n/gcd(n,m)*m;
    LL minn=maxx+1;
    minn=min(get(x,y,maxx),minn);
    minn=min(get(-x,y,maxx),minn);
    minn=min(get(x,-y,maxx),minn);
    minn=min(get(-x,-y,maxx),minn);
    if(minn==maxx+1)return -1;
    else return minn;
}
void slove(){
    init();
    up(i,1,k){
        LL x,y;
        scanf("%I64d%I64d",&x,&y);
        printf("%I64d\n",work(x,y));
    }
}
int main(){
    slove();
}
View Code

 

posted @ 2016-10-09 17:24  CHADLZX  阅读(183)  评论(0编辑  收藏  举报