给定n,求1/x + 1/y = 1/n (x<=y)的解数。(x、y、n均为正整数)

基本的思路

首先化简可得
x+yxy=1n

关键一步


x=n+a
y=n+b

这一步相当关键,整个题目的精髓所在。
因为x和y和n都是正整数,要想等式成立,x和y作为分母肯定都要大于n,不然等式成立不了


n2=ab
然后就等价于求解 n2 的约数(因子)个数了

做法

这里写图片描述
个人对这个公式的理解就是,从分解的质因数中要想凑出一个约数来就是一个组合问题,对每一种质因数可以取 (0pi) 个,那么有 pi+1 种选择,乘起来便是。
首先是可以对 n 分解质因数,然后 n2 只是相对与 n 来说每种因数个数翻倍了。

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        PrintWriter cout = new PrintWriter(System.out);
        int t = cin.nextInt();
        while (t-- > 0) {
            int n = cin.nextInt();
            ArrayList<Integer> factorCnt = new ArrayList<>();
            for (int i = 2; i * i < n; i++) {
                int cnt = 0;
                while (n % i == 0) {
                    n /= i;
                    cnt++;
                }
                if (cnt > 0)
                    factorCnt.add(cnt);
            }
            if (n > 1) {
                factorCnt.add(1);
            }
            long ans = 1;
            for (Integer cnt : factorCnt) {
                ans *= (cnt * 2 + 1);
            }
            cout.println(ans / 2 + 1);
            cout.flush();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq965194745/article/details/79660077
Y/N