A frog has just learned some number theory, and can't wait to show his ability to his girlfriend.

Now the frog is sitting on a grid map of infinite rows and columns. Rows are numbered 1,2,⋯ from the bottom, so are the columns. At first the frog is sitting at grid (sx,sy), and begins his journey.

To show his girlfriend his talents in math, he uses a special way of jump. If currently the frog is at the grid (x,y), first of all, he will find the minimum z that can be divided by both x and y, and jump exactly z steps to the up, or to the right. So the next possible grid will be (x+z,y), or (x,y+z).

After a finite number of steps (perhaps zero), he finally finishes at grid (ex,ey). However, he is too tired and he forgets the position of his starting grid!

It will be too stupid to check each grid one by one, so please tell
the frog the number of possible starting grids that can reach (ex,ey)

!

InputFirst line contains an integer T, which indicates the number of test cases.

Every test case contains two integers ex and ey, which is the destination grid.

⋅ 1≤T≤1000.

⋅ 1≤ex,ey≤109.OutputFor every test case, you should output "
Case #x: y", where x indicates the case number and counts from 1 and y is the number of possible starting grids.

Sample Input

  1. 3
  2. 6 10
  3. 6 8
  4. 2 8

Sample Output

  1. Case #1: 1
  2. Case #2: 2
  3. Case #3: 3

OJ-ID:
hdu-5584

author:
Caution_X

date of submission:
20191021

tags:
math

description modelling:
青蛙跳,每次移动从(x,y)->(x,y+lcm(x,y))或(x,y)->(x+lcm(x,y),y)

major steps to solve it:
设当前位置(at,bt),则下一步为(at(1+b),bt)或(at,bt(1+a))
那么反过来推,可以得到当前步(at,bt),则上一步为(at,bt/(a+1))或(at/(1+b),bt)
以此类推直到b无法被(1+a)整除或者a无法被(1+b)整除

AC code:

  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <cstdio>
  5. #include <cstring>
  6. using namespace std;
  7. int get_gcd(int x,int y)
  8. {
  9. if(!x)return y;
  10. return get_gcd(y%x,x);
  11. }
  12. int main()
  13. {
  14. //freopen("input.txt","r",stdin);
  15. int n,x,y;
  16. scanf("%d",&n);
  17. for(int i=; i<=n; ++i) {
  18. int ans=;
  19. scanf("%d%d",&x,&y);
  20. int c=get_gcd(x,y);
  21. x/=c,y/=c;
  22. if(x>y)swap(x,y);
  23. while(y%(x+)==) {
  24. ans++;
  25. y/=(x+);
  26. if(x>y)swap(x,y);
  27. }
  28. printf("Case #%d: %d\n",i,++ans);
  29. }
  30. return ;
  31. }

LCM Walk HDU - 5584的更多相关文章

  1. L - LCM Walk HDU - 5584 (数论)

    题目链接: L - LCM Walk HDU - 5584 题目大意:首先是T组测试样例,然后给你x和y,这个指的是终点.然后问你有多少个起点能走到这个x和y.每一次走的规则是(m1,m2)到(m1+ ...

  2. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

  3. HDU5584 LCM Walk 数论

    LCM Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  4. hdu-5584 LCM Walk(数论)

    题目链接:LCM Walk Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)To ...

  5. HDU 5584 LCM Walk(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意:(x, y)经过一次操作可以变成(x+z, y)或(x, y+z)现在给你个点(ex, e ...

  6. HDU 5584 LCM Walk【搜索】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 分析: 这题比赛的时候卡了很久,一直在用数论的方法解决. 其实从终点往前推就可以发现, ...

  7. hdu 5584 LCM Walk(数学推导公式,规律)

    Problem Description A frog has just learned some number theory, and can't wait to show his ability t ...

  8. hdu 5584 LCM Walk

    没用运用好式子...想想其实很简单,首先应该分析,由于每次加一个LCM是大于等于其中任何一个数的,那么我LCM加在哪个数上面,那个数就是会变成大的,这样想,我们就知道,每个(x,y)对应就一种情况. ...

  9. HDU - 5584 LCM Walk (数论 GCD)

    A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. No ...

随机推荐

  1. RST Methodology: “Responsible Tester”

    翻译另一篇James Bach的关于快速软件测试的文章,原文链接:http://www.satisfice.com/blog/archives/1364 在快速软件测试方法论中,我们区分出三种主要角色 ...

  2. Linux 下编写一个 PHP 扩展

        假设需求 开发一个叫做 helloWord 的扩展. 扩展里有一个函数,helloWord(). echo helloWord('Tom'); //返回:Hello World: Tom 本地 ...

  3. 多线程通信的两种方式? (可重入锁ReentrantLock和Object)

    (一)Java中线程协作的最常见的两种方式: (1)利用Object的wait().notify()和notifyAll()方法及synchronized (2)使用Condition.Reentra ...

  4. Python切片中的误区与高级用法

    众所周知,我们可以通过索引值(或称下标)来查找序列类型(如字符串.列表.元组...)中的单个元素,那么,如果要获取一个索引区间的元素该怎么办呢? 切片(slice)就是一种截取索引片段的技术,借助切片 ...

  5. JS是解释型还是编译型语言?

    解释型和编译型语言 解释型语言 解释型语言是对代码进行一句一句的直接运行,在程序运行期间,使用解释器动态将代码解释为机器码,再运行. 编译型语言 编译型语言是需要使用编译器先对代码进行编译为机器码,再 ...

  6. hadoop mapreduce求解有序TopN(高效模式)

    1.在map阶段对数据先求解改分片的topN,到reduce阶段再合并求解一次,求解过程利用TreeMap的排序特性,不用自己写算法. 2.样板数据,类似如下 1 13682846555 192.16 ...

  7. 模板引擎Jinja2的基本用法

    Flask提供的模板引擎为Jinja2,易于使用,功能强大.模板仅仅是文本文件,它可以生成任何基于文本的格式(HTML.XML.CSV.LaTex 等等). 它并没有特定的扩展名, .html 或 . ...

  8. bay——安装_RAC11g_LC_测试环境-rehat6+udev.txt

    ★★★____★☆★〓〓〓〓→VMware vSphere Client6.0 https://10.20.4.200/ 下载Vwmare IP:10.20.4.200-------账号:root-- ...

  9. python 指定字符串位置查找

    指定字符串位置查找 #指定字符查找 s = 'F:/my_pycharm/pycharm_project/CSV表格/10.csv' print(s.find('/')) # 2, 第一个/在2位置 ...

  10. Windows10下Git环境变量配置

    一.确定Git正确安装并能使用 右键项目目录,检查Git版本: 输入:  git --version 二. 配置环境变量 右键我的电脑 --> 属性 点击高级系统设置 --> 环境变量 - ...