codeforces 724c Ray Tracing
好题
原题:
There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of
meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
2 ≤ n, m ≤ 100 000, 1 ≤ k ≤ 100 000
题意:
在一个m*n的围墙内有一个激光从(0,0)发射,每过1s这个激光的x和y都会+1,如果激光撞到墙上就会反射,方向改变90°,如果撞到墙的四个角就停止,在墙内有k个测试点,现在问激光第一次经过每个测试点的时间是多少
首先易证激光一定会停下来而且每个点只会经过一次
正解是exgcd,思路非常妙
首先如果激光射到墙上,为了简化问题不让这个激光反射而是把这个围墙(连带着里面的探测点)沿着被撞倒的墙对称过去,差不多是酱紫:

然后当这个激光经过所有点的x都等于y,易证lcm(n,m)后激光停下
测试点原坐标为(x,y),随着围墙翻转过去后的坐标是(2*r*n±x,2*q*m±y)
如果翻转偶数次,测试点在围墙内的相对位置没变,坐标=2*r*n+x(这里只拿x举例),如果翻转奇数次,坐标=(2*r-1)*n+n-x=2*r*n-x
酱紫的话就有四种情况,只需要取这四种情况的最小值即可
然后对于每个测试点的每种情况,当两坐标相等的时候这个测试点被激光经过,即2*r*n+x=2*q*m+y(这里拿一种情况举例,其它类似(将x/y变为-x/-y即可))
n,m,x,y都是已知的,边形就可以得到2*n*r+2*m*q=y-x
这个就是exgcd的标准形式(注意只有r和q是未知的)
然后搞exgcd就可以辣
最后判一下无解,进行变化一下就可以得到一种情况的答案,与其它情况比较即可
注意exgcd后得出的x(这里的x不是坐标)需要(x%(b/gcd(a,b))+b/gcd(a,b))%(b/gcd(a,b)),先膜再加是防止负数,再膜一次为了保证取到最小值(如果不膜保证解合法但不一定最小)
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
ll read(){ll z=,mark=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')mark=-; ch=getchar();}
while(ch>=''&&ch<=''){z=(z<<)+(z<<)+ch-''; ch=getchar();}
return z*mark;
}
ll n,m,k,oo;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void exgcd(ll a,ll b,ll &x,ll &y){
if(!b){ x=,y=; return ;}
exgcd(b,a%b,x,y);
ll c=x; x=y,y=c-a/b*y;
}
ll q(ll sx,ll sy){
ll a=(n<<),b=-(m<<),c=sy-sx,x,y;
exgcd(a,b,x,y); ll d=gcd(a,b);
if(c%d) return oo;
ll md=abs(b/d);
x*=c/d; x=(x%md+md)%md;//如果不膜解合法但不是最小
ll z=*x*n+sx;
return (z<||z>=oo)?oo:z;
}
ll ans[];
int main(){//freopen("ddd.in","r",stdin);
cin>>n>>m>>k; oo=n*m/gcd(n,m)+;
ll x,y,z;
for(int i=;i<=k;++i){
x=read(),y=read(),z=oo;
z=min(z,q(x,y)),z=min(z,q(-x,-y));
z=min(z,q(-x,y)),z=min(z,q(x,-y));
printf("%I64d\n",(z==oo)?-:z);
}
return ;
}
codeforces 724c Ray Tracing的更多相关文章
- Codeforces 724C Ray Tracing 扩展欧几里得
吐槽:在比赛的时候,压根就没想到这题还可以对称: 题解:http://blog.csdn.net/danliwoo/article/details/52761839 比较详细: #include< ...
- CodeForces 724C Ray Tracing(碰撞类,扩展gcd)
又一次遇到了碰撞类的题目,还是扩展gcd和同余模方程.上次博客的链接在这:http://www.cnblogs.com/zzyDS/p/5874440.html. 现在干脆解同余模直接按照套路来吧,如 ...
- Ray Tracing
Ray Tracing 题目链接:http://codeforces.com/problemset/problem/724/C 拓展欧几里得 //为什么这次C题这么难啊=.= 可以观察到,光线在矩形中 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 数学
C. Ray Tracing 题目连接: http://codeforces.com/contest/724/problem/C Description oThere are k sensors lo ...
- OpenCascade Ray Tracing Rendering
OpenCascade Ray Tracing Rendering eryar@163.com 摘要Abstract:OpenCascade6.7.0中引入了光线跟踪算法的实现.使用光线跟踪算法可实现 ...
- 开始研究Ray tracing
几个月前面试时Boss问过我一个问题--"除了scanline渲染方法,你还知道什么其他渲染方式?",我没答出来,至今记忆犹新. 前段时间摆弄Intel VTune时看了它的示例代 ...
- 《Ray Tracing in One Weekend》、《Ray Tracing from the Ground Up》读后感以及光线追踪学习推荐
<Ray Tracing in One Weekend> 优点: 相对简单易懂 渲染效果相当好 代码简短,只看书上的代码就可以写出完整的程序,而且Github上的代码是将基类与之类写在一起 ...
- 【Ray Tracing The Next Week 超详解】 光线追踪2-7 任意长方体 && 场景案例
上一篇比较简单,很久才发是因为做了一些好玩的场景,后来发现这一章是专门写场景例子的,所以就安排到了这一篇 Preface 这一篇要介绍的内容有: 1. 自己做的光照例子 2. Cornell box画 ...
- 【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-7 混合概率密度
Preface 注:鉴于很多网站随意爬取数据,可能导致内容残缺以及引用失效等问题,影响阅读,请认准原创网址: https://www.cnblogs.com/lv-anchoret/category ...
随机推荐
- yii 第一步
第一步:index.php // include Yii bootstrap filerequire_once(dirname(__FILE__).'/../../framework/yii.php' ...
- android--------微信 Tinker 热修复 (二)
前面简单介绍了一下Tinker热修复,今天就来分享一下如何在Android中使用,希望对各位有帮助. 1:Tinker 接入指南 在项目的build.gradle中,添加tinker-patch-gr ...
- zzuli 1875多线DP
1875: 蛤玮的财宝 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 530 Solved: 116 SubmitStatusWeb Board De ...
- HDU-4850 Wow! Such String! (构造)
Problem Description Recently, doge starts to get interested in a strange problem: whether there exis ...
- dp练习(4)——过河卒
1010 过河卒 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 如图,A ...
- arc路径例子-磊哥
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> & ...
- Oracle12c 中RAC功能增强新特性之ASM&Grid
1. 自动存储管理(ASM)方面的增强 1.1. Flex ASM 在典型的网格架构安装中,每个节点有自己的ASM实例运行并扮演该节点上数据库的存储容器的角色,对这种安装配置,存在单点失败的 ...
- Python将列表作为栈和队列
Collections中的各种方法 阅读目录(Content) 一.各种方法介绍 二.代码部分 回到顶部(go to top) 一.各种方法介绍 Counter 统计个数 elements mo ...
- iOS系统版本与机型的对应关系
1.手机系统版本:10.3 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; 2.手机类型:iPhone 6 NSS ...
- duilib 实现列表头任意拖动
1.表头(xml) <List name="List_records" padding="5,10,5,5" bkcolor="#FFFFFFF ...