题目链接:

E. Generate a String

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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 ≤ 107, 1 ≤ 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

题意:

现在添加一个或者删除一个a需要x,复制一次需要y,问正好有n个a至少需要多少时间;

思路:

dp[i]表示正好i个a需要的最少时间,i为偶数的时候dp[i]=min{dp[i-1]+x,dp[i+1]+x,dp[i/2]+y}
i为奇数的时候dp[i]=min{dp[i-1]+x,dp[i+1]+x};
这样写的状态转移方程没法转移啊,把这些式子组合考虑一下可以知道:
当确定了i的dp[i]的时候,可以得到dp[i*2]的估计值为dp[i]+y;这就是得到了偶数的估计值,
所以i%2==1时dp[i]=min{dp[i-1]+x,dp[i+1]+x};i%2==0时dp[i]=min{dp[i-1]+x,dp[i/2]+y}; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e7+10;
const int maxn=1e3+20;
const double eps=1e-12; LL dp[N],x,y;
int n;
int main()
{ read(n);read(x);read(y);
dp[1]=x;dp[2]=x+y;
for(int i=2;i<=n;i++)
{
if(i&1)dp[i]=min(dp[i-1]+x,dp[i+1]+x);
else dp[i]=min(dp[i-1]+x,dp[i/2]+y);
dp[2*i]=dp[i]+y;
}
cout<<dp[n]<<endl;
return 0;
}

  

最新文章

  1. 搭建web框架手册(一)
  2. Codeforces Round #371 (Div. 2) C
  3. js-JavaScript高级程序设计学习笔记13
  4. IOS 网络浅析-(六 网络图片获取之三方SDWebImage)
  5. mysql字段额外属性,除去字段类型外的其他属性
  6. Pentium II paging mechanism
  7. 【Origin】答友朋关切书
  8. js的相关验证
  9. [LeetCode]题解(python):095-Unique Binary Search Trees II
  10. 部分PC端安卓管理器使用强行断开重连的方法来连接手机,容易丢书数据,损坏数据
  11. Android替换APP字体 — Typeface
  12. redis的删除库应用(linux)
  13. nginx+tomcat+session共享(转)
  14. [USACO12JAN]爬山Mountain Climbing
  15. Win32线程安全问题.同步函数
  16. Thinkphp3.2+PHPQRCode 二维码生成示例
  17. 珍藏40个android应用源码分享
  18. nw.js node-webkit系列(15)如何使用内部模块和第三方模块进行开发
  19. SSL虚拟主机安全方案
  20. Spring-导入和混合配置

热门文章

  1. ahjesus C# Flags 位域略说
  2. SQL Server Insert时开启显式事务
  3. css清除浮动定位造成的异常
  4. art-template引擎模板
  5. ArcEngine尝试读取或写入受保护的内存
  6. SAP SD 需求类别确定
  7. 配置SharePoint使用ADFS
  8. linux服务器如何设置目录权限,让开发只能在测试目录下开发,不在线上目录上开发
  9. 可展开的列表组件——ExpandableListView深入解析
  10. Spring(四)Bean注入方试