-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3444.cpp
More file actions
37 lines (35 loc) · 1.17 KB
/
3444.cpp
File metadata and controls
37 lines (35 loc) · 1.17 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
class Solution {
public:
int minimumIncrements(vector<int>& nums, vector<int>& target) {
int m = target.size();
vector<long long> mask2LCM((1 << m), 0);
for (int mask = 1; mask <= (1 << m) - 1; ++mask) {
long long l = 1;
for (int j = 0; j < m; ++j) {
if ((1 << j) & mask) {
l = lcm(l, target[j]);
}
}
mask2LCM[mask] = l;
}
vector<long long> dp((1 << m), INT_MAX);
dp[0] = 0;
for (auto& num : nums) {
vector<long long> tempDP = dp;
for (int mask = 1; mask < (1 << m); ++mask) {
long long l = mask2LCM[mask];
long long cost = num % l;
if (cost != 0) cost = l - cost;
for (int oldMask = 0; oldMask < (1 << m); ++oldMask) {
int newMask = mask | oldMask;
tempDP[newMask] = min(tempDP[newMask], dp[oldMask] + cost);
}
}
dp = tempDP;
}
return dp[(1 << m) - 1];
}
long long lcm(long long a, long long b) {
return a * b / __gcd(a, b);
}
};