惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
J
Java Code Geeks
G
Google Developers Blog
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
Vercel News
Vercel News
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
Y
Y Combinator Blog
博客园_首页
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
A
About on SuperTechFans
B
Blog
Microsoft Security Blog
Microsoft Security Blog

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
hihocoder 1297.扩展欧几里得|OhYee 博客
2017-08-05 · via OhYee 博客

题目

{% fold 点击显/隐题目 %}

小Hi和小Ho周末在公园溜达。公园有一堆围成环形的石板,小Hi和小Ho分别站在不同的石板上。已知石板总共有m块,编号为 0..m-1,小Hi一开始站在s1号石板上,小Ho一开始站在s2号石板上。 小Hi:小Ho,你说我们俩如果从现在开始按照固定的间隔数同时同向移动,我们会不会在某个时间点站在同一块石板上呢? 小Ho:我觉得可能吧,你每次移动v1块,我移动v2块,我们看能不能遇上好了。 小Hi:好啊,那我们试试呗。 一个小时过去了,然而小Hi和小Ho还是没有一次站在同一块石板上。 小Ho:不行了,这样走下去不知道什么时候才汇合。小Hi,你有什么办法算算具体要多久才能汇合么? 小Hi:让我想想啊。。 提示:扩展欧几里德

第1行:每行5个整数s1,s2,v1,v2,m,0≤v1,v2≤m≤1,000,000,000。0≤s1,s2<m 中间过程可能很大,最好使用64位整型

第1行:每行1个整数,表示解,若该组数据无解则输出-1

{% endfold %}

题解

模板题

代码

{% fold 点击显/隐代码 %}```cpp 扩展欧几里得 https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份
#include
#include
using namespace std;

// gcd(a,b)
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

// ax + by = gcd(a,b)
long long ex_gcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1;
y = 0;
return a;
} else {
long long d = ex_gcd(b, a % b, y, x);
y -= x * (a / b);
return d;
}
}

// ax + by = c
bool solve(long long a, long long &x, long long b, long long &y, long long c,
long long minx) {
long long d = ex_gcd(a, b, x, y);
if (c % d)
return false;

long long m = b / d;
if (m < 0)
    m = -m;
x *= c / d;
x = (x % m + m) % m;
y = (c - a * x) / b;
return true;

}

int main() {
cin.tie(0);
cin.sync_with_stdio(false);
long long s1, s2, v1, v2, m;
while (cin >> s1 >> s2 >> v1 >> v2 >> m) {
long long x, y;
if (solve(v1 - v2, x, m, y, s2 - s1, 0))
cout << x << endl;
else
cout << "-1" << endl;
}
return 0;
}

{% endfold %}