codeforces 758 D

n进制

n进制数

求一个最小的十进制数上面那个数可以变成

一眼看到就是不会   看题解 

是区间DP

dp[i] 从前处理到i位的最小的数

dp[i]=min(dp[i],dp[i-1]*n+now);

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<set>
#include<string>

using namespace std;
typedef long long LL;

#define inf  2000000000000000000
#define MAXN 65
char str[MAXN];
LL   dp[MAXN];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",str+1);
        int len=strlen(str+1);
        for(int i=1;i<=len;i++)
            dp[i]=inf;
        dp[0]=0;
        for(int i=1;i<=len;i++)
        {
            LL now=0;
            for(int j=i;j<=len;j++)
            {
                now=now*10+str[j]-'0';
                if(str[i]=='0'&&j>i)  //不能有00  01...  
                    break;
                if(now>=n)
                    break;
                if(1.0*dp[i-1]*n+now>2e18) //这个数<2e18  题目写了 
                    continue;
                dp[j]=min(dp[j],dp[i-1]*n+now);
            }
        }
        printf("%lld
",dp[len]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cherryMJY/p/6429014.html