HDU 5584 LCM Walk 数学

时间:2022-11-06 16:32:32

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&lpar;数论&rpar;

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

随机推荐

  1. BZOJ 2820&colon; YY的GCD &lbrack;莫比乌斯反演&rsqb;【学习笔记】

    2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discu ...

  2. MySQL创建一个具有root权限的用户

    grant all privileges on *.* to 'user'@'host' identified by 'password' WITH GRANT OPTION MAX_QUERIES_ ...

  3. Sql Server 保留几位小数的两种做法

    数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 1. 使用 Round() 函数,如 Round(@num,2)  参数 2 ...

  4. 【WCF 1】WCF框架宏观了解

    导读:使用WCF框架爱开发项目也有很长一段时间了,最开始的时候,是理解的不深,所以不写博客进行总结.后来是项目赶,发现需要总结的有很多,一直没有把WCF排上日程,尤其是最近研究EF这一块,更是研究了一 ...

  5. 应用程序连接oracle rac

    10g rac vip漂移的目的不是让client不加改变的连接过来而是让client尽快收到一个连接终止的包,然后由client去连接tnsnames中配置的下一个地址.和没有vip相比, 使用vi ...

  6. C&num;制作ActiveX控件及部署升级(摘自网络)

    使用C#开发ActiveX控件 控件开发.制作CAB包.签名.部署 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序 ...

  7. 雅礼 noip2018 模拟赛 day3 T3

    典型树形dp 这里,我们应该看到一些基本性质: ①:如果这个边不能改(不是没有必要改),我们就不改,因为就算改过去还要改回来,显然不是最优的 注意:"不能改"是指边的性质和要求的相 ...

  8. LINUX内核分析第五周学习总结——扒开系统调用的&OpenCurlyDoubleQuote;三层皮”(下)

    LINUX内核分析第五周学习总结--扒开系统调用的"三层皮"(下) 标签(空格分隔): 20135321余佳源 余佳源 原创作品转载请注明出处 <Linux内核分析>M ...

  9. ORACLE SQL Developer日期显示格式不全

    如下图,只有月-日-年,不能完整显示小时,分,秒 解决方法: 工具->首选项->数据库->NLS->日期格式: DD-MON-RR 修改为: YYYY-MM-DD HH24:M ...

  10. bzoj3932 任务查询系统

    Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分. 超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...