Pls help me to optimise the code

hello @Senjuti256

pls share correct code link.

I am saving the code in coding blocks ide but code link is not getting generated. Also I am unable to log in into my account in the ide.

ok then pls share ur code using gfg ide

Pls help what to do now. Shall I paste the code in the doubt box. Gfg ide is also not opening.

#include #include<bits/stdc++.h> using namespace std; #define ll long long int vectorp(1000000,0); ll qthprime(ll q) { ll i,cnt=0; for(i=0;i<10000;i++) { if(p[i]==1) cnt++; if(cnt==q) break; } return i; } int main() { ll i,j; p[2]=1; p[1]=0; for(i=3;i<=10000;i+=2) { p[i]=1; } for(i=3;i<=10000;i+=2) { if(p[i]) for(j=i*i;j<=10000;j+=i) { p[j]=0; } } //making a vector of stacks ll data,q,n,r; cin>>n>>q; vector<stack> B(100000),A(100000); for(i=0;i<n;i++) { cin>>data; A[0].push(data); } for(i=0;i<q;i++) { while(!A[i].empty()) { data=A[i].top(); A[i].pop(); data%qthprime(i+1)==0?B[i].push(data):A[i].push(data); } } for(i=0;i<q;i++) { while(!B[i].empty()) { data=B[i].top(); cout<<data<<" “; B[i].pop(); } } //printing Aqth stack while(!A[q-1].empty()) { data=A[q-1].top(); cout<<data<<” "; A[q-1].pop(); } return 0; }

save it here->https://ideone.com/

paste ur code and then click on run.
a link will be generated in ur search bar.
share that link with me

this is not readable

finally could do it.pls e

pls tell me if any other approach is there.

store all primes in some array/vector and then use that vector to find the ith prime in O(1). this will save time and make ur code faster.

check this for reference->

in the reference code why are we printing all the stacks Ai? We should only print Aq right? print

yes . . . . . . . , from that code look how we are storing prime rest implement yourself

Yes thanks now I will try myself. One doubt is that the constraint is that Ai ranges upto 10^9 so should we do the sieve of Eratosthenes upto 10^9?

no , u need q primes (in worst case q is 10^5, so ur sieve should be big enough to accomodate 10^5 th prime).

find 10^5th prime on internet and build sieve upto that

We need q primes but the qth prime might exceed 10^5 because in b/w many are not prime nos. Then?

ss
build ur sieve upto this no,

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.