# NCOPRIMEN - Editorial

Setter: Satyam
Tester: Abhinav Sharma, Aryan
Editorialist: Lavish Gupta

Easy

# PREREQUISITES:

Greatest Common Divisor

# PROBLEM:

An array B of length N (N \ge 2) is said to be good if the following conditions hold:

• For all 1 \leq i \leq N, 2 \leq B_i \leq 10^6
• gcd(B_{i-1},B_i)\neq1 for all i (2 \leq i \leq N).

You have an array A of length N (2 \leq A_i \leq 10^5). You want to make the array A good.

To do so, you can change atmost \left \lceil{\frac {2â‹…N} 3}\right \rceil elements of A.

Print the final array after changing A to a good array. If there are multiple possible final arrays, print any of them.

It is guaranteed that A can be made good after changing atmost \left \lceil{\frac {2â‹…N} 3}\right \rceil elements of A.

# CORNER CASE:

Corner Case

N = 6
A = \{ P_1, P_2, P_3, P_4, P_5, P_6 \} where P_i > 10^4, and each P_i is a prime number.

Make sure that your final array satisfies the condition A_i \leq 10^6, and you have not changed more than \left \lceil{\frac {2â‹…N} 3}\right \rceil elements of A.

# EXPLANATION:

This is one of the possible construction. Other solutions are also possible.

We are given that we can change atmost \left \lceil{\frac {2â‹…N} 3}\right \rceil elements of A. Also note that the new elements should satisfy the following constraint: 2 \leq B_i \leq 10^6.
One useful way to interpret it is: out of every 3 elements, we can change 2 of them, such that the conditions are satisfied. Let us try if we can achieve the above conditions through this interpretation.

Let us consider the first 4 elements of the array - A_1, A_2, A_3 and A_4 and let each of the A_i be a prime number for the sake of this discussion.

Now, because gcd(A_1, A_2) = 1, we would like to change one of the A_1 or A_2. Let us greedily choose A_2 and try to change it to some optimal value. Let A_2 = \lambda \cdot A_1, for some \lambda.

Again, we may have gcd(A_2, A_3) = 1 depending on the value of \lambda, so we need to suitably change A_3. Let the new value of A_3 be A_3' .

