zoukankan      html  css  js  c++  java
  • Codeforces_758_D_(区间dp)

    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
    ",dp[len]);
        return 0;
    }
  • 相关阅读:
    android的ScaleGestureDetector缩放类详解
    ScaleGestureDetector缩放view
    setBackgroundDrawable和setBackgroundColor的用法
    Android安卓开发中图片缩放讲解
    android 比较靠谱的图片压缩
    Android ContentProvider和Uri详解 (绝对全面)
    android:intent flags
    Activity的加载模式及Intent.setFlags
    关于final中的几个忽略的点的再次阐述
    关于继承的一些问题
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6413334.html
Copyright © 2011-2022 走看看