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;
}

#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;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Everything You Need to Know About Viral Kitchen Gadgets Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Liberal MP’s travel documents show claims he voted on bills while in China are false

Recent online claims accusing Canadian Member of Parliament Shaun Chen of violating parliamentary voting rules have been debunked following the release of travel records...

Why Viral Kitchen Gadgets Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!