Tsinsen A1131 最大公约数和最小公倍数问题

http://oj.tsinsen.com/A1131

分析:我绝对是个智障!这么水的题看错了,题目说的是“以x0为gcd,以y0为lcm”…简单啦,gcd*lcm=x*y,枚举就行。

#include "bits/stdc++.h"
using namespace std;
long long Gcd(long long a, long long b) {
    long long r;
    while (b) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}
int main() {
    long long res = 0, gcd, lcm, pro;
    scanf("%I64d%I64d", &gcd, &lcm);
    pro = gcd * lcm;
    for (long long u = gcd; u <= lcm; ++u) {
        for (long long v = gcd; v <= lcm; ++v) {
            long long xPro = u * v;
            if (xPro > pro) break;
            if (xPro < pro) continue;
            long long xGcd = Gcd(u, v);
            if (xPro / xGcd != lcm) continue;
            res += 1;
        }
    }
    printf("%d\n", res);
    return 0;
}

你可能感兴趣的:(ACM-01)