Because we want to change only 2 values out of every 3 values, we donâ€™t want to change our A_4. Hence, the following should also be satisfied: gcd(A_3', A_4) \neq 1. So, let us have A_3' = \lambda ' \cdot A_4.

So, the current array looks like \{A_1, \lambda \cdot A_1, \lambda ' \cdot A_4, A_4 , ...\}. The only condition that now needs to be satisfied is gcd(\lambda \cdot A_1, \lambda ' \cdot A_4) \neq 1.

Note that substituting \lambda = \lambda' = 2 will make the above property satisfied, as well as make sure that for each A_i, 2 \leq A_i \leq 10^6.

Hence, the final array would look like \{A_1, 2\cdot A_1, 2\cdot A_4, A_4, 2\cdot A_4, 2\cdot A_7, A_7, ...\}

# TIME COMPLEXITY:

O(N) for each test case.

# SOLUTION:

Setter's Solution

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
const ll INF_MUL=1e13;
#define pb push_back
#define mp make_pair
#define nline "\n"
#define f first
#define s second
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#ifndef ONLINE_JUDGE
#define debug(x) cerr<<#x<<" "; _print(x); cerr<<nline;
#else
#define debug(x);
#endif
void _print(ll x){cerr<<x;}
void _print(char x){cerr<<x;}
void _print(string x){cerr<<x;}
template<class T,class V> void _print(pair<T,V> p) {cerr<<"{"; _print(p.first);cerr<<","; _print(p.second);cerr<<"}";}
template<class T>void _print(vector<T> v) {cerr<<" [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";}
template<class T>void _print(set<T> v) {cerr<<" [ "; for (T i:v){_print(i); cerr<<" ";}cerr<<"]";}
template<class T>void _print(multiset<T> v) {cerr<< " [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";}
template<class T,class V>void _print(map<T, V> v) {cerr<<" [ "; for(auto i:v) {_print(i);cerr<<" ";} cerr<<"]";}
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_pset;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
const ll MOD=998244353;
const ll MAX=500500;
ll binpow(ll a,ll b,ll MOD){
ll ans=1;
a%=MOD;
while(b){
if(b&1)
ans=(ans*a)%MOD;
b/=2;
a=(a*a)%MOD;
}
return ans;
}
ll inverse(ll a,ll MOD){
return binpow(a,MOD-2,MOD);
}
void solve(){
ll n; cin>>n;
vector<ll> a(n+5);
for(ll i=1;i<=n;i++){
cin>>a[i];
}
a[n+1]=2;
for(ll i=1;i<=n;i++){
if((i%3)==1){
continue;
}
if((i%3)==2){
a[i]=a[i-1]*2;
}
else{
a[i]=a[i+1]*2;
}
}
for(ll i=1;i<=n;i++){
cout<<a[i]<<" ";
}
cout<<nline;
return;
}
int main()
{
ll test_cases=1;
cin>>test_cases;
while(test_cases--){
solve();
}
cout<<fixed<<setprecision(10);
cerr<<"Time:"<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
}

First Tester's Solution
/* in the name of Anton */

/*
Compete against Yourself.
Author - Aryan (@aryanc403)
Atcoder library - https://atcoder.github.io/ac-library/production/document_en/
*/

#ifdef ARYANC403
#else
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma GCC optimize ("-ffloat-store")
#include<bits/stdc++.h>
#define dbg(args...) 42;
#endif

// y_combinator from @neal template https://codeforces.com/contest/1553/submission/123849801
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template<class Fun> class y_combinator_result {
Fun fun_;
public:
template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }

using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) ((lli)(x).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"

typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403()
{
#ifdef ARYANC403
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end_time-start_time;
cerr<<"Time Taken : "<<diff.count()<<"\n";
#endif
}

long long readInt(long long l, long long r, char endd) {
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true) {
char g=getchar();
if(g=='-') {
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g&&g<='9') {
x*=10;
x+=g-'0';
if(cnt==0) {
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd) {
if(is_neg) {
x=-x;
}
assert(l<=x&&x<=r);
return x;
} else {
assert(false);
}
}
}
string readString(int l, int r, char endd) {
string ret="";
int cnt=0;
while(true) {
char g=getchar();
assert(g!=-1);
if(g==endd) {
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt&&cnt<=r);
return ret;
}
long long readIntSp(long long l, long long r) {
}
long long readIntLn(long long l, long long r) {
}
string readStringLn(int l, int r) {
}
string readStringSp(int l, int r) {
}

assert(getchar()==EOF);
}

vi a(n);
for(int i=0;i<n-1;++i)
return a;
}

// #include<atcoder/dsu>
//     vector<vi> e(n);
//     atcoder::dsu d(n);
//     for(lli i=1;i<n;++i){
//         e[u].pb(v);
//         e[v].pb(u);
//         d.merge(u,v);
//     }
//     assert(d.size(0)==n);
//     return e;
// }

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || ( a.X==b.X && a.Y <= b.Y ));   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
auto jt=m.find(x);
if(jt==m.end())         m.insert({x,cnt});
else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
auto jt=m.find(x);
if(jt->Y<=cnt)            m.erase(jt);
else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;
// const lli maxN = 1000000007L;

lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
lli m;
string s;
vi a;
//priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

int main(void) {
ios_base::sync_with_stdio(false);cin.tie(NULL);
// freopen("txt.in", "r", stdin);
// freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
lli sumN = 2e5;
while(T--)
{

sumN-=n;
for(lli i=0;i<n;i+=3){
if(i+1==n)
a[i]=2*a[i-1];
else
a[i]=2*a[i+1];
}
for(lli i=2;i<n;i+=3)
a[i]=2*a[i-1];
for(auto x:a)
cout<<x<<" ";
cout<<endl;
dbg(a);
fo(i,n-1)
assert(__gcd(a[i],a[i+1])!=1);
}   aryanc403();
return 0;
}


Second Tester's Solution
#include <bits/stdc++.h>
using namespace std;

/*
------------------------Input Checker----------------------------------
*/

long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
if(is_neg){
x= -x;
}

if(!(l <= x && x <= r))
{
cerr << l << ' ' << r << ' ' << x << '\n';
assert(1 == 0);
}

return x;
} else {
assert(false);
}
}
}
string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
}
long long readIntLn(long long l,long long r){
}
}
}

/*
------------------------Main code starts here----------------------------------
*/

const int MAX_T = 1e5;
const int MAX_N = 1e5;
const int MAX_SUM_LEN = 1e5;

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)

