zoukankan      html  css  js  c++  java
  • 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
    ",work(x,y));
        }
    }
    int main(){
        slove();
    }
    View Code
  • 相关阅读:
    day06_02 继承
    day06_03 多继承区别
    day03_04 字符集编码转换
    day04_03 序列化与反序列化
    day04_06 单线程生成器的并行效果(协程)
    day04_02 装饰器 高阶版
    day04_05 内置方法
    复合控件的开发心得
    从子节点找父节点的循环sql
    asp中试用存储过程
  • 原文地址:https://www.cnblogs.com/chadinblog/p/5943097.html
Copyright © 2011-2022 走看看