[CodeForces-758D] 区间dp

来源:互联网 发布:简单的c语言程序题目 编辑:程序博客网 时间:2024/06/16 07:43
Input1611311Output475Input20999Output3789Input172016Output594

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.

参考https://vjudge.net/solution/10117143和http://www.cnblogs.com/TreeDream/p/6322755.html
题意:一个n进制下的数k,其中k不会用字母,如果有A就用10代替了。求k这个数对应的,在10进制下最小的数。

#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const ll INF=(1LL<<62)-1;char s[105];ll dp[105];int main(){    int n;    scanf("%d%s",&n,s+1);    int len=strlen(s+1);    for(int i=0; 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+s[j]-'0';            if(now>=n)break;            if(s[i]=='0' && j>i)break;            if(1.0*dp[i-1]*n+now>2e18)continue;            dp[j]=min(dp[j],dp[i-1]*n+now);        }    }    printf("%lld",dp[len]);    return 0;}
原创粉丝点击