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开始出发走 ...
随机推荐
- 使用Filezilla搭建FTP服务器
1.FTP over TLS is not enabled, users cannot securely http://blog.sina.com.cn/s/blog_4cd978f90102vtwl ...
- tcp和udp协议的聊天 和udp协议的时间同步机制-----编码
tcp协议聊天 服务端:: 客户端 udp协议的聊天 ############ udp协议 ########### 服务器 import socket sk = socket.socket(type ...
- 39)django-XSS 过滤
使用kingedit别人是可以输入script代码.这在后台是不允许script代码运行的. 这里主要使用beatifulSoup过滤 示例1 beatufulsoup4 from bs4 impor ...
- ElasticSearch搜索数据到底有几种方式?
Elasticsearch允许三种方式执行搜索请求: GET请求正文: curl -XGET "http://localhost:9200/app/users/_search" - ...
- 洛谷P4389 付公主的背包 [生成函数,NTT]
传送门 同样是回过头来发现不会做了,要加深一下记忆. 思路 只要听说过生成函数的人相信第一眼都可以想到生成函数. 所以我们要求 \[ ans=\prod \sum_n x^{nV}=\prod \fr ...
- 洛谷P5072 [Ynoi2015]盼君勿忘 [莫队]
传送门 辣鸡卡常题目浪费我一下午-- 思路 显然是一道莫队. 假设区间长度为\(len\),\(x\)的出现次数为\(k\),那么\(x\)的贡献就是\(x(2^{len-k}(2^k-1))\),即 ...
- hash·余数hash和一致性hash
网站的伸缩性架构中,分布式的设计是现在的基本应用. 在memcached的分布式架构中,key-value缓存的命中通常采用分布式的算法 一.余数Hash 简单的路由算法可以使用余数Hash: ...
- 'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件
今天安装完MYSQL8.0的版本,根据课本的提示,在CMD里运行,出现了'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件.在网上搜了一下,他的解决方法是这样的: 1.设置一下环境变量 ...
- Confluence 6 查看索引和提示
查看索引 Confluence 使用被称为 Lucene 的搜索引擎.如果你希望在你的 Confluence站点中查看更多有关索引的细节,你可以下载并且运行 Luke.Luke 是一个开发和诊断工具, ...
- 使用应用链接来连接 Jira 和 Confluence 6
请参考 Linking to Another Application 页面中的内容来设置如何让 Confluence 连接到你的 Jira 应用,这个过程只需要一次就可以了. 如果你计划使用 Jira ...