Codeforces Round #259 (Div. 1) B题


B. Little Pony and Harmony Chest
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.

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:

You are given sequence ai, help Princess Twilight to find the key.

Input

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

Output the key — sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.

Sample test(s)
input
5
1 1 1 1 1
output
1 1 1 1 1 
input
5
1 6 4 2 8
output
1 5 3 1 8 

由题意可知b的范围在60以内,而60以下的素数只有17个,所以可以状态压缩,dp[i][s]表示前i个数中已经选得的所有b的素因子的集合s下的最优解,fack[k]表示数k的素因子集合,转移时判断dp[i][s|fact[k]]>dp[i-1][s]+abs(a[i]-k)是否成立,成立就转移。

由于题目最后是要求输出满足最优的方案数,所以要记录一下路径,之后打印出来就好了

#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; } 
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值