Codeforces_758_D_(区间dp)
1 second
256 megabytes
standard input
standard output
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311(475 = 1·162 + 13·161 + 11·160). Alexander lived calmly until he tried to convert the number back to the decimal number system.
Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k.
The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n.
Alexander guarantees that the answer exists and does not exceed 10^18.
The number k doesn't contain leading zeros.
Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem.
13
12
12
16
11311
475
20
999
3789
17
2016
594
In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·130 or 15 = 1·131 + 2·130.
题意:给定一个n数制,和一个该数制下的一个数(最多60位)(如16进制中的A用10表示,B用11表示。。。以此类推,即所有位均为数字)。求
将其转化为十进制的最小值。
乍一看以为比较简单,然后模拟了一下,发现连续的0不太好处理,把连续0处理好后,交一发发现超时,然后放弃了模拟。看题解,
用了区间dp,把这道题变得非常简单。
第一道区间dp。。。弱鸡。。。
思路:将给定了这个数的所有位划分成若干段,求不同的划分方式下这些段的和。要求得[1,len]段的最小值,需要一次求得之前
更小的段的最小值,这是重叠子问题。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std;
#define LL long long
char num[];
LL dp[]; int main()
{
int n;
scanf("%d%s",&n,num+);
int len=strlen(num+);
for(int i=;i<=len;i++)
dp[i]=LLONG_MAX;
dp[]=;
for(int i=; i<=len; i++)
{
LL now=;
for(int j=i; j<=len; j++)
{
now=now*+num[j]-'';
if(num[i]==''&&j>i)
break;
if(now>=n)
break;
if(1.0*dp[i-]*n+now>1e18) //这里一定要*1.0,否则会wa,估计是精度问题
break;
dp[j]=min(dp[j],dp[i-]*n+now);
//cout<<j<<":"<<dp[j]<<endl;
}
}
printf("%I64d\n",dp[len]);
return ;
}
Codeforces_758_D_(区间dp)的更多相关文章
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
- Uva 10891 经典博弈区间DP
经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- Jenkins 使用
Jenkins 安装 Jenkins是用Java语言开发的系统,首先要确定服务器上已经安装JDK或者JRE. 安装方式一 直接运行java –jar Jenkins.war,在浏览器中输入 http: ...
- 用C# (.NET Core) 实现抽象工厂设计模式
用C# (.NET Core) 实现抽象工厂设计模式 本文的概念性内容来自深入浅出设计模式一书. 上一篇文章讲了简单工厂和工厂方法设计模式 http://www.cnblogs.com/cgzl/ ...
- PB MD5
适用环境:powerbuilder 10.0以后的版本号 window server2003以后的測试可用 PB也能够调用系统自带的DLL 实现MD5 当中md5file对大附件的处理速度也比第三方 ...
- jdk、jre、spring、java ee、java se
1 java se.java ee和java me 这三个是java的标准.java se是根本,java ee建立在java se上,用于server.java me是java se的子集,用于终端 ...
- ReSharper warns: “Static field in generic type”
http://stackoverflow.com/questions/9647641/resharper-warns-static-field-in-generic-type It's fine to ...
- 【IOI2013】【Bzoj3246】Dreaming
http://www.lydsy.com/JudgeOnline/problem.php?id=3246 中文题面 天地之初,世界尚在遥远的梦想之中. Serpent(水蛇)生活的地方有N个水坑,编号 ...
- bzoj2157 旅游——LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 仍然是LCT模板题~ 不过有一些需要注意的地方,点和边的区分,0号点的 mx 和 mn ...
- 使用IntelliJ IDEA 创建Maven项目(入门)
一. 下载Maven 下载地址:http://maven.apache.org/download.cgi tar.gz压缩格式用于unix操作系统,而zip用于windows的操作系统,但在windo ...
- 【Learning】多项式的一些东西
FFT 坑 NTT 将\(FFT\)中的单位复数根改成原根即可. 卡常版NTT模版 struct Mul { int Len; int wn[N], Lim; int rev[N]; inline v ...
- bzoj 1572: [Usaco2009 Open]工作安排Job【贪心+堆】
先按照时间顺序加,价值塞进小根堆里,碰到不合法情况就从堆里减去 #include<iostream> #include<cstdio> #include<queue> ...