CF758D Ability To Convert(贪心模拟)

CF758D Ability To Convert(贪心模拟)

题目描述

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 ≤ 109). The second line contains the integer k (0 ≤ k < 1060), 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 ≤ 1018) — the answer to the problem.

Examples

Input1

1312

Output1

12

Input2

1611311

Output2

475

Input3

20999

Output3

3789

Input4

172016

Output4

594

 

翻译及题意

亚历山大正在学习如何把十进制数字转换成其他进制,但是他不懂英文字母,所以他只是把数值按照十进制数字的方式写出来。这意味着他会用 10 代替英文字母 A。这样,他就会把十进制的 475 转换成十六进制的 11311(475=1·162+13·161+11·16^0)。亚历山大平静的生活着,直到有一天他试着把这些数字转换回十进制数字。

亚历山大记着他总是用较小的数字工作,所以他需要找到在 n 进制的基础下,用他的转换系统得出数字 k 的最小十进制数。 输入输出格式 输入格式:

第一行是一个正整数 n ( 2<=n<=10^9),第二行有一个正整数 k ,满足数字 k中包含不超过 60 个数值。第二行整数中的没每一个数字都严格小于 n。

亚历山大保证答案存在且不超过 10^18,数字 k 不含前导 0. 输出格式:

输出一个数字 x —— 问题的答案。

一串用十进制数字代替字母的n进制数字,组合方式不同最终在十进制下大小也不同,要求输出最小的数字

思路

可以用DP做,同时不断用贪心也满足条件

数字位数越多永远越大,所以尽可能让位数小,也就要求n进制某一位所含数字个数尽可能多

用字符串存储n进制数字,从后往前遍历(也就是n进制0次方那一位开始),每次经可能多的选(当然加起来不能大于n),找出最大能选的个数后,删除这些数(string中的erase()),再从尾找,直到数字选完(字符串为空s.size()==0

题中有分别要处理10进制n进制两种进制的数,10进制数是当前末尾选的数的大小(与n比较),会不断刷新,找到目标后加到n进制的最前面那一位。

注意

大致思路定下来后,先注意数据类型:答案不超过 10^18,因此要用long long存各个数据

此外设计到大数的幂运算,所以要自己编写快速幂函数

之后再考虑边界和特殊情况的细节问题

不同实现方法,细节处理也不相同,对于我的代码,有以下要注意

若n进制数字小于n,直接输出n进制数字

末尾为0且不能与前面的数合并为一位,就要独占一位

某一位数返回的结果是一个起始为0的数,就要不断往回找,选择第一位不为0的最大数

上一种特殊情况如果前导0过多,会爆long long,所以要判断0过多情况并及时退出

代码

#include <iostream>
#include<cstring>
using namespace std;
typedef unsigned long long LL;
LL n;
string k;
LL binaryPow(LL a,LL b)
{
    if(b==0) return 1;
    if(b%2==1) return a*binaryPow(a,b-1);
    else
    {
        LL mul=binaryPow(a,b/2);
        return mul*mul;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>k;
    LL ans=0;
    LL p_n=0;
    while(k.size())
    {
        LL p_10=0;
        LL End=0;
        LL i=k.size()-1;
        while(End+(k[i]-'0')*binaryPow(10,p_10)<n&&i>=0   &&p_10<10)
        {
​
            End+=(k[i--]-'0')*binaryPow(10,p_10++);
        }
        if(i<0)
        {
            ans+=End*binaryPow(n,p_n);
            cout<<ans;
            return 0;
        }
        if(k[i+1]=='0')
        {
​
            if(k[k.size()-1]=='0')
            {
                i=k.size()-1;
                End=0;
                p_10=1;
            }
​
            else
            {
                i++;
                while(k[i]=='0')
                {
                    i++;
                    p_10--;
                }
            }
​
        }
        ans+=End*binaryPow(n,p_n++);
        k.erase(k.end()-p_10,k.end());
    }
    cout<<ans;
    return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值