LCM Walk

LCM Walk

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 \((s_x,s_y),\) 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 \((e_x,e_y)\). 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 \((e_x,e_y)\)

Input

First line contains an integer \(T\), which indicates the number of test cases.
Every test case contains two integers \(e_{x}\) and \(e_{y}\), which is the destination grid.
\(1 \leq T \leq 1000\)
\(1 \leq e_{x}, e_{y} \leq 10^{9}\)

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.

Example Input

3

6 10

6 8

2 8

Example Output

Case #1: 1

Case #2: 2

Case #3: 3

思路:

正难则反,考虑倒推从终点能回到那些地方

\[设(x=kx',y=ky')\rightarrow (kx'+kx'y',ky'),(kx',ky'+kx'y')\\ [A\rightarrow B表示从A能1步到B] \]

\[设(x=kx',y=ky')\leftarrow (kz',ky') \]

\[\therefore z'+y'z'=x'\\ z'=\frac{x'}{y'+1} \]

\[(x=kx',y=ky')\leftarrow (kx',kz')同理 \]

\[然后dfs即可,详见代码 \]

\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)

#include<set>
#include<cmath>
#include<stack>
#include<deque>
#include<queue>
#include<cstdio>
#include<vector>
#include<climits>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
# define Type template<typename T>
# define ll long long
# define read read1<ll>()
Type T read1(){
    T t=0;char k;
    bool v=0;
    do (k=getchar())=='-'&&(v=1);while('0'>k||k>'9');
    while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
    return v?-t:t;
}
ll phi(ll x){
    ll f=x;
    for(ll i=2;i*i<=x;++i)
        if(!(x%i)){
            f=f/i*(i-1);
            while(!(x%i))x/=i;
        }
    if(x>1)f=f/x*(x-1);
    return f;
}
ll ch(ll n,ll x,ll mo){
    if(!x)return 0;
    ll t=ch(n,x>>1,mo);
    t=t+t;
    if(x&1)t=t+n;
    if(t>=mo)t%=mo;
    return t;
}
ll qkpow(ll n,ll x,ll mo){
    if(!x)return 1;
    ll t=qkpow(n,x>>1,mo);
    t=ch(t,t,mo);
    if(x&1)t=ch(t,n,mo);
    return t;
}
ll exgcd(ll x,ll y,ll &a,ll &b){
    if(!y){
        a=1,b=0;
        return x;
    }
    ll v=exgcd(y,x%y,b,a);
    b-=x/y*a;
    return v;
}
ll up(ll v,ll d){return (v%d+d)%d;}
ll excrt(ll *a,ll *r,int s){
    for(int i=1;i<s;++i){
        ll x,y,v=exgcd(a[i],a[i+1],x,y);
        if((r[i]-r[i+1])%v)return -1;
        x=up((r[i+1]-r[i])/v*x,a[i+1]/v);
        a[i+1]=a[i+1]/v*a[i];r[i+1]=(r[i]+ch(x,a[i],a[i+1]))%a[i+1];
    }
    return r[s];
}
set<pair<int,int> >se;
void dfs(int x,int y){
    // cout<<x<<' '<<y<<endl;
    auto v=se.lower_bound(make_pair(x,y));
    if(v!=se.end()&&*v==make_pair(x,y))return;
    se.insert(make_pair(x,y));
    int w=__gcd(x,y);
    if(!((x/w)%(y/w+1)))dfs((x/w)/(y/w+1)*w,y);
    if(!((y/w)%(x/w+1)))dfs(x,(y/w)/(x/w+1)*w);
}
int main(){
    for(int T=read,t=0;T--;){
        se.clear();
        dfs(read,read);
        printf("Case #%d: %d\n",++t,se.size());
    }
    return 0;
}  
posted @ 2020-09-03 20:49  ファイナル  阅读(129)  评论(0)    收藏  举报