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. Vue的安装并在WebStorm中运行

    一.Vue的安装需要两个支持分别为:nodejs.npm Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O ...

  2. jquery动态实现填充下拉框

    当点下拉框时动态加载后台数据. 后台代码 protected void doPost(HttpServletRequest request, HttpServletResponse response) ...

  3. 基于Passthru的NDIS开发的个人理解

    这几天对NDIS的学习,基本思路是:首先熟悉理论知识→然后下载一个例子进行研究→最后例子自己模仿扩展→最最后尝试自己写一个新的. Passthru是微软NDIS自己写的一个框架驱动,NDIS开发者可以 ...

  4. Greenplum/Deepgreen(集群/分布式)安装文档

    Deepgreen分布式安装文档 环境准备 1.安装VMware虚拟机软件,然后在VMware安装三台Linux虚拟机(使用centos7版本) 2.使用的虚拟机如下: 192.168.136.155 ...

  5. 洛谷 P2846 光开关

    https://www.luogu.org/problemnew/show/P2846 好多题解用线段树来写,然而分块不是更简单好些吗? 一个数组use记录这一块进行了多少次开关操作,两边单独计算,注 ...

  6. [LUOGU] 3959 宝藏

    https://www.luogu.org/problemnew/show/P3959 注意到n非常小,考虑状压/搜索. 发现状压需要枚举起点,跑n次,一个问题是转移不可以以数字大小为阶段了,考虑用d ...

  7. verilog behaviral modeling -- procedural timing contronls

    1.delay control : an expression specifies the time duration between initially encountering the state ...

  8. PHP开发中涉及到emoji表情的几种处理方法!

    emoji表情 处理 一般Mysql表设计时,都是用UTF8字符集的.把带有emoji的昵称字段往里面insert一下就没了,整个字段变成了空字符串.这是怎么回事呢? 原来是因为Mysql的utf8字 ...

  9. centos中python2替换为python3,并解决yum出错

    这里采用安装python3.6版本. 安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel r ...

  10. React中css的使用

    网页的布局.颜色.形状等UI展示方式主要是由Css进行设置,在ReactJs中也是一样.ReactJs中的Css结构方式与传统的Web网页类似,但依然存在一些差异.ReactJs中Css文件本身的编写 ...