从后往前贪心就好了。各种各样0的情况太BT了。。
(各种爆long long,f**k)
1 #include<bits/stdc++.h> 2 #define LL long long 3 #define N 100005 4 #define lowbit(x) x&(-x) 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 10 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 11 return x*f; 12 } 13 LL n,m=1,ans; 14 string s; 15 LL get(int l, int r)// int boom!! 16 { 17 LL sum=0; 18 for (int i=l; i<=r; i++) 19 { 20 sum=sum*10+s[i]-'0'; 21 if (sum>n) break; 22 } 23 return sum; 24 } 25 int main() 26 { 27 n=(LL)ra(); cin>>s; 28 int len=s.length(); 29 int last=len-1,i=len-1; 30 while (i!=-1) 31 { 32 if (s[i]!='0' && get(i,last)<n) 33 { 34 i--; 35 if (i==-1) 36 { 37 ans+=m*get(0,last); 38 break; 39 } 40 continue; 41 } 42 if (s[i]=='0') 43 { 44 int j=i; 45 while (j!=-1 && get(j,last)<n && s[j]=='0') j--; 46 while (s[last]=='0' && get(j,last)>=n) m=m*n,last--; 47 if (s[j]!='0' && get(j,last)<n) i=j; 48 if (s[j]!='0' && get(j,last)>=n) 49 { 50 ans+=m*get(i,last); 51 last=i; 52 m=m*n; 53 } 54 } 55 if (s[i]!='0' && get(i,last)>=n) 56 { 57 ans+=m*get(i+1,last); 58 last=i; 59 m*=n; 60 } 61 } 62 cout<<ans; 63 return 0; 64 }