Codeforces 982E Billiard exgcd
枚举终点, 对于每一个终点一共有四种周期的相遇方式, 枚举一下取最小的时间。
#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的更多相关文章
- Codeforces 982E Billiard 扩展欧几里德
原文链接http://www.cnblogs.com/zhouzhendong/p/9055728.html 题目传送门 - Codeforces 928E 题意 一束与坐标轴平行或者成$45^\ci ...
- 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} ...
- Codeforces Codeforces Round #484 (Div. 2) E. Billiard
Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/proble ...
- [ 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 ...
- Codeforces 1106F Lunar New Year and a Recursive Sequence | BSGS/exgcd/矩阵乘法
我诈尸啦! 高三退役选手好不容易抛弃天利和金考卷打场CF,结果打得和shi一样--还因为queue太长而unrated了!一个学期不敲代码实在是忘干净了-- 没分该没分,考题还是要订正的 =v= 欢迎 ...
- 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 ...
- Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)
补一篇以前的扩展欧几里得的题,发现以前写错了竟然也过了,可能数据水??? 这个题还是很有意思的,和队友吵了两天,一边吵一边发现问题??? L. Knights without Fear and Rep ...
- codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...
- Codeforces 772C 构造 数学 + dp + exgcd
首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走 ...
随机推荐
- T-SQL 日期函数
GETDATE() 它将返回当前日期和时间. 语法 上述函数的语法: GETDATE() 例 以下查询将返回当前日期以及MS SQL Server中的时间. Select getdate() as c ...
- Linux中给普通用户添加sudo权限
使用Linux系统时,经常会被要求使用超级权限,但是root的权限太过大了,一般慎用!!!因此可以通过给普通用户添加sudo权限,平常用普通用户进行操作,当需要root权限的时候进行sudo操作.以下 ...
- 使用Eclipse进行Makefile项目
最近在MCU on Eclipse网站上看到Erich Styger所写的一篇有关在Eclipse中使用Makefile创建项目的文章,文章讲解清晰明了非常不错,所以呢没人将其翻译过来供各位同仁参考. ...
- 最短路径之Bellman-Ford算法
第一行为源点个数,边的个数m 接下来m行为a->b和权值 5 52 3 21 2 -31 5 54 5 23 4 3 Bellman-Ford算法(1): #include<iostrea ...
- select下拉框的数据回显
需求描述:select框,下拉后又很多的选项,选择一个,根绝后台代码做查询,完成之后,页面上的select框还是之前选的那个值 解决思路:select本质就是 value和text一一对应,根据你的s ...
- 【深度学习】吴恩达网易公开课练习(class1 week3)
知识点梳理 python工具使用: sklearn: 数据挖掘,数据分析工具,内置logistic回归 matplotlib: 做图工具,可绘制等高线等 绘制散点图: plt.scatter(X[0, ...
- 前端基础之初识HTML
一.web服务的本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.b ...
- laravel 关联查询
- laravel 中with关联查询限定查询字段
学习了下laravel5.6框架,果然很优雅,比如ActiveJieSuan model中作如下关联:(laravel模型关联关系可以查看https://laravelacademy.org/post ...
- bzoj 1812
什么鬼noip互测题... 这题很显然是树形dp,但设计状态以及转移是个难点 记状态f[i][j][k]表示以i为根节点的子树,离i最近的祖宗节点编号为j放了虫洞(伐木场?),i的子树内放了k个伐木场 ...