0x31 质数
poj2689 算根号R的质数,然后把L~R区间(这个很小啊)的合数判下
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; int pr,prime[1100000];bool v[1100000]; void get_prime(int li) { pr=0;memset(v,false,sizeof(v)); for(int i=2;i<=li;i++) { if(v[i]==false)prime[++pr]=i; for(int j=1;j<=pr&&i*prime[j]<=li;j++) { v[i*prime[j]]=true; if(i%prime[j]==0)break; } } } int blen,bprime[1100000]; int main() { int L,R; while(scanf("%d%d",&L,&R)!=EOF) { if(L==1)L++; get_prime( (int(sqrt(double(R)))) ); memset(v,false,sizeof(v)); for(int j=1;j<=pr;j++) { LL d=L%prime[j]==0?L:((LL)prime[j])*((LL)(L/prime[j]+1)); if(d==prime[j])d+=prime[j]; while(d<=R) { v[d-L+1]=true; d+=prime[j]; } } blen=0; for(int i=1;i<=R-L+1;i++) if(v[i]==false)bprime[++blen]=i; if(blen<=1)printf("There are no adjacent primes.\n"); else { int mx1=-1,mx2,mn1=-1,mn2; for(int i=1;i<blen;i++) { if(mx1==-1||mx2-mx1<bprime[i+1]-bprime[i]) mx1=bprime[i], mx2=bprime[i+1]; if(mn1==-1||mn2-mn1>bprime[i+1]-bprime[i]) mn1=bprime[i], mn2=bprime[i+1]; } mx1+=L-1, mx2+=L-1, mn1+=L-1, mn2+=L-1; printf("%d,%d are closest, %d,%d are most distant.\n",mn1,mn2,mx1,mx2); } } return 0; }
阶乘分解 筛素数然后看每个素数的贡献啊。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; int pr,prime[1100000];bool v[1100000]; void get_prime(int li) { pr=0;memset(v,false,sizeof(v)); for(int i=2;i<=li;i++) { if(v[i]==false)prime[++pr]=i; for(int j=1;j<=pr&&i*prime[j]<=li;j++) { v[i*prime[j]]=true; if(i%prime[j]==0)break; } } } int main() { int n; scanf("%d",&n); get_prime(n); for(int j=1;j<=pr;j++) { printf("%d ",prime[j]); LL k=prime[j];int c=0; while(k<=n) { c+=n/k; k*=prime[j]; } printf("%d\n",c); } return 0; }
pain and happy in the cruel world.