Codeforces Round #392(Div. 2) D Ability To Convert【贪心+谨慎】

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 ≤ 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
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进制的。

对应第二行输入的就是这个N进制数。

因为组合方式是很多的,我们需要找到最小的原来的十进制数是多大。


思路:


1、我们现在设定一次分段作为一个子段,那么最后一个子段就是其子段数字*N^0,倒数第二个子段就是其子段数字*N ^1............依次类推。

那么我们不难想到贪心的点:就是让子段个数尽可能的少。


2、既然是要让子段个数尽可能的少,那么我们的任务就是处理每一子段的数值尽量的接近N即可。

在整个N进制数中如果没有数字0的话,那么处理起来是相当方便的。

所以关键点在于谨慎数字0的处理方式。


3、那么整体思路还是比较好确定的:

①我们逆序扫.从最后一部分开始贪心分段。

②对于分段的最终位子,我们其实可以维护一个最后可行位子pre.维护最后一个不是数字0的可行位子(即数值还小于N的最后一个位子).

③假设我们当前扫到的数字是0,那么如果pre没有维护到,那么只能把一个0作为一个子段进行处理。

④细节比较多,说的比较乱,大家不妨参考代码,思路更为清晰的多。


4、不造有么有二笔在比赛过程中和窝一样把12也当做12进制数中的了。T T(查了半个小时错误啊)


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
char a[60000];
ll n;
ll judge(ll pos1,ll pos2)
{
    ll num=0;
    for(ll i=pos1;i<=pos2;i++)
    {
        num=num*10+a[i]-'0';
    }
    if(num>=n)return -1;
    else return num;
}
ll powwww(ll a,ll b)
{
    ll ans=1;
    for(ll i=1;i<=b;i++)ans*=a;
    return ans;
}
int main()
{
    while(~scanf("%I64d",&n))
    {
        scanf("%s",a);
        ll len=strlen(a);
        ll tmp=0;
        ll ans=0;
        ll cont=0;
        for(ll i=len-1;i>=0;i--)
        {
            ll prenum=-1;
            ll pre=-1;
            for(ll j=i;j>=0;j--)
            {
                if(judge(j,i)>=0)
                {
                    if(a[j]!='0')
                    {
                        pre=j;
                        prenum=judge(j,i);
                    }
                }
                else break;
            }
            if(pre==-1)
            {
                if(a[i]=='0')
                cont++;
                continue;
            }
            i=pre;
            ans+=prenum*powwww(n,cont);
            cont++;
        }
        printf("%I64d\n",ans);
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值