我不告诉你这个链接是什么

分析:模拟可以过,但是好烦啊。。不会写。还有一个扩展欧几里得的方法,见下:

假设光线没有反射,而是对应的感应器镜面对称了一下的话



左下角红色的地方是原始的N∗M的方格,剩下的三个格子是镜面对称的结果。原来的点是(a,b)的话,剩下三个点从左上到右下分别是(a,2∗N−b),(2∗M−a,2∗N−b),(2∗M−a,b)。真实情况是一个点不止只有这三个镜像点,一个点可以在平面内生成无数个点,可以用坐标表示为(2Mk±a,2Nk±b)k为任意非负整数。

假设光线没有经过反射,那么用函数可以表示为y=x,这样要求包括镜像点在内的所有点x=y,于是就有二元一次方程2Mx±a=2Ny±b,可以改成四个二元一次方程式,用扩展欧几里得计算。

对于一个点,光线的真实情况是可能会经过多次,这意味着一个点以及它的镜像点可能有多个在y=x 上,这是就需要取最小的x坐标的点就行了。一个点也可能没有经过,扩展欧几里得无解就行了。还有需要注意的是如果光线经过了原来矩形框的三个角,那么光线就已经射出矩形框了,对于一些可以求出结果的点,还需要进行一下判断,真实情况下是否可行。

代码:

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31; /*****************************************************/ //对于不定整数方程pa+qb=c,若 c mod Gcd(a, b)=0,则该方程存在整数解,否则不存在整数解
//p = p1 + b/Gcd(a, b) * t
//q = q1 - a/Gcd(a, b) * t(其中t为任意整数,正负皆可)
LL exgcd(LL a,LL b,LL &x,LL &y) {
if(!b) {x=1; y=0; return a; }
LL r=exgcd(b,a%b,y,x);
y-=a/b*x;
return r;
} bool solve_equ(LL a,LL b,LL ca,LL cb,LL &x,LL &y,LL &d,LL &ans) {
LL c = ca + cb;
d = exgcd(a, b, x, y);
if(c % d) return false;
LL k = c / d; LL g = b / d;
if (g < 0) g = -g;
g *= a; x *= k; y *= k;
ans = a * x - cb;
ans = (ans % g + g) % g;
return true;
} int main(int argc, char const *argv[]) {
int N, M, K;
cin>>N>>M>>K;
LL over = INFF;
for (int i = 0; i < 3; i ++) {
LL a, b;
if (i == 0) a = N, b = M;
else if (i == 1) a = 0, b = M;
else a = N, b = 0;
LL x, y, d, res, ans = INFF;
for (int k = 0; k < 4; k ++) {
LL tmpa = a, tmpb = b;
if (k & 1) tmpa = -tmpa;
if ((k >> 1) & 1) tmpb = -tmpb;
if (solve_equ(2 * M, -2 * N, tmpa, tmpb, x, y, d, res))
ans = min(ans, res);
}
over = min(over, ans);
}
while (K --) {
LL a, b;
cin>>a>>b;
LL ans = INFF, x, y, d, res;
for (int k = 0; k < 4; k ++) {
LL tmpa = a, tmpb = b;
if (k & 1) tmpa = -tmpa;
if ((k >> 1) & 1) tmpb = -tmpb;
if (solve_equ(2 * M, -2 * N, tmpa, tmpb, x, y, d, res))
ans = min(ans, res);
}
if (ans == INFF || (over != INFF && ans > over)) puts("-1");
else cout<<ans<<endl;
}
return 0;
}

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 (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

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

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

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

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

  9. 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. iperf交叉编译:

    下载:http://sourceforge.net/projects/iperf 1. 先把iperf-2.0.4.tar.gz解压到你的目录下.2. cd iperf-2.0.43. ./confi ...

  2. Vmware玩mac os x分享

    相信网上类似的材料非常多了,在此就不赘述具体步骤了,先分享一个不错的链接: http://jingyan.baidu.com/article/ff411625b9011212e48237b4.html ...

  3. Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git小结(转)

    摘要 出于兴趣,想要搭建一个自己的小站点,目前正在积极的准备环境,利用Spring+SpringMVC+MyBatis+LogBack+C3P0+Maven+Git,这里总结下最近遇到的一些问题及解决 ...

  4. 某预约系统分析 > 某区公共自行车租车卡在线预约,关于如何提高成功概率

    概诉 网上提交预约申请单,线下面交完成实体卡的交付和办理. 本文主要从技术角度分析预约页面,仅供初学者技术交流使用. 表单输入和校验 系统通过2步的确认点击到达信息输入页面. 地址:/bjggzxc/ ...

  5. Overload和Override的区别

    Overload:重载 |- 在一个类中定义的若干方法 |- 所有的方法名相同,但参数类型或个数不同 |- 只有参数有关,与返回类型无关 Override:覆写 |- 在继承的关系中 |- 子类定义了 ...

  6. NiosII常用函数整理

    NiosII常用函数整理 IO操作函数函数原型:IORD(BASE, REGNUM) 输入参数:BASE为寄存器的基地址,REGNUM为寄存器的偏移量函数说明:从基地址为BASE的设备中读取寄存器中偏 ...

  7. JavaScript的chapterI

    JavaScript是一种由web浏览器进行解析和执行脚本语言,包括ECMAScript,DOM,BOM. JavaScript的最大作用是赋予页面动作,让页面动起来,已经动态的处理页面的数据. 应用 ...

  8. iOS解决NSData转NSString后字符为空

    iOS中,将NSData转NSString的一般方法为[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];但是当dat ...

  9. Sublime Text的常用插件

    工欲善其事,必先利其器.好的插件会更多的帮助我们装逼. 最新感悟:也不要装太多的插件.有些无用的插件可以删除,有时反而臃肿.bloger下载官方最新稳定版st3 3126下载下来25M左右.安装十来个 ...

  10. 前端中sprite.png的实现

    通过background-position来控制X轴和Y轴,从而实现显示sprite.png中想要的ico或图案. 如下图所示: