NOIP2009 Hankson interesting questions

Topic Link

This problem is actually required to satisfy (a, x) = b / \ [c, x] is the number of x = d.

[c, x] = d   =>  x | d

Therefore, x is a number from about d

So the idea of ​​this question is very clear: the number of each approximately enumeration d, find the number of numbers to meet the conditions. Time complexity: a sqrt (enumeration only from 1 to sqrt (d) can be) multiplied by a log, certainly will not burst.

Remember to open long long

Code

#include <iostream>
using namespace std;
//快读 
long long read() {
    long long ret = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar(); 
    }
    while (isdigit(ch)) {
        ret = ret * 10 + ch - '0';
        ch = Getchar (); 
    } 
    return RET * F; 
} 
// fast write 
void Write ( Long  Long X) {
     IF (X> = 10 ) Write (X / 10 ); 
    the putchar (X % 10 + ' 0 ' ); 
} 
Long  Long T, A, B, C, D, ANS;
 // greatest common divisor (Euclidean algorithm) 
Long  Long GCD ( Long  Long X, Long  Long Y) {
     return Y == 0 X: GCD (? y, x% y);
}
//求最小公倍数
long long lcm(long long x, long long y) {
    return x * y / gcd(x, y);
}
int main() {
    t = read();
    while (t--) {
        ans = 0;
        a = read(), b = read(), c = read(), d = read();
        for (long long i = 1; i * i <= d; i++) {//从1到sqrt(d)
            if (d % i == 0) {
                if (gcd(a, i) == b && lcm(c, i) == d) ans++;
                if (d / i != i) {
                    if (gcd(a, d / i) == b && lcm(c, d / i) == d) ans++;
                }
            }
        }
        write(ans);
        putchar('\n');
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zcr-blog/p/11618531.html