Codeforces Round #259 (Div. 1)——Little Pony and Harmony Chest,

來源:互聯網
上載者:User

Codeforces Round #259 (Div. 1)——Little Pony and Harmony Chest,

題目串連

  • 題意:
    給n個整數ai,求一個序列bi,使得b序列中任意兩個數互質,而且sigma(abs(ai - bi))最小,輸出任意一個b序列即可
     (1 ≤ n ≤ 100)  (1 ≤ ai ≤ 30)
  • 分析:
    首先明確一點,題目沒有限制b的範圍。。。。為此wa了好多次,不過可以推斷出來,b肯定小於等於60
    任意兩個數互質,也就是說,對於新加入的一個bi,如果知道了之前所有數的質因子,那麼當前數只要沒有這個質因子就是一種滿足的情況。而60以內的質數不到20個,所以直接狀壓DP即可。DP[i][j],表示處理到第i個數,所含的質因子為j(二進位形式)
const int MAXN = 60;const int SIZE = 16;int ipt[110];int dp[110][1 << SIZE];int p[110][1 << SIZE];int x[110][1 << SIZE];int to[MAXN + 1];int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};map<int, int> mp;void init(){    REP(i, SIZE)        mp[prime[i]] = 1 << i;    FE(ii, 0, MAXN)    {        int n = ii, ret = 0, i;        for (i = 0; i < SIZE && prime[i] * prime[i] <= n; i++)            if (n % prime[i] == 0)            {                while (n % prime[i] == 0)                    n /= prime[i];                ret += 1 << i;            }        if (n > 1)            ret += mp[n];        to[ii] = ret;    }}int n;void dfs(int idx, int id){    if (idx >= 2)        dfs(idx - 1, p[idx][id]);    printf("%d%c", x[idx][id], idx == n ? '\n' : ' ');}int main (){    int all = 1 << SIZE;    init();    while (~RI(n))    {        FE(i, 1, n)            RI(ipt[i]);        CLR(dp, INF); dp[0][0] = 0;        FE(i, 0, n - 1)        {            FF(j, 0, all)            {                FE(jj, 0, MAXN)                {                    int v = to[jj];                    if (j & v)                        continue;                    int val = dp[i][j] + abs(jj - ipt[i + 1]);                    int& nxt = dp[i + 1][j | v];                    if (val < nxt)                    {                        nxt = val;                        p[i + 1][j | v] = j;                        x[i + 1][j | v] = jj;                    }                }            }        }        int ans = INF, id = 0;        REP(j, all)        {            if (dp[n][j] < ans)            {                ans = dp[n][j];                id = j;            }        }        dfs(n, id);    }    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.