CF 453B Little Pony and Harmony Chest 解题报告(状态压缩DP)

56 篇文章 0 订阅
11 篇文章 0 订阅

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 containsn integers a1, 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 

    解题报告: 上次CF的题,比赛时没有想到。要求是将给定的n个数的序列变换成一个两两互质的序列,且每个数改动的和最小。

    直接状态压缩,记录使用了哪些素数,达到的最小的改动和。因为给定的ai<=30,那么改动最大不超过58,30改成59和改成1得到的结果是一样的。

    每次尝试将当前数字改成1到58,最后取最小值即可。代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
const LL inf=1e15;
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM
    work();
}

/***************************************************/

int h[60];
int pri[18];
int tot;

int dp[105][bit(16)];
int ss[105][bit(16)];

void calPrime()
{
    fff(i, 2, 58) if(h[i] == 0)
    {
        pri[tot] = i;
        h[i] ^= bit(tot);
        for(int j=i+i;j<=58;j+=i)
            h[j] ^= bit(tot);

        tot++;
    }
}

void work()
{
    calPrime();

    int n;
    while(scanf("%d", &n) == 1)
    {
        memset(dp, 0x1f, sizeof(dp));
        dp[0][0] = 0;

        ff(i, n)
        {
            int cur;
            scanf("%d", &cur);

            ff(st, bit(tot)) if(dp[i][st] < 3000)
            {
                fff(j, 1, 58) if((st & h[j]) == 0 && dp[i+1][st ^ h[j]] > dp[i][st] + abs(cur - j))
                {
                    dp[i+1][st ^ h[j]] = dp[i][st] + abs(cur - j);
                    ss[i+1][st ^ h[j]] = j;
                }
            }
        }

        int mi = *min_element(dp[n], dp[n]+bit(tot));

        vector<int> ans;
        ff(st, bit(tot)) if(mi == dp[n][st])
        {
            int state = st;
            dff(i, n, 1)
            {
                ans.push_back(ss[i][state]);
                state = state ^ h[ss[i][state]];
            }
            break;
        }

        if(n) printf("%d", ans[n-1]);
        dff(i, n-2, 0) printf(" %d", ans[i]);
        puts("");
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值