F-等式

链接:https://www.nowcoder.com/acm/contest/90/F
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
给定n,求1/x + 1/y = 1/n (x<=y)的解数。(x、y、n均为正整数)

输入描述:
在第一行输入一个正整数T。
接下来有T行,每行输入一个正整数n,请求出符合该方程要求的解数。
(1<=n<=1e9)
输出描述:
输出符合该方程要求的解数。
示例1
输入
3
1
20180101
1000000000
输出
1
5
181
1/x + 1/y = 1/n –> xn +yn = xy –> n^2 + xn + yn - xy = n^2 –> (n-x)(n-y) = n^2 事实上是(x-n)(y-n) = n^2,因为y >= x >= n。
于是求n^2的因子即可,考虑n的质数分解n = p1^a1 * p2^a2…pk^ak,n的因子数=(a1 + 1)..(ak + 1),n^2 = p1^2a1 * p2^2a2 … pk^2ak,n^2 的因子数=(2a1 + 1)…(2ak +1).

#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <cstring>
#include <stack>
#include <vector>
#include <map>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
int main() {
    int T, n;
    cin >> T;
    while (T--) {
        cin >> n;
        int t = sqrt(n);
        ll sum = 1;
        for (int i = 2; i <= t; ++i) {
            int a = 0;
            while ((n % i) == 0) {
                //cout << i << endl;
                n /= i;
                a++;
            }
            sum = sum * (2*a + 1);
        }
        if (n > 1) {
            sum *= 3;
        }
        printf("%lld\n", (sum + 1) / 2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jack_zhuiyi/article/details/79711423