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

InputFirst line contains an integer T, which indicates the number of test cases.

Every test case contains two integers exex and eyey, 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 = LCM(x , y),就是x,y的最小公倍数。现在已知青蛙跳到了(ex,ey)
问青蛙可能的起点有多少个?从这样的起点跳若干次可以到达(ex , ey),可以一次不跳,要跳必须向右跳或者向上跳,步长为z。 【题解】:首先明白这样一个事实,假设能到达终点(ex,ey)的最左下角的起点是(x,y), 那么沿途的所有点(x',y')都是可以到达终点的点。 设GCD(ex , ey) = k, 由于每次跳跃的步长是原坐标中横坐标x和纵坐标y的LCM,即z,从(x,y)出发,不论新的坐标( x' , y')是(x+z,y) 还是 (x, y+z),GCD(x' , y')始终等于k
证明如下:不妨下一步跳到(x+z,y) 设gcd(x,y) = s ; x = ms, y = ns; 显然mn互质
下一步坐标(x',y;) gcd(x',y') = gcd(ms + (ms * ns) / s , ns) = gcd( m*(1+n) , n)
由于m和n互质, n+1 和 n也互质,所以m*(1+n) 和 n必然互质,所以gcd(x',y') = s所以沿途每一个点的坐标x,y的gcd都相等,等于什么呢,等于最后终点的gcd(ex,ey) = k,这是给定的 【递推】
正推不好推,可以从终点反推,递推公式 f(x , y) = f(x-z1, y) + f(x, y-z2),其中z1 = (x-z1) * y / k 即z1 = x*y / (k+y)
同理 z2 = x*y / (k+x) 【代码】f函数可以写成循环的,若写成递归的,更新x,y坐标的操作一定要写在递归函数的参数里,不能在此之前做,否则会WA,原因是回溯问题
#include<iostream>
using namespace std; int gcd(int a, int b){
if(b == ) return a;
else return gcd(b,a%b);
}
int k;
long long ans = ;
void f(long long x, long long y){ if(x == y){
return ;
} //其实要往右走x > y是必须的,自己可以稍微证明 ,加一个这个条件可以快一点不加也行
long long z1 = (x*y) / (k+y);
//注意保持每一步GCD都是K
if( x>y && (x*y) % (k+y) == && gcd(x-z1 , y) == k){ ans++;
//cout<<"往右边走 x = "<<x<<" y = "<<y<<endl;
f(x - z1,y);
      //写成x = x - z1 然后 f(x,y)会WA
}
long long z2 = (x*y) / (k+x);
if(y > x && (x*y) % (k+x) == && gcd(x , y-z2) == k){
ans++;
//cout<<"往上边走 x = "<<x<<" y = "<<y<<endl;
f(x,y - z2);
} } int main(){
int ex,ey;
int t;
cin>>t;
int cas=;
while(t--){
ans = ;
cin>>ex>>ey;
k = gcd(ex,ey);
f(ex,ey);
cout<<"Case #"<<cas++<<": ";
cout<<ans+<<endl;
}
return ; }
 

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

  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(数学题)

    题目链接: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(数学推导公式,规律)

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

  5. hdu 5584 LCM Walk

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

  6. HDU5584 LCM Walk 数论

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

  7. hdu-5584 LCM Walk(数论)

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

  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采用正则表达式获取地址栏参数

    getQueryString:function(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*) ...

  2. metasploit-shellcode生成

    0x00 安装metasploit $ curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/t ...

  3. applicationContext.xml重要配置

    <!-- 加载 hibernate.properties 文件--> <bean id="propertyConfig" class="org.spri ...

  4. PHP 腾讯云cos使用之我见

    因为某些人的原因,本文从新改名发布一遍. 原名称:tp5 -- 腾讯云cos简单使用 原文链接:https://www.cnblogs.com/YFYQ/p/10840050.html 因项目需要,本 ...

  5. 相机 感光度iso,焦距,光圈,ccd 和 噪点, 景深关系表格

    表格 矩阵 感官度iso: 越低曝光速度越慢,所谓慢工出细活,成像质量会好,如果形成的话. 但是因为慢,所以要更多的光量,才能画完. 就需要更慢的快门 (但是太慢手抖的话就糊掉,或者动的物体形成轨迹. ...

  6. iMessage, Facetime 解决办法

    不需要白苹果三码,亲测可用:原帖地址: https://www.reddit.com/r/hackintosh/comments/2wohwn/getting_imessage_working_on_ ...

  7. js转换金钱为中文单位元、万元、亿元、万亿

    function unitConvert(num) { var moneyUnits = ["元", "万元", "亿元", "万 ...

  8. python多进程与多线程编程

    进程(process)和线程(thread)是非常抽象的概念.多线程与多进程编程对于代码的并发执行,提升代码运行效率和缩短运行时间至关重要.下面介绍一下python的multiprocess和thre ...

  9. python爬虫入门七:pymysql库

    我们使用python爬取得到的数据,有时候会数据量特别大,需要存入数据库. 需要注意的是,MySQL是一种关系型数据库管理系统,利用MySQL可以对数据库进行操作,而MySQL并不是一个数据库. 而p ...

  10. uboot的Makefile裁剪(针对飞思卡尔的mx6系列)

    VERSION = 2009PATCHLEVEL = 08SUBLEVEL =EXTRAVERSION =ifneq "$(SUBLEVEL)" ""U_BOO ...