Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)

时间:2022-03-08 18:50:03
D. Ability To Convert
time limit per test

1 second

Cmemory 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.

一个星期不更博了~这场CF我没打,其实是很水的一场,不过这道题解法想到too simple,就是细节特别讨厌,会导致WA声一片。。。

题意:给你N进制与对应的数字,问转化成10进制最小是多少?

解析:一眼贪心,倒着每次尽可能取较多的数,其实证明也是很简单的(略=懒的写),不过要注意前导0问题,细节题啊~最讨厌这种带细节题的场了。DP也可以,可能DP在细节上处理的不会很烦,我这里给出贪心AC代码。

#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
using namespace std; long long n,ans;
int l;
string k; long long powx(long long x,int y)
{
if(y==0)return 1;
long long s=powx(x,y/2);
if(y%2)return s*s*x; else return s*s;
} int main()
{
scanf("%I64d",&n);
long long t=n;
while(t>0)
{
t/=10LL;
l++;
}
cin>>k; int p=0,v=0,last=k.size()-1;
long long sum=0;
for(int i=k.size()-1;i>=0;i--)
{
long long t=sum;
sum+=powx(10,v)*(k[i]-48);
if(sum<n && v<l)v++;
else
{
int kkk=i;
if(k[i+1]=='0')
{
int ss=0;
while(k[++i]=='0')
{
ss++;
if(i==last)break;
}
i--;
}
ans+=powx(n,p)*t;
sum=k[i]-48;
v=1;
p++;
last=i;
}
}
ans+=powx(n,p)*sum;
printf("%I64d\n",ans);
return 0;
}