hdu5584 LCM Walk(gcd)

题目链接: hdu5584 ( LCM Walk )

令起点 \(x_0=pt,y_0=qt\)\(p,q,t\in N^+\)\(p,q\) 互质,

\(gcd(x_0,y_0)=t,lcm(x_0,y_0)=pqt\) .

不妨设 \(x_1=pt,y_1=pt+pqt=qt(1+p)=q(t+x_1)\)

于是有 \(x_0=x_1, y_0=y_1/(t+x_1)\cdot t\)

不难发现 \(gcd(x_1,y_1)=gcd(x_0,y_0)=t\) .

/**
 * hdu5584 LCM Walk
 *
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

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

const int N = 1003;
int main()
{
    int T;
    scanf("%d", &T);
    for (int cas = 1; cas <= T; ++cas) {
        int x, y;
        scanf("%d%d", &x, &y);
        int d = gcd(x, y);
        if (x > y) swap(x, y);
        int ans = 1;
        while (y%(d+x) == 0) {
            // cout << x << '#' << y << endl;
            ++ans;
            y = y/(d+x)*d;
            if (x > y) swap(x, y);
        }
        printf("Case #%d: %d\n", cas, ans);
    }
    return 0;
}

posted @ 2021-02-17 16:05  Zewbie  阅读(74)  评论(0编辑  收藏  举报