Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
![](https://i-blog.csdnimg.cn/blog_migrate/46e717efa1569f152b4199daf7a0ec54.png)
A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:
![](https://i-blog.csdnimg.cn/blog_migrate/ebae29064716836aa36a058a2573ede9.png)
You are given sequence ai, help Princess Twilight to find the key.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of elements of the sequences a and b. The next line contains n integersa1, a2, ..., an (1 ≤ ai ≤ 30).
Output the key — sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
5 1 1 1 1 1
1 1 1 1 1
5 1 6 4 2 8
1 5 3 1 8
由于题目最后是要求输出满足最优的方案数,所以要记录一下路径,之后打印出来就好了
#include
#include
#include
#include
using namespace std;
#define INF ((1<<31)-1)
int a[110];
int anss[110];
int dp[102][131080];
int dp1[102][131080];
int dp2[102][131080];
int fact[60];
int prime[20];
int isprime[65];
int n;
int hehe(int x)
{
if(x<0)return -x;
else return x;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int i,j,temp;
memset(isprime,0,sizeof(isprime));
for(i=2;i<=8;i++)if(!isprime[i])
for(j=i*i;j<=60;j+=i)isprime[j]=1;
int d=0;
for(i=2;i<=60;i++)if(!isprime[i])prime[d++]=i;
memset(fact,0,sizeof(fact));
fact[1]=0;
for(i=2;i<=60;i++)
{
int x=i;
j=0;
while(x>1)
{
int ok=0;
while(x%prime[j]==0){
ok=1;
x/=prime[j];
}
if(ok)fact[i]+=(1<
dp[n][j]){ ans=dp[n][j]; flag=n*131080+j; } d=0; while(flag/131080) { i=flag/131080; j=flag%131080; anss[d++]=dp1[i][j]; flag=dp2[i][j]; } for(i=n-1;i>0;i--)printf("%d ",anss[i]); printf("%d\n",anss[i]); } return 0; }