# CF 453B (Little Pony and Harmony Chest-minimum addition and subtraction of the Series 1 Change Scheme, meeting the needs of any two numbers of mutual quality-bit operation dp + worst case analysis + Memory search), plus and subtraction within 100

Source: Internet
Author: User

CF 453B (Little Pony and Harmony Chest-minimum addition and subtraction of the Series 1 Change Scheme, meeting the needs of any two numbers of mutual quality-bit operation dp + worst case analysis + Memory search), plus and subtraction within 100

B. Little Pony and Harmony Chesttime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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

A sequence of positive integersBIIs harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an existing ent book, the key of the chest is a harmony sequenceBIWhich minimizes the following expression:

You are given sequenceAI, Help Princess Twilight to find the key.

Input

The first line contains an integerN(1 digit ≤ DigitNLimit ≤ limit 100)-the number of elements of the sequencesAAndB. The next line containsNIntegersA1, bytes,A2, middle..., middle ,...,AN(1 digit ≤ DigitAILimit ≤ Limit 30 ).

Output

Output the key-sequenceBIThat minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.

Sample test (s) input
`51 1 1 1 1`
Output
`1 1 1 1 1 `
Input
`51 6 4 2 8`
Output
`1 5 3 1 8 `

Suppose you want to change the number of columns to, 1... 1.

Therefore, for any number of columns, we only need to find a better strategy than 1.

Because ai is less than or equal to 30, bi is less than or equal to 30 + 29 = 59, the involved prime numbers are less than or equal to 59.

Therefore, you can use the Memory search + bit operation Dp to solve the problem.

`#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;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;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## 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.