https://i2.wp.com/eblogarithm.com/wp-content/uploads/2017/01/Codeforces-Round-392-Div-2-problem-D-Ability-To-Convert-Solution-in-CC1.png?fit=640%2C360

Codeforces Round #392 (Div. 2), problem: (D) Ability To Convert Solution in C/C++

#include <stdio.h>
#include <string.h>

int main() {
static char s[128];
static long long dp[128];
int n, i, j, l;
long long a, b;

scanf(“%d”, &n);
scanf(“%s”, s);
l = strlen(s);
for (i = 0; i < l; i++)
dp[i] = 1e18 + 1;
for (i = 0; i < l; i++)
for (j = i, a = s[i] – ‘0’, b = 1; j >= 0 && a < n && b < n; j–) {
double x = (j == 0 ? (double) a : (double) dp[j – 1] * n + a);

if ((a >= b || i – j + 1 == 1) && dp[i] > x)
dp[i] = x;
b *= 10;
a += j == 0 ? 0 : b * (s[j – 1] – ‘0’);
}
printf(“%lld\n”, dp[l – 1]);
return 0;
}

(Visited 198 times, 1 visits today)



There are no comments

Add yours

Leave a Reply

%d bloggers like this: