题目链接:LCM Walk

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 491    Accepted Submission(s): 254

Problem 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
题意:(x,y)的下一步为(x+z,y)或(x,y+z),z为x和y的最小公倍数,问有多少个地方最终可以到达给的目的地(ex,ey)包括自身;
思路:lcm(x,y)=x*y/gcd(x,y)    gcd(ex,ey)=gcd(x+x*y/gcd(x,y),y)(y比x小的时候,不然x,y换一下也行)
   由于gcd(a,n)=gcd(a+k*n,n)所以gcd(x,y)=gcd(ex,ey);所以就可以求出x了,然后循环这些步骤就可以计算一共有多少个位置满足题意了,记得算出一个x要代回去验证是否成立;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ll gcd(ll a,ll b)
{
if(b==)return a;
return gcd(b,a%b);
}
int main()
{
int t,cnt=;
ll x,y;
scanf("%d",&t);
while(t--)
{
ll ans=;
cin>>x>>y;
while()
{
if(x<y)
{
ll fx=x;
x=y;
y=fx;
}
ll fy=y/gcd(x,y);
if(x%(fy+)!=)break;
else
{
ll ax=x/(fy+);
if(ax+ax*y/gcd(ax,y)!=x)break;
else ans++,x=ax;
}
}
cout<<"Case #"<<cnt<<": "<<ans<<"\n";
cnt++;
} return ;
}

hdu-5584 LCM Walk(数论)的更多相关文章

  1. HDU 5584 LCM Walk 数学

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

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

  3. HDU 5584 LCM Walk(数学题)

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

  4. HDU 5584 LCM Walk【搜索】

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

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

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

  6. hdu 5584 LCM Walk

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

  7. HDU5584 LCM Walk 数论

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

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

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

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

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

随机推荐

  1. 关于iphone自动播放音频和视频问题的解决办法

    大家都知道 做移动端 会遇到音频和视频无法自动播放问题(我也遇到了)于是想办法解决这个问题 我只是找到了在微信中解决的办法(如果谁有在别的浏览器有这个办法  请私聊我 )我是没有发现 document ...

  2. Linux删除乱码文件的方法

    当文件名为乱码的时候,无法通过键盘输入文件名,所以在终端下就不能直接利用rm,mv等命令管理文件了. 我们可以通过以下几种方法删除linux下的乱码文件.(文件名为乱码) l  方法1 我们知道每个文 ...

  3. web安全之SQL注入---第二章 什么是sql注入?

    如何理解SQL注入?SQL注入是一种将SQL代码添加到输入参数中传递到SQL服务器解析并执行的一种攻击方法总结:其实就是输入的参数没有进行过滤,直接参加sql语句的运算,达到不可预想的结果.SQL注入 ...

  4. hdu4847:Wow! Such Doge!(字符串匹配)

    题目:hdu4847:Wow! Such Doge! 题目大意:在给出的段落里面找出"doge"出现的次数.大写和小写都能够. 解题思路:字符串匹配问题,能够在之前将字母都转换成统 ...

  5. [JavaScript] Imitate String.Format() in c#

    Definition if (!String.prototype.format) { String.prototype.format = function () { var args = argume ...

  6. 开启貌似已经过时很久的新坑:SharePoint服务器端对象模型

    5年前(嗯,是5年前),SharePoint 2010刚发布的时候,曾经和kaneboy试图一起写一本关于SharePoint 2010开发的书,名字叫<SharePoint 2010 应用开发 ...

  7. images have the “stationarity” property, which implies that features that are useful in one region are also likely to be useful for other regions.

    Convolutional networks may include local or global pooling layers[clarification needed], which combi ...

  8. vue前戏ES6

    es6语法 es6语法:let和const: { var a=123; let b=234; } console.log(a); console.log(b); 浏览器里会只看到123; 而且还会抱一 ...

  9. GeekforGeeks Trie - 键树简单介绍 - 构造 插入 和 搜索

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  10. vim中使用sed去除网上copy的源代码行号和空格

    有些时候,在网上搜索到的代码都包含有行号,高亮显示控件不支持直接提取,如: test.sh 01 #!/bin/bash 02 echo “aaa” 简单的去掉行号和前面的空格: 方案一: 1.vim ...