Codeforces758D Ability To Convert

来源:互联网 发布:期刊论文数据造假 编辑:程序博客网 时间:2024/06/05 07:39

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
1312
output
12
input
1611311
output
475
input
20999
output
3789
input
172016
output
594
Note

In the first example 12 could be obtained by converting two numbers to the system with base 1312 = 12·130 or 15 = 1·131 + 2·130.


——————————————————————————————————

给出n进制和一个对应的n进制的数,问最小换成多大的十进制数。

贪心处理,先倒序,从最小位处理每次尽量取较多的位,注意前导零,题目不允许出现前导零。



#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <stack>#include <set>#include <map>#include <string>using namespace std;long long mypow(long long a,long long b){    long long ans=1;    for(int i=0; i<b; i++)    {        ans*=a;    }    return ans;}int main(){    char s[100];    long long n,a;    while(~scanf("%I64d %s",&n,s))    {        int k=strlen(s);        for(int i=0; i<k/2; i++)        {            swap(s[i],s[k-1-i]);        }        long long  m=n;        int t=0;        while(m>0)        {            m/=10;            t++;        }        long long kk=0,as=0,pw=0,tot=0,w[1000],ans=0;        for(int i=0; i<k; i++)        {            as+=(s[i]-'0')*mypow(10,kk);            kk++;            if(kk==t)            {                while(s[i]=='0'&&kk>1)                {                    i--;                    kk--;                }                if(as<n)                w[tot++]=as;                else                {                    w[tot++]=as-mypow(10,kk-1)*(s[i]-'0');                    kk--;                    i--;                    while(s[i]=='0'&&kk>1)                {                    i--;                    kk--;                }                }                as=0;                kk=0;            }        }         w[tot++]=as;        for(int i=0;i<tot;i++)         {             ans+=w[i]*mypow(n,pw);             pw++;         }        printf("%I64d\n",ans);    }    return 0;}


0 0
原创粉丝点击