題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5584
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
const int maxn =5e4+5;
const int mod=1e9+7;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
題目大意:一個人的起點在(x,y),
下一步可以走到(x+lcm(x,y),y),(x,y+lcm(x,y)).
現有最終位置(ex,ey)問有多少點可以到達ex,ey,
本身也算。
數論技巧題。首先可以觀察到,
公因數對答案不影響,
是以可以先把公因數處理掉,
考慮互質的兩個數,p,q,
其lcm為pq,是以下一步可以為(p,q+pq),(p+pq,q),
那麼對于終點給定的兩個點,就可以這樣輾轉下去求解答案了。
*/
int x,y;
int main()
{
int t;scanf("%d",&t);
for(int ca=1;ca<=t;ca++)
{
scanf("%d%d",&x,&y);
int cnt=1;
while(1)
{
if(x>y) swap(x,y);
int gd=gcd(x,y);
x/=gd,y/=gd;
if(y%(x+1)) break;
y/=(x+1),cnt++;
}
printf("Case #%d: %d\n",ca,cnt);
}
return 0;
}