Billiard

枚举终点, 对于每一个终点一共有四种周期的相遇方式, 枚举一下取最小的时间。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} LL x, y;
LL exgcd(LL a, LL b, LL &x, LL &y) {
if(!b) {
x = ; y = ;
return a;
} else {
LL gcd, t; gcd = exgcd(b, a % b, x, y);
t = x; x = y; y = t - (a / b) * y;
return gcd;
}
} LL n, m, sx, sy, vx, vy; LL gao(LL a, LL b, LL t1, LL t2) {
LL c = t2 - t1;
LL gcd = exgcd(a, b, x, y);
if(c % gcd) return INF;
x *= c / gcd; y *= c / gcd;
LL mo = abs(b / gcd);
x = (x % mo + mo) % mo;
return * n * x + t1;
} LL calc(LL sx, LL sy, LL vx, LL vy, LL tx, LL ty) {
LL t11 = , t12 = ;
LL t21 = , t22 = ;
if(vx > ) {
if(tx >= sx) t11 = tx - sx, t12 = t11 + * (n - tx);
else t11 = sx - tx + * (n - sx), t12 = t11 + * tx;
} else {
if(tx <= sx) t11 = sx - tx, t12 = t11 + * tx;
else t11 = tx - sx + * sx, t12 = t11 + * (n - tx);
}
if(vy > ) {
if(ty >= sy) t21 = ty - sy, t22 = t21 + * (m - ty);
else t21 = sy - ty + * (m - sy), t22 = t21 + * ty;
} else {
if(ty <= sy) t21 = sy - ty, t22 = t21 + * ty;
else t21 = ty - sy + * sy, t22 = t21 + * (m - ty);
}
LL tim = INF;
chkmin(tim, gao( * n, - * m, t11, t21));
chkmin(tim, gao( * n, - * m, t11, t22));
chkmin(tim, gao( * n, - * m, t12, t21));
chkmin(tim, gao( * n, - * m, t12, t22));
return tim;
} void print(int who) {
if(who == ) cout << "0 0" << "\n";
else if(who == ) cout << n << " " << "0\n";
else if(who == ) cout << "0 " << m << "\n";
else if(who == ) cout << n << " " << m << "\n";
else cout << "-1" << "\n";
exit();
} int main() {
cin >> n >> m >> sx >> sy >> vx >> vy;
if(!vx) {
if(sx == && vy == ) print();
else if(sx == && vy == -) print();
else if(sx == n && vy == ) print();
else if(sx == n && vy == -) print();
else print(-);
} else if(!vy) {
if(sy == && vx == ) print();
else if(sy == && vx == -) print();
else if(sy == m && vx == ) print();
else if(sy == m && vx == -) print();
else print(-);
} else {
LL tim = INF, who = -;
if(chkmin(tim, calc(sx, sy, vx, vy, , ))) who = ;
if(chkmin(tim, calc(sx, sy, vx, vy, n, ))) who = ;
if(chkmin(tim, calc(sx, sy, vx, vy, , m))) who = ;
if(chkmin(tim, calc(sx, sy, vx, vy, n, m))) who = ;
print(who);
}
return ;
} /*
*/

Codeforces 982E Billiard exgcd的更多相关文章

  1. Codeforces 982E Billiard 扩展欧几里德

    原文链接http://www.cnblogs.com/zhouzhendong/p/9055728.html 题目传送门 - Codeforces 928E 题意 一束与坐标轴平行或者成$45^\ci ...

  2. Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂

    https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...

  3. Codeforces Codeforces Round #484 (Div. 2) E. Billiard

    Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/proble ...

  4. [ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]

    https://codeforces.com/contest/1143/problem/D D. The Beatles time limit per test 1 second memory lim ...

  5. Codeforces 1106F Lunar New Year and a Recursive Sequence | BSGS/exgcd/矩阵乘法

    我诈尸啦! 高三退役选手好不容易抛弃天利和金考卷打场CF,结果打得和shi一样--还因为queue太长而unrated了!一个学期不敲代码实在是忘干净了-- 没分该没分,考题还是要订正的 =v= 欢迎 ...

  6. Codeforces Beta Round #7 C. Line Exgcd

    C. Line 题目连接: http://www.codeforces.com/contest/7/problem/C Description A line on the plane is descr ...

  7. Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)

    补一篇以前的扩展欧几里得的题,发现以前写错了竟然也过了,可能数据水??? 这个题还是很有意思的,和队友吵了两天,一边吵一边发现问题??? L. Knights without Fear and Rep ...

  8. codeforces 492E. Vanya and Field(exgcd求逆元)

    题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...

  9. Codeforces 772C 构造 数学 + dp + exgcd

    首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走 ...

随机推荐

  1. Android动画学习(二)——Tween Animation

    前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...

  2. Ubuntu14.04安装wineqq国际版

       一开始,我在Ubuntu14.04下安装的QQ版本是WineQQ2013SP6-20140102-Longene, 但后来发现这个版本QQ在linux下问题很多,比如不能用键盘输入密码,QQ表情 ...

  3. Noip2016提高组 玩具谜题toy

    Day 1 T1 题目大意 一些naive的玩具小人把小南的眼镜藏起来,但小南有一份too simple的小纸条,告诉小南眼镜在第一个小人往哪数第几个的往哪数的第几个的往哪数第几个的往哪数的第几个的往 ...

  4. POJ 2318 TOYS &amp;&amp; POJ 2398 Toy Storage(几何)

    2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...

  5. JAVA笔记1-00

    package chapter1; public class Demo1 { public static void main(String[] args) { System.out.println(& ...

  6. Redis的安装和配置

    在网站redis.io复制下载链接 wget http://download.redis.io/releases/redis-3.0.5.tar.gz tar zxvf redis-3.0.5.tar ...

  7. HDU 2612 Find a way BFS,防止超时是关键

    之前我写的时候是:每找到一个‘@’就广搜一次,如果这样写有多少个‘@’就会广搜几次,这样就超时了.我队友告诉我应该打个表,这个方法确实不错.因为'Y'和'M'是唯一的,我通过这两个点分别广搜一次,对所 ...

  8. 14. leetcode 383. Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  9. [SDOI2015]约数个数和

    Sol 首先有个结论 \(\sum_{i=1}^{m}\sum_{j=1}^{n}d(i*j)=\sum_{i=1}^{m}\sum_{j=1}^{n}\sum_{x|i}\sum_{y|i}[gcd ...

  10. leetcode146

    public class LRUCache { ; ; long sernumbers; long SerNumbers { get { if (sernumbers <= long.MaxVa ...