Codeforces 453B B. Little Pony and Harmony Chest (dp+ number theory)

Source: Internet
Author: User

Topic Links:

Codeforces 453B

Main topic:

Given a sequence a, the number of a sequence b,b sequence of 22 coprime, ask can cause ∑| a i ? b i | The smallest scenario

Topic Analysis:
    • The definition state Dp[i][j] represents the smallest result of the number of previous I to J states, and the J-state represents the prime numbers that have been used.
    • Because when the data range of a is not more than 30, so if a number more than 60, then select 1 is better than it, so we can use the number of the quality factor also must not exceed 60, also only 17, so we only need each enumerator, and then the previous state to transfer.
    • The specific transfer process is shown in the code.
    • In order to get the scheme, we recorded that Pre[i][j] represents the last state where the number of first I reaches the maximum value of the state J, Num[i][j] indicates the number of the number of previous I to the state J to get the maximum value.
AC Code:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstdlib>#include <cstring>#define MAX 107using namespace STD;intN,a[max];intdp[107][1<< -];intpre[107][1<< -];intnum[107][1<< -];intprime[]={2,3,5,7, One, -, -, +, at, in, to,Panax Notoginseng, the, A, +, -, -};intstate[ -];voidPrint (intIintu) {if(i = =1)     {printf("%d", Num[i][u]);return; } Print (I-1, Pre[i][u]);printf("%d", Num[i][u]);}intMain () { while( ~scanf("%d", &n)) { for(inti =1; I <= N; i++)scanf("%d", &a[i]);memset(DP,0x3f,sizeof(DP));intINF = dp[0][0]; dp[0][0] =0;memset(State,0,sizeof(state)); for(inti =2; I < -; i + +) for(intj =0; J < -; J + +)if(I%prime[j] = =0) State[i] |= (1&LT;&LT;J);intTotal =1<< -; for(inti =0; I < n; i++) for(intj =0; J < Total; J + +) {if(Dp[i][j] = = INF)Continue; for(intK =1; K < -; k++) {if(STATE[K]&AMP;J)Continue;intx = state[k]|j;if(dp[i+1][X] > Dp[i][j] +ABS(a[i+1]-K) {dp[i+1][X] = Dp[i][j] +ABS(a[i+1]-K); pre[i+1][x] = j; num[i+1][x] = k; }                }            }intAns = Inf,loc =-1; for(inti =0; I < total; i++)if(Ans > Dp[n][i])                {ans = dp[n][i];            loc = i; } print (n, loc);puts(""); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 453B B. Little Pony and Harmony Chest (dp+ number theory)

Contact Us

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.