LCM Walk

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5584

Description

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)!

Input

First 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.

Output

For 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

HINT

题意

给你(x,y) 然后可以走到(x+lcm(x,y),y)或者走到(x,y+lcm(x,y))

然后现在给你一个位置,问你起点有多少种。

题解:

假设x = pt,y =qt

所以(pt,qt),那么下一步就可以走到(pt(1+q),qt)或者走到(pt,(1+p)qt)

那么很显然我们走到了(x,y) 那么上一步就是 (x/(y+1),y)和(x,y/(x+1))

代码:

#include<iostream>
#include<stdio.h>
using namespace std; int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
int ans = ;
void solve(int x,int y)
{
ans++;
if(x<y)swap(x,y);
if(x%(y+)==)solve(x/(y+),y);
}
int main()
{
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
ans = ;
int x,y;
scanf("%d%d",&x,&y);
int p = gcd(x,y);
x = x/p,y = y/p;
solve(x,y);
printf("Case #%d: %d\n",cas,ans);
}
}

HDU 5584 LCM Walk 数学的更多相关文章

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

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

  2. HDU 5584 LCM Walk(数学题)

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

  3. HDU 5584 LCM Walk【搜索】

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

  4. hdu 5584 LCM Walk

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

  5. 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 ...

  6. HDU 5844 LCM Walk(数学逆推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...

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

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

  8. HDU5584 LCM Walk 数论

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

  9. hdu-5584 LCM Walk(数论)

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

随机推荐

  1. 字符串中符号的替换---replace的用法

    #include<iostream> #include<string> using namespace std; int main() { string s1 = " ...

  2. nodejs ctrl+B 快捷键设置

    开发 nodejs 的时候,要使用 sublime 软件,其中的启动方式有很多中.可以通过 supersivor 当然也可以 利用nodejs 的自身功能通过 ctrl+B来直接启动. 具体设置方式, ...

  3. [转]一些实用的图表Chart制作工具

    最近工作过程中需要用到前端一些JS框架,看到一篇博文就转过来备份使用,后续会再完善一些材料.   Flot   Flot一个纯javascript绘画库,基于jQuery开发.它能够在客户端根据任何数 ...

  4. ubuntu ip

    一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6 sudo vi /etc/modprobe.d/blacklist.conf 在文档最后添加 blac ...

  5. AudioManager --- generateAudioSessionId

    AudioManager中的generateAudioSessionId方法介绍: 1.方法声明 pubilc void generateAudioSessionId(); 2.API描述 返回一个不 ...

  6. Python 最佳实践

    前言 对我来说,以前每次面试是我审视自己,检验自己的一种方式.每次准备面试,以及被面试官问住的时候才会发现,其实我python我学的还不够好.工作中也是,可以从其他的同事那里获得成长.但是我今天说的是 ...

  7. navicat 或者workbench 无法连接127.0.0.1(61)的解决方法

    1.输入mysql -uroot 进入命令行模式, 2.输入"show variables like '%sock%';"查看sock文件所在位置 如: 3.配置客户端(以navi ...

  8. windows程序移植linux

    1,路径名统一用正斜杠“/”.(windows下正反斜杠都识别,linux只认正斜杠.) 2,统一使用UTF-8格式编码. vim中无法保存汉字时,可输入下列命令: :set fileencoding ...

  9. 查看MySql中每个IP的连接数

    要统计数据库的连接数,我们通常情况下是统计总数,没有细分到每个IP上.现在要监控每个IP的连接数,实现方式如下: ) as ip , count(*) from information_schema. ...

  10. Clean Code第三章<函数>

    1.方法不要写太长,如果太长,抽取其中的逻辑到新的方法中 bad good 2.函数只做一件事 如果做了多件事,要在方法名里体现出来 3.每个函数一个抽象层级 4.函数名可以长一些,比长注释好 5.方 ...