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可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- Spring MVC 数据验证——validate编码方式
1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...
- ubuntu系统下,下载Android源码
博主电脑的Windows的 是用的vmware装的ubuntu kylin.vmware随便找都能找到尽量安装最新版本号的,而unbuntu在官方站点下载即可了,安装vmware的时候建议关闭安全类 ...
- 用block变量来对字符数组对象进行排序
<span style="font-size:18px;">降序排序</span> <span style="font-size:18px; ...
- ramfs、rootfs和initramfs【转】
ramfs, rootfs and initramfs October 17, 2005 Rob Landley <rob@landley.net> =================== ...
- 容器ArrayList原理(学习)
一.概述 动态数组,容量能动态增长,元素可以为null,用数组存储,非线程同步(vector线程同步) 每个 ArrayList 实例都有一个容量,该容量是指用来存储列表元素的数组的大小,自动增长(默 ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- ViewControl的size设为freeform
freeform的用处是让你写一些不标准的view,比如说自定义一个cell,或者自己写一个小的VIEW,freeform的XIB是可以自己拖拽更改大小的
- maven中的三种工程,以及在idea中构建父子工程。
1.pom工程:用在父级工程或聚合工程中.用来做jar包的版本控制.主要是定义POM文件,将后续各个子模块公用的jar包等统一提出来,类似一个抽象父类 2.war工程:将会打包成war,发布在服务器上 ...
- 短链接及关键字过滤ac自动机设计思路
=============:短链接设计思路:核心:将长字符转为短字符串并建立映射关系,存储redis中.1.使用crc32转换为Long 2.hashids将long encode为最短字符串.作为短 ...
- 【js】再谈移动端的模态框实现
移动端模态框的机制因为与PC的模态框机制一直有所区别,一直是许多新人很容易踩坑的地方,最近笔者作为一条老咸鱼也踩进了一个新坑中,真是平日里代码读得太粗略,故而写上几笔,以儆效尤. 故事的起因是这样的, ...