1AC. 1D DP + sieving
#include <cmath>#include<cstdio>#include<cmath>#include<climits>#include<cctype>#include<vector>#include<string>#include<iostream>#include<algorithm>#include<numeric>#include<unordered_map>using namespacestd;intWaysintN) { if(N <=1)returnN; Vector<int> dp (n +1,0); dp[0] =1; for(inti =0; I < n; i++) {Dp[i+1] +=Dp[i]; if((i +4) <= N) dp[i +4] +=Dp[i]; } returndp[n];}intCntprimes (intN//sieving{vector<int> Mark (N,1); mark[0] =0; intInx =1;//2 intSCNT =1; while(Inx < n && scnt >0) {scnt=0; for(inti =2* Inx +1; I < n; i + = (Inx +1) ) {Mark[i]=0; SCNT++; } if(Scnt >0) {Inx++; while(Inx < n && Mark[inx] = =0) inx++; } } returnStd::accumulate (Mark.begin (), Mark.end (),0);}intMain () {intT CIN >>T; while(t--) { intN CIN >>N; intncnt =ways (n); intRET =cntprimes (ncnt); cout<< ret <<Endl; } return 0;}
Hackerrank-"Red John is Back"