美文网首页
codeforces #392 div2 D. Ability

codeforces #392 div2 D. Ability

作者: FD丶grass | 来源:发表于2017-01-22 00:27 被阅读0次

    题目

    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 = 116^2 + 1316^1 + 11*16^0). 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 ≤ 1e9). The second line contains the integer k (0 ≤ k < 1e60), 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 1018.

    The number k doesn't contain leading zeros.

    Output

    Print the number x (0 ≤ x ≤ 1e18) — the answer to the problem.

    分析

    给你一个n进制的数,在题目条件下转换成最小的10进制数,从后面贪心,取最大的小于n的数,0的时候需要单独考虑一下。写跪了不少次,终于找到一种比较不错的写法。

    ac代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <vector>
    typedef unsigned long long ull;
    using namespace std;
    ull trans(string str){
        ull ans = 0;
        for(int i = 0; i < str.size(); i++){
            ans *= 10;
            ans += (str[i] - '0');
        }
        return ans;
    }
    int main(){
        vector <ull> a; 
        ull n, ans = 0;
        string base, str;       
        cin >> base >> str;
        n = trans(base);
        int nlen = base.size(), slen = str.size();
        int i = slen - 1;
        while(i >= 0){
            int j = max(i - nlen + 1, 0);
            if(trans(string(str, j, i + 1 - j)) >= n){
                j++;
            }
            while(j != i && str[j] == '0') j++;
            ull temp = trans(string(str, j, i + 1 - j));
            a.push_back(temp);
            i = j - 1;
        }
        for(int i = a.size() - 1; i >= 0; --i){
            ans *= n;
            ans += a[i];
        }
        cout << ans;
        return 0;
    } 
    

    相关文章

      网友评论

          本文标题:codeforces #392 div2 D. Ability

          本文链接:https://www.haomeiwen.com/subject/wobnbttx.html