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 ; }
 

最新文章

  1. ElasticSearch与Spring Boot集成问题
  2. windows字符串
  3. 【Swift学习】Swift编程之旅(二)
  4. [No000066]python各种类型转换-int,str,char,float,ord,hex,oct等
  5. ThinkPHP的RBAC
  6. 团队开发——冲刺1.g
  7. Java学习笔记之_JDBC
  8. Some Useful Property Settings Explained Of Oracle Forms
  9. UVA 11997 STL 优先队列
  10. java一个简单的管理系统
  11. jQuery 删除HTML元�
  12. 概率分布之间的距离度量以及python实现(四)
  13. trackerClient.getConnection()为null
  14. POST 400 的一次遭遇
  15. ElasticSearh更新nested字段(Array数组)。怎么根据查询条件(query)复制一个(index)到新的Index how to update by query a nested fields data for elasticsearch
  16. C# Http方式下载文件到本地类改进版
  17. GIT(3)----问题汇总
  18. ny37 回文字符串
  19. 在centos命令行下安装软件
  20. Hive 体系结构

热门文章

  1. Java习题附答案
  2. Python IDE推荐
  3. shell脚本调试打印日志问题
  4. 使用一位数组解决 1 1 2 3 5 8 13 数列问题 斐波纳契数列 Fibonacci
  5. postgresql+pgadmin3安装
  6. java获取本地计算机MAC地址
  7. Java测试技巧
  8. js 做的随机8位验证码
  9. Codeforces Round #439 (Div. 2) C. The Intriguing Obsession
  10. java 协程框架quasar gradle配置