所以最终直接对 n 进行质因数分解即可。假设我们得到了 n=p1q1p2q2…pkqk,则有一个定理:n 的质因数个数为 i=1∏k(qi+1)。所以有 p=i=1∏k(2×qi+1)。
时间复杂度 O(Tn)。
C++
#include <bits/stdc++.h>#define int long longusing namespace std;
unordered_map<int, int> mp;
signed main() { ios::sync_with_stdio(false); cin.tie(0);
int t; cin >> t;
while(t--) {
int n; cin >> n; mp.clear();
for(int i = 2;i * i <= n;i++)
if(n % i == 0) {
while(n % i == 0) n /= i, mp[i]++;
} if(n != 1) mp[n]++; for(auto i : mp) mp[i.first] *= 2;
long long num = 1; for(auto i : mp) num = (num * (mp[i.first] + 1)); cout << num / 2 << "\n";
}
return 0;
}