Test instructions: If k adjacent prime p1,p2,p3.....pk satisfies pk-p1=s, it is said that these prime numbers constitute a distance of s of Prime K-tuple, input a,b,k,s, the output interval [a, b] within the distance is the number of K-tuple of S.
Idea: first hit the 50000 prime number table, and then violently strive to out a A, a, and then judge the number of interval.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 intT;7 inta,b,k,s,cnt;8 BOOLvis[500000];9 intf[500000];Ten intp[10000000]; One voidgetprime () A { -Cnt=0; -memset (Vis,0,sizeof(Vis)); thevis[1]=true; -vis[0]=true; - for(intI=2; i<=50000; i++) - { + if(!Vis[i]) - { +f[cnt++]=i; A for(intj=i*2; j<=50000; j+=i) at { -vis[j]=true; - } - } - } - } in - intMain () to { + getprime (); -scanf"%d\n",&t); the while(t--) * { $scanf"%d%d%d%d",&a,&b,&k,&s);Panax NotoginsengMemset (P,0,sizeof(P)); - intans=0; the intt1=0; + for(intI=a; i<=b; i++) A { the if(i<=50000) + { - if(!vis[i]) p[t1++]=i; $ } $ Else - { - BOOLflag=true; the for(intj=0; j<cnt&&f[j]*f[j]<=i; J + +) - {Wuyi if(i%f[j]==0) the { -flag=false; Wu Break; - } About } $ if(flag) - { -p[t1++]=i; - } A } + } the for(intI=0; i<t1; i++) - { $ if(p[i+k-1]-p[i]==s) the { theans++; the } the } -printf"%d\n", ans); in } the return 0; the}View Code
LA 3998 Prime K-tuple