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

题解: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. css-通过css让块显示或隐藏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. (1)sqlite基础

    一.安装sqlite 下载页面:http://www.sqlite.org/download.html 1.下载 sqlite-tools-win32-*.zip 和 sqlite-dll-win32 ...

  3. es6系列-变量的解构赋值

    git地址: https://github.com/rainnaZR/es6-study/tree/master/src/destructuring 变量的解构赋值 变量的解构赋值: 数组, 对象, ...

  4. module has no attribute 'seq2seq'

    tensorflow 中tf.nn.seq2seq.sequence_loss_by_example to tf.contrib.legacy_seq2seq.sequence_loss_by_exa ...

  5. 如何使用ssh远程编辑定时任务crontab?

    linxu定时任务使用crontab,编辑crontab可以直接编辑:crontab -e:也可以直接读取文件 crontab file.这两种操作都不需要特殊权限sudo.区别在于,crontab ...

  6. 使用hexo搭建github个人博客网站

    搭建步骤: 1>Mac或win电脑一台,本文以mac为例. 2>下载安装Git和Node 3>安装hexo 4>注册登录GitHub,创建一个仓库,库名格式为:GitHub用户 ...

  7. 《从0到1》读书笔记第一章&quot;未来的挑战&quot;第2记:做老子还是做孙子

    从1到N VS 从0到1 - 别让自己的小鸡鸡抓在别人的手上 近几年国内互联网创业上非常流行一种C2C(也就是Copy to China - 复制到中国)的创业模式,打的就是一个时间差和地域差.将在国 ...

  8. Jstl indexOf 参考

    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> < ...

  9. ZipOutputStream 用法 小计

    ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); 构造函数之后 文件就已经创建出来了 只是 0kb s.Write(bu ...

  10. Codeforces 569 B. Inventory

    click here~~ **B. Inventory** time limit per test1 second memory limit per test256 megabytes inputst ...