cf 724C Ray Tracing(扩展欧几里得)



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)after 3 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).




因为反射考虑问题很复杂,就利用镜像把图形展开来考虑。。。很复杂。。。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
const int N = 25;
typedef long long LL;
LL n, m;
LL e_gcd(LL a,LL b, LL &x,LL &y)
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    LL ans=e_gcd(b,a%b,x,y);
    LL tmp = x;
    x=y;
    y=tmp - a/b*y;
    return ans;
}


LL ex(LL a,LL b, LL c, LL &x,LL &y)
{
    LL g = e_gcd(a,b,x,y);
    if(c%g)
        return -1;
    LL mod = b/g;
    x*=(c/g);
    if(mod<0)
        mod=-mod;
    x=(x%mod+mod)%mod;
    return x;
}


LL mint(LL a,LL b)
{
    return a>b?b:a;
}


LL cal(LL a, LL b, LL c)
{
    LL x, y;
    if(ex(2*n,-2*m,-a+b,x,y)==-1)
        return c+1;
    LL tx=2*x*n+a;
    if(tx<0||tx>c)
       return c+1;
    return tx;
}


LL solve(LL a,LL b)
{
    LL gcd=__gcd(n,m);
    LL maxx=1LL*n/gcd*m;
    LL ans=maxx+1;
    ans=mint(ans,cal(a,b,maxx));
    ans=mint(ans,cal(a,-b,maxx));
    ans=mint(ans,cal(-a,b,maxx));
    ans=mint(ans,cal(-a,-b,maxx));
    if(ans==maxx+1)
        return -1;
    return ans;
}


int main()
{
    int k;
    while(scanf("%I64d %I64d %d", &n, &m, &k)!=EOF)
    {
        for(int i=0;i<k;i++)
        {
            LL a, b;
            scanf("%I64d %I64d",&a, &b);
            printf("%I64d\n",solve(a,b));
        }
    }
    return 0;
}



参考:

把矩形对称展开,最后小球在横纵坐标均为 maxx=mn/gcd(m,n) 处被吸收。 
原坐标为 (x,y) 的小球经过轴对称展开,其坐标为 (2kn±x,2sm±y),k,s .要使得在吸收前经过点,则坐标必须在线段(0, 0)到(maxx, mxx)之间。 
即要解方程 2kn±x=2sm±y ,求为正最小的 2kn±x 。利用扩展欧几里得解方程。

求ax+by=c最小正整数解,坑点:ran<0时不搞成正数会出错!

<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">LL equation(LL a, LL b, LL c, LL &x, LL &y)
{
    LL g = extend_Euclid(a, b, x, y);
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(c % g) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    LL ran = b / g;
    x <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*=</span> c/g;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(ran < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) ran = -ran; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//wa point</span>
    x = (x<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%ran</span> + ran) % ran;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

代码

<code class="language-cpp hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*--------------------------------------------
 * File Name: CF 724C
 * Author: Danliwoo
 * Mail: Danliwoo@outlook.com
 * Created Time: 2016-10-08 23:37:05
--------------------------------------------*/</span>

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <bits/stdc++.h></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>;
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define LL long long</span>
LL n, m;
LL extend_Euclid(LL a, LL b, LL &x, LL &y){
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(b==<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>){
        x = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; y = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> a;
    }
    LL r = extend_Euclid(b, a%b, y, x);
    y -= a/b*x;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> r;
}
LL equation(LL a, LL b, LL c, LL &x, LL &y)
{
    LL g = extend_Euclid(a, b, x, y);
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(c % g) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    LL ran = b / g;
    x *= c/g;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(ran < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) ran = -ran;
    x = (x%ran + ran) % ran;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}
LL gao(LL dx, LL dy, LL M) {
    LL k, s;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(equation(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>*n, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>*m, -dx+dy, k, s) == -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> M + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    LL tx = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> * k * n + dx;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(tx < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> || tx > M) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> M + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> tx;
}
LL minL(LL a, LL b) {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> a < b ? a : b;
}
LL solve(LL x, LL y) {
    LL g = __gcd(n, m);
    LL maxx = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1L</span>L * m / g * n;
    LL ans = maxx + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    ans = minL(ans, gao(-x, -y, maxx));
    ans = minL(ans, gao(-x, y, maxx));
    ans = minL(ans, gao(x, -y, maxx));
    ans = minL(ans, gao(x, y, maxx));
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(ans == maxx + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> ans;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main() {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> k;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(~<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">scanf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%I64d%I64d%d"</span>, &n, &m, &k)) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;i < k;i++) {
            LL x, y;
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">scanf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%I64d%I64d"</span>, &x, &y);
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%I64d\n"</span>, solve(x, y));
        }
    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值