Hnu 13190 Fractional Lotion(数论)

题意:

题目给出1/n,求(x,y)满足下面的等式的有多少组。
1/x + 1/y = 1/n

解析:

令 x = n+k
1/(n+k) + 1/y = 1/n
1/y = 1/n - 1/(n+k)
1/y = k/n(n+k)
y = n(n+k) / k
y = n*n/k + n

枚举k小于 使得 k能够被 n*n 给整除,问题便转化为求n*n的因子个数, 设n=p1^e1 * p2^e2 * p3^e3 …*pk^ek,则 n*n= p1^(2*e1) p2^(2*e2) …*pk^(2*ek) 。则因子个数sum=(2*e1+1)(2*e2+1)(2*ek+1); 然后要去除重复的 (sum+1)/2。

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e3;
char num[N];
vector<int> prime;
int factor[N];
bool is_prime(int num) {
    for(int i = 2; i*i < num; i++)
        if(num % i == 0)
            return false;
    return true;
}
int init() {
    prime.clear();
    for(int i = 2; i <= 10000; i++)
        if(is_prime(i))
            prime.push_back(i);
}
int main() {
    init();
    int n;
    while(scanf("%s", num) != EOF) {
        int n = atoi(num+2), tot = 0;
        memset(factor, 0, sizeof(factor));
        for(int i = 0; i < prime.size() && n > 1; i++) {
            if(n % prime[i] == 0) {
                while(n % prime[i] == 0) {
                    factor[tot]++;
                    n /= prime[i];
                }
                tot++;
            }
        }
        ll ans = 1;
        for(int i = 0; i < tot; i++) {
            ans *= (factor[i] * 2 + 1);
        }
        printf("%lld\n", (ans+1) / 2);
    }
    return 0;
}

你可能感兴趣的:(13190,hnu)