int sum_len = 0;
int max_n = 0;
int yess = 0;
int nos = 0;
int total_ops = 0;

const ll mod = 998244353;

ll po(ll x, ll n ){
ll ans=1;
while(n>0){
if(n&1) ans=(ans*x)%mod;
x=(x*x)%mod;
n/=2;
}
return ans;
}

void solve()
{

sum_len += n;
max_n = max(max_n, n);

int a[n+1];
a[n] = 1;

rep_a(i,1,n){
if(i%3==0) continue;
else if(i%3==1) a[i] = 2*a[i-1];
else a[i] = 2*a[i+1];
}

rep(i,n) cout<<a[i]<<" ";
cout<<'\n';

}

signed main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w" , stdout);
#endif
fast;

int t = 1;

for(int i=1;i<=t;i++)
{
solve();
}

assert(getchar() == -1);
assert(sum_len<=2e5);

cerr<<"SUCCESS\n";
cerr<<"Tests : " << t << '\n';
cerr<<"Sum of lengths : " << sum_len << '\n';
cerr<<"Maximum length : " << max_n << '\n';
// cerr<<"Total operations : " << total_ops << '\n';
//cerr<<"Answered yes : " << yess << '\n';
//cerr<<"Answered no : " << nos << '\n';
}


Editorialist's Solution
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
const ll z = 998244353 ;

void solve()
{
ll n;
cin >> n ;
ll arr[n] ;
for(int i = 0 ; i < n ; i++)
cin >> arr[i] ;

for(int i = 0 ; i < n ; i++)
{
if(i%3 == 0)
continue ;
if(i%3 == 1)
arr[i] = arr[i-1]*2 ;
else
{
if(i+1 < n)
arr[i] = arr[i+1]*2 ;
else
arr[i] = arr[i-1] ;
}
}
for(int i = 0 ; i < n ; i++)
cout << arr[i] << ' ';
cout << '\n' ;
return ;
}

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("inputf.txt" , "r" , stdin) ;
freopen("outputf.txt" , "w" , stdout) ;
freopen("error.txt" , "w" , stderr) ;
#endif

ll t;
cin >> t ;
while(t--)
{
solve() ;
}

return 0;
}

11 Likes

Nice Problem sirâ€¦ failed but learnt <3

6 Likes

Good Problem Setâ€¦ Couldnâ€™t understand durning the contest after that learned it

2 Likes

I tried it using prime sieve but I failed

1 Like

Can someone verify my logic about this case, itâ€™s similar to the editorial but I got WA

1. I would change every A[i], where i is odd to be A[i-1]*A[i+1], hence the gcd(A[i-1],A[i]) == A[i-1] and gcd(A[i],A[i+1]) == A[i+1]
2. If length of A is even, iâ€™ll change the last element in the array as the second last array
If it is odd, i wonâ€™t change the last array

the end result for odd and even array is as following:
odd Array = A1, A1 x A2, A2
even Array = A1, A1 x A2, A2, A2

It is guaranteed to change at most 1/2+1 element which is less than 2/3N

Edit: I didnâ€™t realize there is a constraint of A1 <= 10^6, itâ€™s all cleared up. Itâ€™s just hard to understand that many notations at a glance lol

4 Likes

i did the same whatâ€™s wrong in this solution can anyone explain.

a[i] *a[i+1] should be less or equal than 10^6.
let suppose if we have 3 numbers 999998 , 99999, 10^5 for this case a[I] *a[i+1] >10^6

In your logicâ€¦you are not considering the edge cases, let consider A1 is some prime number ( A1 >= 10^4) and similarly A2 is some prime number (A2 >= 10^4), So in this case when you multiply both the number to replace it with middle element their product would be greater than 10^8, but in the question they have clearly mentioned that the resultant array elements can lie between 2 and 10^6 (2<=Bi<=10^6)

Let {A1,A2,A3,A4,A5,A6,A7} be the original given array, consider A1 is a prime number greater than 10^4 (A1>=10^4) and A3 is a prime number greater than 10^4 (A3 >= 10^4). Now going with your logic, you replace A2 with A1A3 (A1A3 >= 10^8), after multiplying both of them A2 becomes greater than 10^8 (A2>=10^8).

This was the observation i missed. I was trying about changing half of the indices and doing something with it. I upsolved it. Very beautiful problem and i mean it!!

I thought I had it all figured out but it was the corner cases
Learned from this one.

