题目链接: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. js读取json包装的map集合

    后台 Map<String,Integer> map = new HashMap<>(); map.put("你好1", 1); map.put(" ...

  2. 生成JNI的DLL时提示找不到jni.h的解决的方法Cannot open include file: &#39;jni.h&#39;: No such file or directory

    解决的方法: 就是到jdk的安装文件夹下include下把下面对应的文件,拷贝到vc文件夹下的include文件夹下 \jdk\include\jni.h \jdk\include\win32\jaw ...

  3. BlockingQueue的使用 http://www.cnblogs.com/liuling/p/2013-8-20-01.html

    BlockingQueue的使用 本例介绍一个特殊的队列:BlockingQueue,如果BlockQueue 是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到Blocki ...

  4. php单元测试标注(注解)

    @after 用于指明此方法应当在测试用例类中的每个测试方法运行完成之后调用. /** * @before1 */ public function bbb() { $this->assertTr ...

  5. 【BZOJ2422】Times 树状数组

    [BZOJ2422]Times Description 小y作为一名资深的dotaer,对视野的控制有着深刻的研究.每个单位在一段特定的时间内会出现在小y的视野内,除此之外的时间都在小y看不到的地方. ...

  6. PAT 1061. 判断题(15)

    判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过100的正整数N和M,分别是学生人数和判断题数量.第二行给出M个不超过5的正 ...

  7. python-安装 pip

    https://pip.pypa.io/en/stable/installing/ wget https://bootstrap.pypa.io/get-pip.py python get-pip.p ...

  8. PAT 天梯赛 L2-009. 抢红包 【排序】

    题目链接 https://www.patest.cn/contests/gplt/L2-009 思路 用结构体存储,然后结构体排序 注意一下 个人编号是从 1 开始 计数的 AC代码 #include ...

  9. 323 id与小数据池

    a = 1000b = 1000print(a == b)== 比较的是数值is 比较的是内存地址.print(a is b)查看内存地址id()print(id(a))print(id(b)) 小数 ...

  10. 全屏滚动插件之 fullpage.js

    前言:做移动端网页下滑/点击切换到下一页的效果,采用了fullpage,js最新的版本 https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/3.0.4 ...