Codeforces Round #473 (Div. 2), problem: (D) Mahmoud and Ehab and another array construction task Solution In C/C++

#include<stdio.h>
int vi[10000001]={0};
int main(void)
{
int n, a, now = 2;
int ans;
scanf(“%d”, &n);
int flag = 0;
int i,j,k;
for (i = 1; i <= n; i++)
{
scanf(“%d”, &a);
ans = now;
if (!flag&&a>ans)
ans = a;
while (vi[ans])
ans++;
printf(“%d “, ans);
if (ans>a)
flag = 1;
for (j = now; j*j <= ans; j++)
{
if (ans%j)
continue;
while (ans%j == 0)
ans /= j;
if (!vi[j])
for (k = j; k<10000001; k += j)
vi[k] = 1;
}
if (ans != 1)
for ( j = ans; j<10000001; j += ans)
vi[j] = 1;
while (vi[now])
now++;
}
return 0;
}

#include<stdio.h>
int vi[10000001]={0};
int main(void)
{
int n, a, now = 2;
int ans;
scanf(“%d”, &n);
int flag = 0;
int i,j,k;
for (i = 1; i <= n; i++)
{
scanf(“%d”, &a);
ans = now;
if (!flag&&a>ans)
ans = a;
while (vi[ans])
ans++;
printf(“%d “, ans);
if (ans>a)
flag = 1;
for (j = now; j*j <= ans; j++)
{
if (ans%j)
continue;
while (ans%j == 0)
ans /= j;
if (!vi[j])
for (k = j; k<10000001; k += j)
vi[k] = 1;
}
if (ans != 1)
for ( j = ans; j<10000001; j += ans)
vi[j] = 1;
while (vi[now])
now++;
}
return 0;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

We Tested Viral Kitchen Gadgets Here Is What You Must Know

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

NBA Trade Rumors: Potential RFA Targets, Fallout for Jalen Duren, & the Big Board

The NBA landscape is constantly shifting, and recent contract updates regarding Detroit Pistons center Jalen Duren have created significant ripples across the league’s transactional...

The Ultimate Viral Kitchen Gadgets Guide You Didnt Know You Needed

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

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!