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可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- 分析Linux内核的启动过程
第一章 环境 Ubuntu 14.10 Linux Kernel 3.18.6 第二章 代码及调试过程 环境搭建与内核准备: cd ~/LinuxKernel/ wget https://www.ke ...
- 步长为float
import numpy as np for i in np.arange(0.005, 0.05, 1): print(i)
- Bootstrap 过渡效果 transition.js源码分析
前言: 阅读建议:去github下载一个完整dom然后把,本篇代码复制进去然后运行就好了以地址 Bootstrap 自带的 JavaScript 插件的动画效果几乎都是使用 CSS 过渡实现的,那么判 ...
- Vijos 1523 贪吃的九头龙 【树形DP】
贪吃的九头龙 背景 安徽省芜湖市第二十七中学测试题 NOI 2002 贪吃的九头龙(dragon) Description:OfficialData:OfficialProgram:Converted ...
- POJ3675 Telescope 圆和多边形的交
POJ3675 用三角剖分可以轻松搞定,数据也小 随便AC. #include<iostream> #include<stdio.h> #include<stdlib.h ...
- linux下的C语言开发(静态库/动态库)
动态链接库不是Linux独有的特性,在windows下面也存在这样的特性.一般来说,windows下面的动态连接库是以*.dll作为结尾的,而linux下面的动态连接库是以*.so结尾的.和静态链接库 ...
- linux驱动编写(Kconfig文件和Makefile文件)
在Linux编写驱动的过程中,有两个文件是我们必须要了解和知晓的.这其中,一个是Kconfig文件,另外一个是Makefile文件.如果大家比较熟悉的话,那么肯定对内核编译需要的.config文件不陌 ...
- Python基础Web服务器案例
一.WSGI 1.PythonWeb服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI) 是Python应用程序或框架和Web服务器之间的一种接口, ...
- 17_activity任务栈和启动模式
夜神安卓模拟器. 如果从第一个页面开启另外一个页面,只要不去调finish()方法咱们上一个页面它是不会退出的,它会留在底下.留在底下的话它设计了这样一个模式就是为了维护一个比较好的用户体验,你的仪器 ...
- Java多线程系列一——Java实现线程方法
Java实现线程的两种方法 继承Thread类 实现Runnable接口 它们之间的区别如下: 1)Java的类为单继承,但可以实现多个接口,因此Runnable可能在某些场景比Thread更适用2) ...