codeforce 453B - Little Pony and Harmony Chest

本文介绍了一个算法问题,即寻找最小化特定表达式的和谐序列。通过动态规划和预处理质因数,解决了输入序列的最优解问题,确保序列中任意两数的最大公约数为1。

题目如下:

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 nintegers 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 

分析:因为ai的范围时1-30,而数组全部为1时肯定满足条件的,所以只需要找1-60内的值就够了。而另一个要求是gcd为一,也就是没有除了1以为的共同因子,那么可以先把每个数的因子求出来,已位的形式表达,两个数要是gcd为1的话,则需要两个位表达式与一下为0,如4的因子为2,我们标记2所在的那一位为1,当出现8时,8的因子也为2,而看到2所在的那位已经是1了,所以8可能不可能是合法的值了。转移方程为dp[i][j|p(k)] = min(dp[i][j|p(k)],dp[i-1][j]+abs(a[i]-k)),意思是当第i-1个数结束后状态为j的时候,在第i个位置的地方选择了数值k,那么当前的状态就是dp[i][j|p(k)], p(k)即k用位表达的值,当然进行转移的前提是p(k)&j==0,不然就不满足gcd为1的条件了,其他变量在注视中写的挺清楚了



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>

using namespace std;

const int maxn=62;
int top;
int primes[maxn];
int prime[maxn];
int N=(1<<16);

void make()
{
    top=0;
    int i, j;
    for(i = 2; i < maxn; i++)
    {
        if(prime[i]) continue;
        primes[top++] = i;
        for(j = 2; i * j < maxn; j++)
        {
            prime[i * j] = 1;
        }
    }
}

int dp[105][(1<<17)+10];
int pre[105][(1<<17)+10];
int anss[105][(1<<17)+10];

int a[105];
int bit[65];
int as[105];

int gt(int x)
{
    int ans=0;
    for(int i=0; i<top; i++)
    {
        if(x%primes[i]==0)
        {
            ans+=(1<<i);
            while(x%primes[i]==0)
            {
                x/=primes[i];
            }
        }
    }
    return ans;
}
void init()
{
    for(int i=1; i<=60; i++)
    {
        bit[i]=gt(i);
    }
}
int inf=30000;

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        make();
        init();

        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);

        for(int i=0; i<105; i++)
            for(int j=0; j<N; j++)
            {
                dp[i][j]=inf;
                pre[i][j]=-1;
                anss[i][j]=0;
            }


        dp[0][0]=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<N; j++)
            {
                for(int k=1; k<=60; k++)
                {
                    if(dp[i-1][j]!=inf&&(bit[k]&j)==0)
                    {
                        if(dp[i][bit[k]|j]>(dp[i-1][j]+abs(a[i]-k)))
                        {
                            dp[i][bit[k]|j]=(dp[i-1][j]+abs(a[i]-k));
                            anss[i][bit[k]|j]=k;
                            pre[i][bit[k]|j]=j;
                        }
                    }
                }
            }
        }

        int ans=inf;
        int tar=0;
        for(int i=0; i<N; i++)
        {
            if(ans>dp[n][i])
            {
                ans=dp[n][i];
                tar=i;
            }
        }

        stack<int> s;
        int sta=n;
        s.push(anss[sta][tar]);
        while(pre[sta][tar]!=-1)
        {
            tar=pre[sta][tar];
            sta--;
            s.push(anss[sta][tar]);
        }
        s.pop();
        while(!s.empty())
        {
            printf("%d ",s.top());
            s.pop();
        }
        printf("\n");
    }
}


内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值