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

题解: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=;
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==){d=a;x=;y=;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 -;
LL ran=b/d;if(ran<)ran=-ran;
x*=c/d;
x=(x%ran+ran)%ran;
return ;
}
LL get(LL x,LL y,LL maxx){
LL K,S;
if(t(*n,-*m,y-x,K,S)==-)return maxx+;
LL ans=*K*n+x;
if(ans<||ans>maxx)return maxx+;
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+;
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+)return -;
else return minn;
}
void slove(){
init();
up(i,,k){
LL x,y;
scanf("%I64d%I64d",&x,&y);
printf("%I64d\n",work(x,y));
}
}
int main(){
slove();
}

Codeforces 724C Ray Tracing 扩展欧几里得的更多相关文章

  1. CF724C Ray Tracing 扩展欧几里得 平面展开

    LINK:Ray Tracing 虚这道题很久了 模拟赛考了一个加强版的 瞬间就想到了这道简化版的. 考虑做法 暴力模拟可能可以 官方正解好像就是这个. 不过遇到这种平面问题可以考虑把平面给无限的展开 ...

  2. codeforces 1244C (思维 or 扩展欧几里得)

    (点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...

  3. CodeForces 724C Ray Tracing(碰撞类,扩展gcd)

    又一次遇到了碰撞类的题目,还是扩展gcd和同余模方程.上次博客的链接在这:http://www.cnblogs.com/zzyDS/p/5874440.html. 现在干脆解同余模直接按照套路来吧,如 ...

  4. codeforces 724c Ray Tracing

    好题 原题: There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is ...

  5. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  6. C. Ray Tracing——披着搜索外衣的扩展欧几里得

    [题目大意] 给你一个n*m的矩形,光线从(0,0)出发,沿右上方向以每秒根号2米的速度运动,碰到矩形边界就会反弹(符合物理规律的反弹),询问k个点,这些点都在矩形内部且不在矩形边界上,求光经过这些点 ...

  7. 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  8. 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

    题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...

  9. [codeforces 200 E Tractor College]枚举,扩展欧几里得,三分

    题目出自 Codeforces Round #126 (Div. 2) 的E. 题意大致如下:给定a,b,c,s,求三个非负整数x,y,z,满足0<=x<=y<=z,ax+by+cz ...

随机推荐

  1. CodeForces - 11D A Simple Task

    Discription Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycl ...

  2. ORACLE MOS 翻译

    http://blog.csdn.net/msdnchina/article/details/53174196

  3. TClientDataSet的 fastscript封装

    TClientDataSet的 fastscript封装 // 陈新光 2017-2-10// TClientDataSet's fastscript unit fs_ClientDataSet; i ...

  4. 【java】RC4加密转16进制获取长度为40的不重复优惠码字符串 【未优化版本】

    需求:需要一串给各机构独有的优惠码 间接需求:固定长度.不重复.没有规律可循 实现思想如下: 1.首先获取一个UUID 2.去除UUID中的“-” 3.小写转大写 4.获取一个固定长度字符串 5.按照 ...

  5. nodeJS一些事儿

    node-webkit:开发桌面+WEB混合型应用的神器[大漠穷秋] 展望未来 其实这条路老早就有人在走 网上有很多人在争论,未来究竟是原生的应用会胜出,还是WEB APP会胜出,实际上这两者并不是你 ...

  6. 采用scp命令在Linux系统之间copy文件

    不同的Linux之间copy文件常用有3种方法,第一种就是ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy.第二种方法就是采用 ...

  7. 微服务指南走北(三):Restful API 设计简述

    API的定义取决于选择的IPC通信方式,假设是消息机制(如 AMQP 或者 STOMP).API则由消息频道(channel)和消息类型.假设是使用HTTP机制,则是基于请求/响应(调用http的ur ...

  8. Git以及github的使用方法(三),git status查看工作区的状态,git diff查看具体修改内容

    我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容: Git is a distributed version c ...

  9. Laravel 设置语言不生效的问题

    使用了validate 验证,提示错误默认是 英文的.将en 改为zh-CN 后 运行 composer require "overtrue/laravel-lang:~3.0"时 ...

  10. vue native

    1.示例 <el-icon icon="Setting" size="sm" @click.native="ceshi">< ...