CodeForces 758 D Ability To Convert
Posted Schenker
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 758 D Ability To Convert相关的知识,希望对你有一定的参考价值。
题意:给你一个n进制的60位的数,但是由于Alexander只会写0->9,所以他就会用10来表示十而不是A(假设进制>10);
题解:模拟就好了,先走往前走进制的位数的倍数,再判断一下首位是不是0,或者这个数有没有大于等于进制,如果有就不行,就要往后走,走到一个非0开头的点,或者就是走到只有1个0的点,然后算出目前这段区间的答案加一个倍数就好了。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 4 #define LL long long 5 #define ULL unsigned LL 6 #define fi first 7 #define se second 8 #define pb push_back 9 #define lson l,m,rt<<1 10 #define rson m+1,r,rt<<1|1 11 #define max3(a,b,c) max(a,max(b,c)) 12 #define min3(a,b,c) min(a,min(b,c)) 13 typedef pair<int,int> pll; 14 const int INF = 0x3f3f3f3f; 15 const LL mod = (int)1e9+7; 16 const int N = 2e5 + 100; 17 char str[N]; 18 char cmp[N]; 19 LL ans = 0; 20 LL tmp = 0; 21 LL n = 0; 22 LL base = 1; 23 LL b = 1; 24 LL t; 25 vector<LL> vc; 26 int main(){ 27 scanf("%s", cmp); 28 int len1 = strlen(cmp); 29 for(int i = 0; i < len1; i++){ 30 n = n*10 + cmp[i] - ‘0‘; 31 } 32 scanf("%s", str); 33 if(str[0] == ‘0‘){ 34 cout << 0 << endl; 35 return 0; 36 } 37 int len2 = strlen(str); 38 for(int i = len2-1; i >= 0;){ 39 int j = i - len1 + 1; 40 if(j < 0) j = 0; 41 //cout << j <<‘ ‘<< i << endl; 42 if((j+len1-1 == i && strcmp(cmp,str+j) <= 0) || str[j] == ‘0‘){ 43 j++; 44 if(j >= i) j = i; 45 while(str[j] == ‘0‘ && j < i) j++; 46 t = 0; 47 for(int k = j; k <= i; k++) 48 t = t*10 + str[k] - ‘0‘; 49 ans += b*t; 50 b *= n; 51 } 52 else { 53 t = 0; 54 for(int k = j; k <= i; k++) 55 t = t*10 + str[k] - ‘0‘; 56 ans += b*t; 57 b *= n; 58 } 59 i = j - 1; 60 } 61 printf("%I64d", ans); 62 return 0; 63 }
以上是关于CodeForces 758 D Ability To Convert的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 758D Ability To Convert dp