http://codeforces.com/contest/758/problem/D
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进制下给你一个数,要你转换成最小的十进制数;
思路:贪心;
从后往前低位贪心选取的数尽量大。
要注意数字中有0的时候,如果不能和后面加前面不为0的数组在一起,将后面先组起来,然后在判断当前0时候能和前面不为0的组,不能单独成一位。
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 char a[100]; 5 LL quick(LL n,LL m); 6 LL ac[100]; 7 int main(void) 8 { 9 LL n; 10 scanf("%lld",&n); 11 scanf("%s",a); 12 int l = strlen(a); 13 LL sum = 0; 14 LL ak = 1; 15 if(a[0] == '0') 16 printf("0\n"); 17 else 18 {int cn = 0; 19 for(int i = l-1; i >= 0;) 20 { 21 if(a[i]!='0') 22 { 23 LL app = sum + (LL)(a[i]-'0')*ak; 24 if(app >= n) 25 { 26 ac[cn++] = sum; 27 ak = 1; 28 sum = 0; 29 } 30 else 31 { 32 sum = app; 33 ak*=10; i--; 34 } 35 } 36 else if(a[i] == '0') 37 { 38 LL app = sum; 39 int j; 40 for( j = i-1; j >= 0; j--) 41 { 42 if(a[j]!='0') 43 break; 44 } 45 int s; 46 for( s = i; s >= j; s--) 47 {if(ak >= n)break; 48 app = app+ak*(LL)(a[s]-'0'); 49 ak*=10; 50 } 51 if(app < n&& s == j-1) 52 { 53 sum = app; 54 i = j-1; 55 } 56 else 57 { 58 ac[cn++] = sum; 59 if(sum == 0) 60 i--; 61 sum = 0; 62 ak = 1; 63 } 64 } 65 } 66 ac[cn++] = sum; 67 sum = 1; 68 LL ask = 0; 69 for(int i = cn-1;i >= 0;i--) 70 { 71 ask *= n; 72 ask += ac[i]; 73 } 74 printf("%lld\n",ask);} 75 return 0; 76 }