C. Ray Tracing

C. Ray Tracing
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.

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

这个题比较好玩
把这个矩形对称展开然后把所有的按照直线画出来最后把所有的链接到一条线上我们会发现最后到达最终点的x值是n和m的最小公倍数maxx=mn/gcd(m,n)
然后 所有点的坐标就是(2kn±x,2sm±y),) 然后用扩展欧几里得求出最小的k就可以 2kn±x不超出maxx值就是那2kn±x就是最少时间。 画个图理解一下

这里写图片描述

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
LL x,y;
LL EXGCD(LL a,LL b){
    if(b==0){
        x=1;y=0;
        return a;
    }
    LL r=EXGCD(b,a%b);
    LL t=x;
    x=y;
    y=t-a/b*y;
    return r;
}
int main(){
    LL n,m;
    int t;
    scanf("%lld %lld %d",&n,&m,&t);
    LL g=EXGCD(n,m);
    LL maxx=1LL*m/g*n;
    LL a=2*n; LL b=-2*m;
    LL G=EXGCD(a,b);
    while(t--){
        LL p,q;
        scanf("%lld %lld",&p,&q);
        LL xi[5];
        LL xt[5];
        xi[1]=p+q;  xi[2]=p-q; xi[3]=-p+q; xi[4]=-p-q;
        xt[1]=-p;  xt[2]=-p; xt[3]=p; xt[4]=p;
        if((xi[1]%G)&&(xi[2]%G)&&(xi[3]%G)&&(xi[4]%G))
            printf("-1\n");
        else{
            LL tx=maxx+1;
            for(int i=1;i<=4;i++){
 //               printf("%lld======%lld\n",x,G);
                if(xi[i]%G!=0)
                    continue;
                LL ran=b/G;
                LL ans=x*xi[i]/G;
                if(ran<0) ran=-ran;
                ans=(ans%ran+ran)%ran;
 //               printf("%lld___%lld____%lld___%lld__%lld\n",x,b,xi[i],ran,G);
                if(2*ans*n+xt[i]<=0)
                    tx=tx;
                else
                    tx=min(tx,2*ans*n+xt[i]);
                //printf("%lld====%lld\n",2*ans*n+xt[i],xt[i]);
            }
            if(tx<maxx&&tx>0)
                printf("%lld\n",tx);
            else
                printf("-1\n");
        }
    }
    return 0;
}
射线追踪法是一种计算机图形学中常用的算法,用于生成逼真的图像。直达波ray tracing 试射法是射线追踪算法的一种应用,可以模拟光线直接从光源射到物体表面上的效果。以下是一个简单的C源代码示例: ```c #include <stdio.h> typedef struct { float x; float y; float z; } Vector3; typedef struct { Vector3 origin; Vector3 direction; } Ray; typedef struct { Vector3 position; Vector3 normal; } Plane; int intersectRayPlane(Ray ray, Plane plane, float* t) { float denominator = plane.normal.x * ray.direction.x + plane.normal.y * ray.direction.y + plane.normal.z * ray.direction.z; if (denominator == 0) { return 0; } float numerator = plane.normal.x * (plane.position.x - ray.origin.x) + plane.normal.y * (plane.position.y - ray.origin.y) + plane.normal.z * (plane.position.z - ray.origin.z); *t = numerator / denominator; if (*t >= 0) { return 1; } return 0; } int main() { Ray ray; ray.origin.x = 0; ray.origin.y = 0; ray.origin.z = 0; ray.direction.x = 1; ray.direction.y = 0; ray.direction.z = 0; Plane plane; plane.position.x = 5; plane.position.y = 0; plane.position.z = 0; plane.normal.x = -1; plane.normal.y = 0; plane.normal.z = 0; float t; if (intersectRayPlane(ray, plane, &t)) { printf("The ray intersects the plane at t = %f\n", t); } else { printf("The ray does not intersect the plane\n"); } return 0; } ``` 以上代码示例定义了一个包含原点和方向的射线结构体`Ray`以及一个包含位置和法向量的平面结构体`Plane`。通过调用`intersectRayPlane`函数,可以判断射线和平面是否相交并计算相交点的参数`t`值。在示例中,射线的原点在坐标原点,方向为X轴正方向,平面的位置在X轴上5个单位处,法向量为X轴负方向。输出结果将显示射线是否与平面相交以及相交点的参数`t`值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值