CF 453B(Little Pony and Harmony Chest-数列最小加减1更改方案,满足任意2数互质-位运算dp+最坏情况分析+记忆化搜索)

16 篇文章 0 订阅
9 篇文章 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 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 

假想把数列改成1,1,1... 1 满足题意

因此我们对于数列中任意1个数,只要寻找比改成1更优的策略

由于ai30,bi30+29=59,因此涉及的质数≤59

故可用记忆化搜索+位运算Dp求解



#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100+10)
#define MAXAi (30)
#define MAXBi (59)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,a[MAXN],prime[16] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
int dp[1<<16][MAXN],divv[MAXN];
int dfs(int mask,int siz)
{
	if (siz==n+1) return 0;
	int &ret=dp[mask][siz];
	if (ret!=-1) return ret;ret=INF;
	For(i,2*a[siz]-1)
	{
		if (mask&divv[i]) continue;
		ret=min(ret,dfs(mask|divv[i],siz+1)+abs(i-a[siz]));
	}
	return ret;
}
int ans[MAXN];
void find(int mask,int siz)
{
	if (siz==n+1) return ;
	int ret=dp[mask][siz];
	For(i,2*a[siz]-1)
	{
		if (mask&divv[i]) continue; //每个质数最多出现在1个bi因子中
		if (ret==dfs(mask|divv[i],siz+1)+abs(i-a[siz])) 
		{
			ans[siz]=i;
			find(mask|divv[i],siz+1); 
			return;
		}
	}
}
int main()
{
//	freopen("Harmony Chest.in","r",stdin);
//	freopen(".out","w",stdout);
	scanf("%d",&n);
	For(i,n) scanf("%d",&a[i]);
	dfs(0,1);
	MEM(divv)
	memset(dp,-1,sizeof dp);
	For(i,MAXBi)
		Rep(j,16)
		{
			if (i%prime[j]==0) divv[i]|=1<<j;
		}
	dfs(0,1);
	find(0,1);
	
	For(i,n-1) printf("%d ",ans[i]);printf("%d\n",ans[n]);
	
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值