-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp_bit.cpp
executable file
·96 lines (78 loc) · 2.16 KB
/
dp_bit.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
using namespace std;
#define N 105
#define M 1<<17
#define MAX 60
#define PRIMESIZE 17
#define INF INT_MAX
typedef long long ll;
int n, a[N], b[N], dp[N][M], factorMask[MAX];
int primes[PRIMESIZE] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59};
void computeFactorMask() {
for(int i=1;i<MAX;i++){
for(int j=0;j<PRIMESIZE;j++){
if(i%primes[j] == 0) {
factorMask[i] |= 1<<j;
}
}
}
}
int computeMinSum(int index, int mask, int n, int a[]) {
if(index >= n) {
return 0;
}
if(dp[index][mask] != -1) {
return dp[index][mask];
}
dp[index][mask] = INF;
for(int bi=1;bi<2*a[index];bi++){
int primeFactorMask = factorMask[bi];
if((mask & primeFactorMask) == 0){
dp[index][mask] = min(dp[index][mask], abs(a[index]-bi) +
computeMinSum(index+1, mask|primeFactorMask, n, a));
}
}
return dp[index][mask];
}
void findArrayBUtil(int index, int mask, int n, int a[], int minSum) {
if(index >= n) {
return;
}
for(int bi=1;bi<2*a[index];bi++) {
int primeFactorMask = factorMask[bi];
if((primeFactorMask & mask) == 0) {
int newMask = mask | primeFactorMask;
if(abs(a[index]-bi) + dp[index+1][newMask] == minSum) {
b[index] = bi;
findArrayBUtil(index+1, newMask, n, a, minSum - abs(a[index]-bi));
return;
}
}
}
}
void findArrayB() {
int minSum = computeMinSum(0, 0, n, a);
findArrayBUtil(0, 0, n, a, minSum);
}
void printArrayB() {
for(int i=0;i<n;i++)
cout<<b[i]<<" ";
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if(i >= n)
dp[i][j] = 0;
else
dp[i][j] = -1;
}
}
computeFactorMask();
findArrayB();
printArrayB();
return 0;
}