题目链接: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. MS SQL 分类汇总参数 grouping(**)=1 rollup cubt

    转:http://www.111cn.net/database/mssqlserver/43368.htm 本文章介绍了关于sql多级分类汇总实现方法及数据结构,有碰到问题的同学可参考一下. 据库结构 ...

  2. PHP-Manual的学习----【序言】

    2017年6月27日16:57:32 学习资料:2015-PHP-Manual 打好坚实的基础是做任何事的前提 序言: 笔记: 1.PHP,即"PHP: Hypertext Preproce ...

  3. Manager模块 队列 管道 进程池

    Manager模块 作用:  多进程共享变量. Manager的字典类型: 如果value是简单类型,比如int,可以直接赋值给共享变量,并可以后续直接修改 如果value是复杂类型 ,比如list, ...

  4. jquery 使用笔记

    一下是在做项目中用到jquery涉及到的一些知识点,把源码复制过来,省得以后忘记了: <link href="<%=request.getContextPath()%>/c ...

  5. Nginx结合GeoIP库

    1. 编译nginx时带上geoip模块 # wget http://nginx.org/download/nginx-x.x.x.tar.gz # tar zxvf nginx-x.x.x.tar. ...

  6. OLTP和OLAP

    1 OLTP和OLAP online transaction processing,联机事务处理.业务类系统主要供基层人员使用,进行一线业务操作,通常被称为联机事务处理. online analyti ...

  7. iOS main函数讲解

    int main(int argc, char * argv[]) { @autoreleasepool { //四个参数 主要讲解后面两个参数 /* 第三个参数:UIApplication或者其子类 ...

  8. Python: generator, yield, yield from 详解

    1.Generator Expressions 生成器表达式是用小括号表示的简单生成器标记法: generator_expression ::= "(" expression co ...

  9. 改善程序与设计的55个具体做法 day6

    条款13:以对象管理资源 资源,包括但不限于内存.句柄.GDI对象.数据库连接等. 内存要记得释放,句柄要记得closehandle, GDI对象要记得删除,数据库连接要记得关闭,等等等等. 以对象来 ...

  10. Android AbsoluteLayout绝对布局

    绝对布局也叫坐标布局,指定元素的绝对位置,因为适应性很差,一般很少用到.可以使用RelativeLayout替代. 常用属性: android:layout_x --------组件x坐标 andro ...