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

3
6 10
6 8
2 8

Sample Output

Case #1: 1
Case #2: 2
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:

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int get_gcd(int x,int y)
{
if(!x)return y;
return get_gcd(y%x,x);
}
int main()
{
//freopen("input.txt","r",stdin);
int n,x,y;
scanf("%d",&n);
for(int i=; i<=n; ++i) {
int ans=;
scanf("%d%d",&x,&y);
int c=get_gcd(x,y);
x/=c,y/=c;
if(x>y)swap(x,y);
while(y%(x+)==) {
ans++;
y/=(x+);
if(x>y)swap(x,y);
}
printf("Case #%d: %d\n",i,++ans);
}
return ;
}

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. Protractor - 环境设置

    去年出于好奇搭建过一个Protractor+Cucumber的测试框架,当时项目上并没有用到AngularJS,所以框架能运行起来之后没有再深入了.最近新项目引入了AngularJS,想起去年搭的那个 ...

  2. Python - 部分PEP8规范

    写代码就像写字一样,为什么有的人写的字十分漂亮,而有的人写的字过后连自己都不认识,最主要还是从一开始是否对自己严格要求.从现在开始就当自己是个初学者,把代码写漂亮点.以下截取了部分PEP8代码规范,里 ...

  3. Newtonsoft.Json 指定某个属性使用特定的时间格式

    Newtonsoft.Json 指定某个属性使用特定的时间格式 Intro Newtonsoft.Json 是 .NET 下最受欢迎 JSON 操作库,原为 JSON.Net 后改名为 Newtons ...

  4. Linux下正确修改Docker镜像和容器的默认存储位置,亲测有效

    我们通过 yum 的方式安装完Docker环境后,它默认的存储位置是 /var/lib/docker,默认的 pid 存放位置是 /var/run/docker.pid. 如果仅仅是做测试,我们可能没 ...

  5. Python使用APScheduler实现定时任务

    APScheduler是基于Quartz的一个Python定时任务框架.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.在线文档:https://apscheduler. ...

  6. golang 的几个入门资料

    =====================视频=====================无闻 老师的<Go 编程基础>视频https://github.com/Unknwon/go-fun ...

  7. 重启电脑 wamp图标是橙色(未变绿)

    记录一个错误: 修复系统漏洞后,重启电脑,wamp没有开机自启动,手动启动后发现,图标是大红色变成了橙色,也就是服务未完全启动(1/2)状态. ??? 但是我其实也不知道是哪个服务(Apache/My ...

  8. java:数据结构(四)二叉查找树以及树的三种遍历

    @TOC 二叉树模型 二叉树是树的一种应用,一个节点可以有两个孩子:左孩子,右孩子,并且除了根节点以外每个节点都有一个父节点.当然这种简单的二叉树不能解决让树保持平衡状态,例如你一直往树的左边添加元素 ...

  9. ansible错误ImportError: No module named ansible.runner记录

    按着官网提供的安装ansible To configure the PPA on your machine and install ansible run these commands: $ sudo ...

  10. C++ - 结构体构造函数使用总结

    关于结构体构造函数使用总结 三种结构体初始化方法 1.利用结构体自带的默认构造函数 2.利用带参数的构造函数 3.利用默认无参的构造函数 要点: 在建立结构体数组时,如果只写了带参数的构造函数将会出现 ...