Codeforces Round #392 (Div. 2)-D. Ability To Convert(贪心+dp)

来源:互联网 发布:淘宝 中老年男装毛衣 编辑:程序博客网 时间:2024/05/21 19:30

记录一个菜逼的成长。。

题目链接
题目大意:
给你一个进制数n,和一个不超过60位的数k。
以超过十的数代替字母。
问转换成十进制的数最小是多少。

PS:果然dp的题懵逼时刻占大多数。

dp[i] := 以第i个数结尾的最小的十进制数。
i枚举起点,j枚举起点往后不超过n的数,那么dp[j+1] = min(dp[j+1],dp[i]*n+x)
比如3^3+2*3^2+3^1 = ((3+2)*3+1)*3
如果起点为0,那么dp[i+1] = min(dp[i+1],dp[i]*n)

#include <bits/stdc++.h>using namespace std;#define cl(a,b) memset(a,b,sizeof(a))typedef long long LL;const int maxn = 100 + 10;LL inf = (LL)1e18 + 10,dp[maxn];char str[maxn];int n;LL mul(LL a,LL b){    if(a >= inf / b)return inf;    return a*b;}LL sum(LL a,LL b){    if(a+b >= inf)return inf;    return a + b;}int main(){    ios::sync_with_stdio(false);    cin.tie(0);    while(cin>>n>>str){        fill(dp,dp+maxn,inf);        dp[0] = 0;        int len = strlen(str);        for( int i = 0; i < len; i++ ){            if(str[i] != '0'){                LL x = 0;                for( int j = i; j < len; j++ ){                    x = x * 10 + str[j] - '0';                    if(x >= n)break;                    dp[j+1] = min(dp[j+1],sum(mul(dp[i],n),x));                }            }            else {                dp[i+1] = min(dp[i+1],mul(dp[i],n));            }        }        cout<<dp[len]<<endl;    }    return 0;}
0 0