-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
73 lines (67 loc) · 1.94 KB
/
Solution.cs
File metadata and controls
73 lines (67 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class Solution
{
public int IdealArrays(int n, int maxValue)
{
int mod = (int)1e9 + 7;
// dp[n][k] : how many distinct plans to place k same factors in n places (allowing multiple factors in the same place)
// dp[i][j] = sum { dp[i-1][j-t] } where t = 0,1,2...j
long[][] dp = new long[n + 1][];
for (int i = 0; i <= n; i++)
{
dp[i] = new long[15]; // 2^14 > 1e4
}
dp[0][0] = 1;
for (int i = 1; i <= n; i++)
{
dp[i][0] = 1;
for (int j = 1; j <= 14; j++)
{
// for (int t = 0; t <= j; t++)
// {
// dp[i][j] = (dp[i][j] + dp[i - 1][j - t]) % mod;
// }
dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % mod;
}
}
int[] spf = BuildSpf(maxValue);
long ret = 0;
for (int i = 1; i <= maxValue; i++)
{
Dictionary<int, int> map = CountPrimeFactors(i, spf);
long ans = 1;
foreach (int count in map.Values)
{
ans = ans * dp[n][count] % mod;
}
ret = (ret + ans) % mod;
}
return (int)ret;
}
int[] BuildSpf(int maxValue)
{
int[] spf = new int[maxValue + 1];
for (int i = 2; i <= maxValue; i++) spf[i] = i;
for (int i = 2; i <= maxValue; i++)
{
if (spf[i] == i)
{
for (int j = 2 * i; j <= maxValue; j += i)
{
if (spf[j] == j) spf[j] = i;
}
}
}
return spf;
}
Dictionary<int, int> CountPrimeFactors(int num, int[] spf)
{
Dictionary<int, int> map = [];
while (num > 1)
{
int prime = spf[num];
map[prime] = map.GetValueOrDefault(prime, 0) + 1;
num /= prime;
}
return map;
}
}