题意:
一只青蛙,站在x,y点,每次跳跃,会跳到,(x+lcm(x,y),y) 或者(x,y+lcm(y)) 点。
现在给出终点,(
e
x
e_x
ex,
e
y
e_y
ey)求有多少个可能的起点。
思路:
乍一看好像每次都有两种可能,搜索起来复杂度很高。其实每次跳跃的方向都是固定的。
因为lcm(x,y)必然大于 x和y,那么显然上一步加的就是现在比较大的坐标。所以上一步的方向总是固定的。
往回推的时候。
如果 x>y 直接 swap(x,y) 方便计算
现在假设上一步是 x,y
那么 gcd(x,y) = d; x = pd; y = qd; 显然 p q 是互质的,不然gcd就不是 d 了。lcm(x,y) = pqd;
当前点是 (
e
x
e_x
ex,
e
y
e_y
ey) 那么可以推出
e
y
e_y
ey = y+lcm(x,y) = pqd+qd = q(p+1)d;
由此也就可以推回上一步了。 y =
e
y
e_y
ey/(
e
x
e_x
ex/d + 1); (
e
x
e_x
ex = x,因为增加的坐标是y 所以x和上一步相同。)
然后重复这个步骤。当
e
y
e_y
ey % (
e
x
e_x
ex/d + 1) != 0 时,结束求解。因为这个点不可能时从别的地方跳过来的。
所以最终的ans 就是 碰到过的所有点,当然终点也算一个。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int,int>
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define Max(x,y) (x)>(y)?(x):(y)
#define int long long
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const ll mod = 998244353;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
//const int inf = 0x3f3f3f3f;
inline int read(){int ret = 0, sgn = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')sgn = -1;ch = getchar();}
while (ch >= '0' && ch <= '9'){ret = ret*10 + ch - '0';ch = getchar();}
return ret*sgn;}
inline void Out(int a){if(a>9) Out(a/10);putchar(a%10+'0');}
int qpow(int m, int k, int mod){int res=1%mod,t=m%mod;while(k){if(k&1)res=res*t%mod;t=t*t%mod;k>>=1;}return res;}
ll gcd(ll a,ll b){return b==0?a : gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll x,ll mod){return qpow(x,mod-2,mod)%mod;}
signed main()
{
int t = 1,cas = 1;
cin>>t;
while(t--)
{
int x,y;
cin>>x>>y;
if(x > y) swap(x,y);
int d = gcd(x,y);
int ans = 1;
while(y%(x+d) == 0)
{
ans ++;
y /= (x/d+1);
if(x>y) swap(x,y);
d = gcd(x,y);
}
cout<<"Case #"<<cas++<<": "<<ans<<endl;
}
}