LCM Walk

Topic links: https://vjudge.net/contest/362170#problem/F

 

Subject to the effect:

To tell you the end is (ex, ey), ask you the number of possible starting point (if the starting point is (x, y) then you go to the next point is (x + z, y) or (x, y + z) z = lcm (x, y))

 

idea:

Reference blog: https://blog.csdn.net/yuzhiwei1995/article/details/51100389

 

 

LL gcd(LL a,LL b) {
    LL t;
    while (b) {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

int main() {
    int T;
    scanf("%d",&T);
    int t = 1;
    while (T--) {
        LL a,b;
        scanf("%lld%lld",&a,&b);
        LL GCD = gcd(a,b);
        LL m1 = a / GCD;
        LL m2 = b / GCD;
        LL ans = 1;
        while (1) {
            if (m1 < m2)
                swap(m1,m2);
            if (m1 % (1 + m2) == 0) {
                m1 = m1 / (1 + m2);
                ans++;
            }
            else
                break;
        }
        printf("Case #%d: %lld\n",t++,ans);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/-Ackerman/p/12508144.html