D. Ability To Convert
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem.

Examples
input
13
12
output
12
input
16
11311
output
475
input
20
999
output
3789
input
17
2016
output
594
Note

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)的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. 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 ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. react jsx 常见问题

    问题一: Expected to return a value in arrow function 解决方案: 修改后: // 使用 store return ( <div> <h1 ...

  2. 神马都是浮云,unity中自己写Coroutine协程源代码

    孙广东   2014.7.19 无意之间看到了,Unity维基上的一篇文章,  是关于自己写协程的介绍. 认为非常好,这样能更好的了解到协程的执行机制等特性.还是不错的. 原文链接地址例如以下: ht ...

  3. 单一责任原则(SRP)

    1.就一个类而言,应该仅有一个引起它变化的原因. 2.在SRP中定义职责为:“变化的原因”.  如果你想到多个动机去改变这个类,那这个类就有多个职责

  4. hbase shell经常使用命令

    hbase经常使用命令 /usr/local/cloud/hbase/bin/hbase shell 用shell来连接hbase exit 退出hbase shell version 查看hbase ...

  5. Eclipse离线安装Emmet插件

    Eclipse离线安装Emmet插件 近期发现了一个写前端代码很好的一个东西,一个叫做Emmet的工具,这个工具使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,前身就是大 ...

  6. HTTP要点概述:五,HTTP的无状态性,持久连接,Cookie

    一,HTTP的无状态性: HTTP 是一种不保存状态,无状态(stateless)协议.HTTP 协议自身不对请求和响应之间的通信状态进行保存.也就是说在 HTTP 这个级别,协议对于发送过的请求或响 ...

  7. HDU2072单词数

    #include<iostream> #include<set> #include<sstream> using namespace std; int main() ...

  8. 利用JFreeChart生成组合图表 (8) (转自 JSP开发技术大全)

    利用JFreeChart生成组合图表 (8) (转自 JSP开发技术大全) 14.8 利用JFreeChart生成组合图表  实例位置:光盘\mingrisoft\14\dxyy\02 通过JFree ...

  9. 23. Ext xtype : "combo" 下拉选择框

    转自:https://blog.csdn.net/majishushu/article/details/52601161

  10. VS2015 framework4.5代码提示英文切换为中文

    输入下面的地址,复制里面所有的文件 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 ...