Skip to content

Commit db1c052

Browse files
authored
Create 2518-number-of-great-partitions.js
1 parent bade561 commit db1c052

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

2518-number-of-great-partitions.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const ll = BigInt,
2+
mod = 1e9 + 7,
3+
bmod = ll(mod)
4+
const sm = (a) => a.reduce((x, y) => x + y, 0)
5+
const powmod = (a, b, mod) => {
6+
let r = 1n
7+
while (b > 0n) {
8+
if (b % 2n == 1) r = (r * a) % mod
9+
b >>= 1n
10+
a = (a * a) % mod
11+
}
12+
return r
13+
}
14+
const minus_mod = (x, y, mod) => (((x - y) % mod) + mod) % mod
15+
const knapsack_01 = (a, k) => {
16+
if (sm(a) < 2 * k) return 0
17+
let dp = Array(k).fill(0)
18+
dp[0] = 1
19+
for (const x of a) {
20+
for (let j = k - 1; j > x - 1; j--) {
21+
dp[j] += dp[j - x]
22+
dp[j] %= mod
23+
}
24+
}
25+
let bad = ll(sm(dp) * 2),
26+
tot = powmod(2n, ll(a.length), bmod),
27+
good = minus_mod(tot, bad, bmod)
28+
return good
29+
}
30+
/**
31+
* @param {number[]} nums
32+
* @param {number} k
33+
* @return {number}
34+
*/
35+
const countPartitions = function(nums, k) {
36+
return knapsack_01(nums, k)
37+
};
38+
39+
40+

0 commit comments

Comments
 (0)