First, a linear prime sieve, 1 to 10000000 of the prime numbers are screened out, and then violently run through all possible values, make a table, query the time O (1) Efficiency out of the solution.
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#include<cstring>using namespacestd;Const LongN =10000000;Long LongPrime[n] = {0},num_prime =0;intIsnotprime[n] = {1,1};intr[10000000+Ten];intans[10000000+Ten];intMain () {memset (R),0,sizeof(R)); memset (ans,0,sizeof(ans)); for(Longi =2; i < N; i + +) { if(!Isnotprime[i]) prime[num_prime++]=i; //key points 1 for(Longj =0; J < num_prime && I * prime[j] < N; J + +) {Isnotprime[i* Prime[j]] =1; if( ! (i% prime[j]))//Key points 2 Break; } } for(intI=0; i<num_prime;i++) { for(intj=i+1; j<num_prime;j++) { if(prime[i]*prime[j]>10000000) Break; R[prime[i]*prime[j]]=1; }} ans[0]=0; for(intI=1; i<=10000000; i++) { if(r[i]==1) ans[i]=ans[i-1]+1; Elseans[i]=ans[i-1]; } intLL,RR; while(~SCANF ("%d%d",&ll,&RR)) {printf ("%d\n", ans[rr]-ans[ll-1]); } return 0;}
NEU OJ 1649 GMZ ' s pretty number