C. Ray Tracing

题目连接:

http://codeforces.com/contest/724/problem/C

Description

oThere 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.

Input

The first line of the input contains three integers n, m and k (2 ≤ n, m ≤ 100 000, 1 ≤ k ≤ 100 000) — lengths of the room's walls and the number of sensors.

Each of the following k lines contains two integers xi and yi (1 ≤ xi ≤ n - 1, 1 ≤ yi ≤ m - 1) — coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.

Output

Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or  - 1 if this will never happen.

Sample Input

3 3 4

1 1

1 2

2 1

2 2

Sample Output

1

-1

-1

2

Hint

题意

有一个球,一开始从00点开始发射,速度向量为(1,1),在一个nm的矩形里面弹来弹去

然后k个询问,问你第一次遇到这个点的时间是多少

题解:

其实可以转换成一个同余方程,然后求解就好了。

方程实际上是,x%2n=x0,x%2m=y0,显然可以转化为同余方程,exgcd求解就好了

HDU 5114

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF=1e16;
ll extend_gcd(ll a,ll b,ll &x,ll &y)
{
ll d=a;
if(b!=0)
{
d=extend_gcd(b,a%b,y,x);
y-=(a/b)*x;
}
else
{
x=1;
y=0;
}
return d;
}
ll xx,yy;
long long solve(int x, int y)
{
long long N = 2 * xx, M = 2 * yy;
long long X, Y;
long long g = extend_gcd(N, M, X, Y);
if ((y - x) % g != 0)
return INF;
long long lcm = 1ll * N * M / g;
X *= (y - x) / g;
long long x0 = X % lcm * N % lcm + x;
x0 = (x0 % lcm + lcm) % lcm;
if (x0 == 0)
x0 += lcm;
return x0;
} int main()
{
int k;
scanf("%lld%lld%d",&xx,&yy,&k);
long long t=min({solve(xx,yy),solve(0,0),solve(0,yy),solve(xx,0)});
while(k--)
{
long long xxx,yyy;
cin>>xxx>>yyy;
long long tt=min({solve(xxx,yyy),solve(2*xx-xxx,yyy),solve(xxx,2*yy-yyy),solve(2*xx-xxx,2*yy-yyy)});
if(tt<t&&tt<INF)cout<<tt<<endl;
else cout<<"-1"<<endl;
}
}

Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 数学的更多相关文章

  1. CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)

    1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort    暴力枚举,水 1.题意:n*m的数组, ...

  2. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)D Dense Subsequence

    传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...

  3. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort

    链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...

  4. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing

    我不告诉你这个链接是什么 分析:模拟可以过,但是好烦啊..不会写.还有一个扩展欧几里得的方法,见下: 假设光线没有反射,而是对应的感应器镜面对称了一下的话 左下角红色的地方是原始的的方格,剩下的三个格 ...

  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. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)

    题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...

  7. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)

    传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...

  8. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)

    传送门 Description You are given a table consisting of n rows and m columns. Numbers in each row form a ...

  9. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B

    Description You are given a table consisting of n rows and m columns. Numbers in each row form a per ...

  10. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A

    Description You are given names of two days of the week. Please, determine whether it is possible th ...

随机推荐

  1. asp.net分页之AJAX 分页

    查询功能是开发中最重要的一个功能,大量数据的显示,我们用的最多的就是分页. 在ASP.NET 中有很多数据展现的控件,比如Repeater.GridView,用的最多的GridView,它同时也自带了 ...

  2. Anaconda+django写出第一个web app(九)

    今天来学习外键的使用,用外键来连接数据库中的两个表. 当我们的tutorials非常多的时候,目前的显示方式就会使得页面非常凌乱.我们可以考虑把这些教程分为不同的系列,页面只显示标题以及概要等信息,进 ...

  3. [HNOI2013]比赛 (用Hash实现记忆化搜索)

    [HNOI2013]比赛 题目描述 沫沫非常喜欢看足球赛,但因为沉迷于射箭游戏,错过了最近的一次足球联赛.此次联 赛共N支球队参加,比赛规则如下: (1) 每两支球队之间踢一场比赛. (2) 若平局, ...

  4. 洛谷 P4910 帕秋莉的手环 矩阵乘法+快速幂详解

    矩阵快速幂解法: 这是一个类似斐波那契数列的矩乘快速幂,所以推荐大家先做一下下列题目:(会了,差不多就是多倍经验题了) 注:如果你不会矩阵乘法,可以了解一下P3390的题解 P1939 [模板]矩阵加 ...

  5. LEARN HOW TO HACK

    出处:https://www.hackerone.com/hacker101 什么是HACKER101? https://hacker101.com/Hacker101是一个视频,资源和实践活动的集合 ...

  6. oracle行转列

    wm_concat()函数 --创建表 create table test(id number,name varchar2(20)); --插入数据 insert into test values(1 ...

  7. python 列表元组加减乘除法

    元组(typle)列表(list)没有减法和除法,但有加法和乘法. 1.加法,即把元素相加.只可以list和tuple相加,不能加其他类型. t= (1, ) + (2, 3, 4) print(t, ...

  8. highchart 横轴纵轴数据

    1.highchart 横轴为字符串数组,必须加引号:纵轴为数值数组,不能加引号2.series中的json内容,属性不能加引号3.chart.height: Number,图表的高度.默认高度是根据 ...

  9. day22-23作业

    1.字节流  字符流    2.read()  3.-1  4.System.out  5.InputStream  6.OutputStream 1.IO流按流向分为输入流和输出流,即输入流和输出流 ...

  10. Ruby语言学习笔记

    在codecademy上开始玩ruby了 1.数据类型:boolean,string,number 变量直接用即可,不用声明(“拿来主义”) 运算符:+ - * / ** % == != && ...