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.
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.
Print the number x (0 ≤ x ≤ 1018) — the answer to the problem.
13 12
12
16 11311
475
20 999
3789
17 2016
594
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);
}
}