首页 技术 正文
技术 2022年11月18日
0 收藏 563 点赞 4,576 浏览 2207 个字

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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844