HDU-5584 LCM Walk(GCD)
来源:互联网 发布:淘宝大衣比较好的店 编辑:程序博客网 时间:2024/06/05 14:23
题意:
给出ex,ey终点,询问起点有多少种情况。 如果当前在x,y, 则下一步可以走到x+ lcm(x,y) ,y 或者是 x , y+ lcm(x,y)
思路:
对于每个点 (x,y) 如果他是前一步走过来的,那必定是 x,y的较大者 在前一步加上了lcm 。那么如何知道我们前一步加的是多少?
假设gcd(x,y) = k 那么 x= nk, y= mk, 则lcm(x,y) =nmk,下一步必定是可以走到x+lcm ,y 或者是x, y+lcm ,又因为n 与m*(n+1)互质, m与n*(m+1)互质所以
下一步的gcd仍然为 k 不会改变。
这样很容易列出公式 y= x*y /(gcd(x,y) +x);
#include<cstdio>#include<map>#include<algorithm>#include<cstring>#include<queue>#include<cmath>using namespace std;typedef long long ll;const int N = 1e5 + 5;ll x, y;int main(){ int T; scanf("%d", &T); for(int kase = 1; kase <= T; kase++){ int ans = 1; scanf("%lld%lld", &x, &y); if(x > y) swap(x, y); while(x * y % (__gcd(x, y) + x) == 0){ ll p = x * y / (__gcd(x, y) + x); if(__gcd(x, y-p) != __gcd(x, y)) break; y -= p; if(x > y) swap(x, y); ans++; } printf("Case #%d: %d\n", kase, ans); } return 0;}
阅读全文
0 0
- HDU-5584 LCM Walk(GCD)
- HDU 5584 LCM Walk(number theory && GCD)
- HDU-5584 LCM Walk(GCD/LCM+找规律)
- hdu 5584 LCM Walk
- HDU 5584 LCM Walk
- HDU 5584 LCM Walk
- HDU 5584 LCM Walk
- hdu 5584 LCM Walk
- HDU-5584-LCM Walk
- HDU 5584 LCM Walk【搜索】
- HDU 5584 LCM Walk 数学
- HDU 5584 LCM Walk 数论
- hdu 5584 LCM Walk(规律)
- HDU-5584-LCM Walk(数学)
- HDU 5584 LCM Walk (数学推导)
- 【HDU 5584】 LCM Walk 【递推】
- LCM Walk HDU
- hdoj-5584-LCM Walk
- 网络通讯协议和分层
- 直方图匹配(规定化)
- 安装Linux win双系统 无法正常启动 读不出U盘
- cnetos6.5 sublime3 rpm 安装/字体设置/禁止弹出更新
- SGU 143. Long Live the Queen(树形DP)
- HDU-5584 LCM Walk(GCD)
- Http协议
- springboot 系统学习2 -- fastjson
- 内联函数inline ,宏与函数的区别
- socket编程接口函数简单介绍
- Intersection
- Java--day06构造方法
- 我的第一个安卓通讯
- HDU