Codeforces_758_D_(区间dp)

本文介绍了一种使用区间动态规划解决从特定数制到十进制最小值转换的方法。通过将给定的数按位划分为多个段,并计算各段的最小值,最终得到整个数的最小十进制值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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[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<<j<<":"<<dp[j]<<endl;
        }
    }
    printf("%I64d\n",dp[len]);
    return 0;
}

 

转载于:https://www.cnblogs.com/jasonlixuetao/p/6413334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值