C. Divisors of the Divisors of An Integer(质因数分解,数论) 2018-2019 ACM-ICPC, Asia Dhaka Regional Contest
C - Divisors of the Divisors of An Integer(质因数分解,数论)
题目:
给出\(n(1e6)\),请问\(n!\)的因子的因子的个数。
思路:
因子的个数求解不难,可以知道是质因数分解。
对于一个质因数\(P_i^{k}\),可以知道其因子的数量的是\((k + 1)\)。对于具体的\(p_i^0\),有一个因子1;对于\(p_i^1\),有两个因子1, \(p_1\);依次类推。易得公式为:
\[res=\prod{(k+1)*(k+2)/2}
\]
其中k是各个质因子的幂次
实现:
由于N的范围是\(1e6\),我们用\(N\sqrt{N}\)直接分解显然是会超时的。我们可以用筛法优化质因数的分解,达到O(n)的时间复杂度。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e7 + 7;
const int N = 1000005;
vector<int> primes;
bool vis[N];
int n;
void init()
{
for(int i = 2; i <= N; i ++)
{
if(!vis[i])
primes.push_back(i);
for(int j = 2; j * i <= N; j ++)
vis[i * j] = 1;
}
}
ll calc(int x)
{
int res = 0;
ll tmp = n;
while(tmp)
{
res += tmp / x;
tmp /= x;
}
return res;
}
int main()
{
init();
while(scanf("%d", &n))
{
if(!n) break;
ll res = 1;
for(int x : primes)
{
if(x > n)
break;
ll tmp = calc(x);
res = (res * ((tmp + 1) * (tmp + 2)) / 2ll) % mod;
}
printf("%lld\n", res);
}
}