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开始出发走 ...
随机推荐
- nginx中Geoip_module模块的使用
nginx中Geoip_module模块的使用 .安装模块,nginx也是通过yum安装 yum install nginx-module-geoip -y # 可以看到模块的链接库文件 [root@ ...
- 前端 ---client、offset、scroll系列
client.offset.scroll系列 1.client系列 代码如下: <!DOCTYPE html> <html> <head> <meta c ...
- 查看ubuntu版本信息
参考文章:https://blog.csdn.net/qq_27818541/article/details/75207986 版本信息lsb_release -a No LSB modules ar ...
- elasticsearch6.3.1 安装以及配置IK 使用
https://blog.csdn.net/whb3299065/article/details/80104323
- python之hashlib
简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法.在python3中已经废弃了md5和sha模块,简单说明 ...
- 用docker快速搭建wordpress博客
WordPress是一个非常著名的PHP编写的博客平台,发展到目前为止已经形成了一个庞大的网站平台系统.在WP上有规模庞大的插件和主题,可以帮助我们快速建立一个博客甚至网站. 在Windows上可 ...
- Confluence 6 后台中为站点添加应用导航
Confluence 6 后台中为站点添加应用导航的连界面和方法. https://www.cwiki.us/display/CONFLUENCEWIKI/Configuring+the+Site+H ...
- vue 树状图数据的循环 递归循环
在main.js中注册一个子组件 在父组件中引用 树状图的数据格式 绑定一个数据传入子组件,子组件props接收数据 子组件中循环调用组件,就实现了递归循环
- js操作数组元素
一, 删除数组指定的某个元素 首先可以给JS的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引,代码为: Array.prototype.indexOf = function(val) ...
- Django框架第一篇基础
一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...