-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
90 lines (70 loc) · 1.64 KB
/
D.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
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
const int INF = 1e8;
const int MAXN = 105;
const int MAXV = 4099;
int n;
int num[MAXN];
int otra[MAXN];
int primo[MAXN];
int criba[MAXN];
int memo[MAXN][MAXV];
void Pre(int u){
int cont = 0;
for(int i=2;i<=u;i++)
if(!criba[i]){
for(lli j=i*i;j<=u;j+=i)
criba[j] = i;
criba[i] = i;
primo[i] = cont++;
}
//printf("%d\n",cont);
for(int i=2;i<=u;i++){
int nueva = 0 , aux = i;
while(aux!=1){
nueva |= (1<<primo[criba[aux]]);
aux /= criba[aux];
}
otra[i] = nueva;
}
}
int DP(int id,int mask){
if(id==n) return memo[id][mask] = 0;
if(memo[id][mask]!=-1) return memo[id][mask];
int res = INF;
for(int i=1;i<=39;i++){
if(mask&otra[i]) continue;
res = min( res , DP( id + 1 , mask | otra[i] ) + abs(num[id] - i) );
}
return memo[id][mask] = res;
}
void DP2(int id,int mask){
if(id==n){
printf("\n");
return;
}
for(int i=1;i<=39;i++){
if(mask&otra[i]) continue;
if(memo[id][mask] - abs(num[id] - i)==memo[id+1][mask|otra[i]]){
printf(" %d",i);
DP2(id+1 , mask|otra[i]);
return;
}
}
}
int main(){
int u = 1;
Pre(39);
//printf("%d\n",(1<<13));
while(scanf("%d",&n) && n){
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
for(int i=0;i<n;i++)
fill( memo[i] , memo[i] + MAXV , -1 );
DP(0,0);
printf("Case #%d:",u++);
DP2(0,0);
}
return 0;
}