D. 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 kcontains 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上莫名其妙的变了。。。
1 //2016.01.21 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cstring> 6 #define ll long long 7 8 using namespace std; 9 10 int fun_len(ll a) 11 { 12 ll n = 0; 13 while(a) 14 { 15 n++; 16 a/=10; 17 } 18 return n; 19 } 20 21 ll pow(ll a, ll b) 22 { 23 ll ans = 1; 24 while(b) 25 { 26 if(b&1)ans *= a; 27 a*=a; 28 b>>=1; 29 } 30 } 31 32 int main() 33 { 34 ll cnt, dig[65]; 35 ll n; 36 string k; 37 while(cin>>n>>k) 38 { 39 cnt = 0; 40 ll tmp = n, base; 41 int len_of_n = 0, high; 42 while(tmp) 43 { 44 len_of_n++; 45 high = tmp%10; 46 tmp/=10; 47 } 48 memset(dig, 0, sizeof(dig)); 49 tmp = 0, base = 1; 50 for(int i = k.length()-1; i >= 0; i--) 51 { 52 if(k[i]=='0') 53 { 54 int pos = i; 55 while(k[pos] == '0')pos--; 56 int num_of_zero = i-pos; 57 if(tmp!=0 && ((k[pos]-'0')*pow(10, num_of_zero+fun_len(tmp))+tmp>=n))dig[cnt++] = tmp; 58 else if(tmp!=0){ 59 tmp = (k[pos]-'0')*pow(10, num_of_zero+fun_len(tmp))+tmp; 60 base = pow(10, fun_len(tmp)); 61 i = pos; 62 if(i==0){dig[cnt++] = tmp; break;} 63 continue; 64 } 65 if(num_of_zero<len_of_n-1){ 66 tmp = (k[pos]-'0')*pow(10, num_of_zero); 67 base = pow(10, num_of_zero+1); 68 } 69 else{ 70 tmp = (k[pos]-'0')*pow(10, len_of_n-1); 71 int zero = num_of_zero-len_of_n+1; 72 if(tmp>=n){ 73 tmp/=10; 74 zero++; 75 } 76 for(int j = 0; j < zero; j++)dig[cnt++] = 0; 77 base*=10; 78 } 79 if(pos == 0){dig[cnt++] = tmp; break;} 80 i = pos-1; 81 if(k[i] == '0'){i++;continue;} 82 } 83 if(tmp+base*(k[i]-'0')>=n) 84 { 85 dig[cnt++] = tmp; 86 tmp = k[i]-'0'; 87 base = 10; 88 }else{ 89 tmp += base*(k[i]-'0'); 90 base*=10; 91 } 92 if(i==0)dig[cnt++] = tmp; 93 } 94 ll ans = 0; 95 base = 1; 96 for(int i = 0; i < cnt; i++) 97 { 98 ans += base*dig[i]; 99 base*=n; 100 } 101 cout<<ans<<endl; 102 } 103 104 return 0; 105 }