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. MySQL插入SQL语句后在phpmyadmin中注释显示乱码

    自己写一个建一个简单的数据表,中间加了个注释,但是用PHPmyadmin打开后发现注释不对. 就先查询了一下sql 语句 发现SQL 语句并没有问题,感觉像是显示编码的问题,就先用set names ...

  2. CAP 可用性理解

    从容灾角度看可用性. 多机同时返回. 主通过 heart-beat 脑裂. 用 paxos. 性能远距离. 对整体压力较大. 从用户体验的角度看单数据可用性: 不考虑城市灾备的情况发生.只有单机房的 ...

  3. [LUOGU] P2245 星际导航

    题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边的带权无向图,顶点表示各个星系, ...

  4. database---many to many relationships(多对多关系型数据库)

    Many to many Relationships A many-to-many relationship occurs when multiple records in a table are a ...

  5. (4)zabbix监控第一台服务器

    2. zabbix监控服务器 创建主机,选择模板以及录入基本信息,过一分钟左右,就可以看到cpu.内存.硬盘等等使用情况.本节以图文为主.by the way, zabbix中文翻译很烂,config ...

  6. python学习第一天 计算机基础知识

    目录 什么是编程语言 什么是编程? 为什么要编程? 计算机5大组成分别有什么作用? qq启动的流程? 建议相关学习 课外 什么是编程语言 什么是编程语言? python和中文.英语一样,都是一门语言, ...

  7. raywenderlich.com Objective-C编码规范

    原文链接 : The official raywenderlich.com Objective-C style guide 原文作者 : raywenderlich.com Team 译文出自 : r ...

  8. u-boot顶层Makefile分析

    1.u-boot制作命令 make forlinx_nand_ram256_config: make all; 2.顶层mkconfig分析,参考 U-BOOT顶层目录mkconfig分析 mkcon ...

  9. xtu数据结构 C. Ultra-QuickSort

    C. Ultra-QuickSort Time Limit: 7000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java ...

  10. 学习iis工作原理

    文章:IIs工作原理 文章:Asp.Net 构架(Http Handler 介绍) - Part.2