Count the Number of Ideal Arrays LeetCode solution in C++

1
int comb[10001][14] = {1}, cnt[10001][14] = {}, mod = 1000000007;
2
class Solution {
3
public:
4
int idealArrays(int n, int maxValue) {
5
if (comb[1][1] == 0) { // one-time computation.
6
for (int s = 1; s <= 10000; ++s) // nCr (comb)
7
for (int r = 0; r < 14; ++r)
8
comb[s][r] = r == 0 ? 1 : (comb[s - 1][r - 1] + comb[s - 1][r]) % mod;
9
for (int div = 1; div <= 10000; ++div) { // Sieve of Eratosthenes
10
++cnt[div][0];
11
for (int i = 2 * div; i <= 10000; i += div)
12
for (int bars = 0; cnt[div][bars]; ++bars) cnt[i][bars + 1] += cnt[div][bars];
13
}
14
}
15
int res = 0;
16
for (int i = 1; i <= maxValue; ++i)
17
for (int bars = 0; bars < min(14, n) && cnt[i][bars]; ++bars)
18
res = (1LL * cnt[i][bars] * comb[n - 1][bars] + res) % mod;
19
return res;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0