Ray Tracing(扩展欧几里得,ax+by=c最小解)

C. Ray Tracing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line of the input contains three integers nm and k (2 ≤ n, m ≤ 100 0001 ≤ 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 - 11 ≤ 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.

Examples
input
3 3 4
1 1
1 2
2 1
2 2
output
1
-1
-1
2
input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
output
1
-1
-1
2
5
-1
input
7 4 5
1 3
2 2
5 1
5 3
4 3
output
13
2
9
5
-1
Note

In the first sample, the ray will consequently pass through the points (0, 0)(1, 1)(2, 2)(3, 3). Thus, it will stop at the point (3, 3) after3 seconds.

In the second sample, the ray will consequently pass through the following points: (0, 0)(1, 1)(2, 2)(3, 3)(2, 4)(1, 3)(0, 2),(1, 1)(2, 0)(3, 1)(2, 2)(1, 3)(0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3)(2, 4),(0, 2)(2, 0) and (3, 1).



把矩形展开,我们观察发现,小球总是在m*n/(gcd(m,n))时间停止,所以把矩形展开到边长为m*n/(gcd(m,n))的正方形。然后把要求的点扩展,对于点(x,y),他扩展的点横坐标为2*k+x或2*k-x,纵坐标为2*s+y或2*s-y。k,s 为常数,这样我们的问题转换为求

2*k*n ±x=2*s *m±y. 这个等式的最小解,即求最小的k,s满足等式。最后的答案就是2*k*n ±x。

通过扩展欧几里得,在加上改进,可以求ax+by=c中最小的x,y (a,b,c为常数)满足等式。那么问题就可以解决

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<cstdio>
#include<math.h>
#include<vector>
using namespace std;
long long c;
long long maxn;
long long n,m,k;
long long dx,dy;
int gcd(long long a,long long b)
{
    return b ? gcd(b,a%b) : a;
}
long long extended_euclid(long long a,long long b,long long &x,long long &y)  
{  
    if(b==0)  //即gcd(a,b)=a   
    {  
        x=1;y=0;  
        return a;  
    }  
    long long n=extended_euclid(b,a%b,y,x);  //最大公约数相等    
    y-=a/b*x;
    return n;  
}  
  //ax+by=c
long long kk(long long a, long long b, long long c, long long &x, long long &y)  
{  
    long long g = extended_euclid(a, b, x, y);  
    if(c % g) return -1;  
    long long ran = b / g;  
    x *= c/g;  
    if(ran < 0) ran = -ran;  
    x = (x%ran + ran) % ran;  
    return 0;  
}  
long long check(long long x,long long y,long long dx,long long dy)
{
	if(kk(2*n,-2*m,y-x,dx,dy)==-1)
	{
		return maxn+1;
	}
	else
	{
		if(2*n*dx+x>maxn||2*n*dx+x<0)
			return maxn+1;
		//cout<<y-x<<" "<<dx<<" "<<dy<<endl;
		return 2*n*dx+x;
	}
}
int main()
{
	scanf("%lld%lld%lld",&n,&m,&k);
	maxn=n*m/(gcd(n,m));
	long long x,y;
	for(int i=0;i<k;i++)
	{
		scanf("%lld%lld",&x,&y);
		//cout<<c<<endl;
		//cout<<dx<<" "<<dy<<endl;
		long long ans=maxn+1;
		ans=min(ans,check(x,y,dx,dy));
		//cout<<ans<<"c"<<endl;
		ans=min(ans,check(-x,y,dx,dy));
		//cout<<ans<<"c"<<endl;
		ans=min(ans,check(x,-y,dx,dy));
		//cout<<ans<<"c"<<endl;
		ans=min(ans,check(-x,-y,dx,dy));
		//cout<<ans<<"c"<<endl;
		if(ans==maxn+1)
			printf("-1\n");
		else
			printf("%lld\n",ans);
	}
	//system("pause");
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值