1
7
30103 30203 30403 30703 30803 31013 31513

I read the corner case hint and believe me i knew that before
but i donâ€™t know how to solve this corner case even manually
i donâ€™t want to read the answer editorial now, can you help me with this corner case??

edit : nvm i looked at the editorial

It was a beautiful problem, enjoyed solving it!


// Non coprime neighbours

#include<bits/stdc++.h>
#define int long long int
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define min_pq priority_queue<int, vector<int>, greater<int>>
#define take_input freopen("input.txt", "r", stdin)
#define give_output freopen("output.txt", "w", stdout)

using namespace std;

vi smallestprime(int n=1e6) {
vi isp(n+1);
for(int i=0; i<=n; i++) isp[i] = i;
for(int i=2; i*i<=n; i++){
for(int j=i*i; j<=n; j+=i) {
if(isp[j] == j) {
isp[j] = i;
}
}
}
return isp;
}

int32_t main() {
// take_input;
// give_output;
vi isp = smallestprime();
int t; cin >> t;
while(t--){
int n; cin >> n;
vi arr(n);
for(int &i:arr) cin >> i;
int limit = ((2*n)%3 == 0? 2*n/3:2*n/3+1);
int cnt=0;
for(int i=1; i<n; i++) {
if(__gcd(arr[i-1], arr[i]) == 1){
if(i!=n-1){
arr[i] = isp[arr[i-1]]*isp[arr[i+1]];
}else{
arr[i] = arr[i-1];
}
cnt++;
}
}
if(cnt <= limit) {
for(int i:arr) cout << i << " ";
cout << endl;
}
}
}



I got the solution but here I am getting WA for all the test set. except one. Can somebody explain this. I first tried arr[i] = arr[i-1]*arr[i+1]/__gcd(arr[i-1], arr[i+1]) but because of the limit changed it to this but no difference at all

Hi @chetan06 lets you have 2 prime number one at arr[i-1] and one at arr[i+1] and lets say these prime numbers are of the order of 10^5 which is the range given in the question then their product will be of order 10^10 and gcd of two prime numbers is always 1 so this arr[i-1]*arr[i+1]/__gcd(arr[i-1], arr[i+1]) will be order of 10^10 which is greater than 10^6 hence WA

Ohh that was silly By the way, thanks

1 Like
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define MOD 1e9 + 7
#define int int64_t
using namespace std;
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

int Tc;
cin >> Tc;
while (Tc--)
{
int n;
cin >> n;
vector<int> vec(n);
for (auto &&i : vec)
cin >> i;

for (int i = 1; i < n; i += 2)
{
int x = vec[i - 1];
int y = vec[i + 1];
int gcd = __gcd(x, y);

if (i == n - 1)
{
vec[i] = x * vec[i];
vec[i] = __gcd(vec[i - 1], vec[i]);
continue;
}

if (x % 2 == 0 and y % 2 == 0 and gcd > 1)
{
vec[i] = gcd;
}
else if ((x + y) & 1)
{
if (x & 1)
{
vec[i] = 2 * x;
}
else
{
vec[i] = 2 * y;
}
}
else
{
int z = x * y;
vec[i] = __gcd(z, x) * __gcd(z, y);
}
}

for (auto &&i : vec)
{
cout << i << " ";
}
cout << endl;
}

return 0;
}


can someone tell me whatâ€™s wrong in this code

Can anyone tell me why i am getting WA with this approach

void solve() {
int n;
cin >> n;
vector<int> v(n + 2);
for (int i = 1; i < n + 1; i++) {
cin >> v[i];
}
int i = 1;
while (i <= n) {
if (i >= n - 1) {
v[n - 1] = 2 * v[n];
break;
}
if (v[i + 1] & 1) {
v[i] = v[i + 1] * 2;
v[i + 2] = v[i + 1] * 2;
}
else {
if (v[i] & 1)
v[i] = 2 * v[i + 1];
if (v[i + 2] & 1)
v[i + 2] = 2 * v[i + 1];
}
i += 3;
}
for (int i = 1; i <= n; i++) {
cout << v[i] << " ";
} cout << endl;
}


Input

1
4
3 5 2 3


Output
10 5 6 3

Still WA after updating:

if (i >= n - 1) {
v[n] = 2 * v[n - 2];
v[n - 1] = 2 * v[n - 2];
break;
}


Input

1
2
2 3


Output:
0 0

1 Like