Educational Codeforces Round 16 E. Generate a String

本文介绍了一种通过动态规划解决的问题:如何用最短时间生成包含n个字符'a'的文件。具体操作包括插入或删除字符'a'以及复制粘贴整个文件内容。

zscoder wants to generate an input file for some programming competition problem.

His input is a string consisting of n letters 'a'. He is too lazy to write a generator so he will manually generate the input in a text editor.

Initially, the text editor is empty. It takes him x seconds to insert or delete a letter 'a' from the text file and y seconds to copy the contents of the entire text file, and duplicate it.

zscoder wants to find the minimum amount of time needed for him to create the input file of exactly n letters 'a'. Help him to determine the amount of time needed to generate the input.

Input

The only line contains three integers nx and y (1 ≤ n ≤ 1071 ≤ x, y ≤ 109) — the number of letters 'a' in the input file and the parameters from the problem statement.

Output

Print the only integer t — the minimum amount of time needed to generate the input file.

Examples
input
8 1 1
output
4
input
8 1 10
output
8

这题的大意是一个人要输出n个‘a’,他打出或删除一个‘a’需要x秒钟,复制粘贴要y秒钟,问你要输入n个字符‘a’最少要多长时间。

这题其实是一个不难的DP,分奇偶进行DP就行,一个转态只和前面的某两个状态有关。


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

using namespace std;

long long dp[10000010];
int main(void)
{
    int n,i,j,x,y;
    while(scanf("%d%d%d",&n,&x,&y)==3)
    {
        dp[0] = 0;
        for(i=1;i<=n;i++)
        {
            if(i % 2 == 1)
                dp[i] = min(dp[i-1]+x,dp[(i+1)/2]+x+y);
            else
                dp[i] = min(dp[i-1]+x,dp[i/2]+y);
        }
        printf("%I64d\n",dp[n]);
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值