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#include #include #include using namespace std;#define LL long longchar num[65];LL dp[65];int main(){ int n; scanf("%d%s",&n,num+1); int len=strlen(num+1); for(int i=1;i<=len;i++) dp[i]=LLONG_MAX; dp[0]=0; for(int i=1; i<=len; i++) { LL now=0; for(int j=i; j<=len; j++) { now=now*10+num[j]-'0'; if(num[i]=='0'&&j>i) break; if(now>=n) break; if(1.0*dp[i-1]*n+now>1e18) //这里一定要*1.0,否则会wa,估计是精度问题 break; dp[j]=min(dp[j],dp[i-1]*n+now); //cout< <<":"< <