这是欧拉筛。。。
#include<bits/stdc++.h>
using namespace std;
int n,now,d[1000005],x;
bool f[10000005];
int read()
{
int x=0;char c;bool f=false;
c=getchar();
if (c=='-') f=true;
while (c<'0'||c>'9') c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
if (f==true) x=x*-1;
return x;
}
int main()
{
// freopen("1.out","w",stdout);
n=read();
now=0;
memset(d,0,sizeof(d));
memset(f,false,sizeof(f));
for (int i=2;i<=n;i++)
{
if (f[i]==false) {now++;d[now]=i;}
for (int j=1;j<=now&&i*d[j]<=n;j++)
{
f[i*d[j]]=true;
if (i%d[j]==0) break;
}
}
n=read();
f[1]=true;
for (int i=1;i<=n;i++)
{
x=read();
if (f[x]==false) printf("Yes\n");
else printf("No\n");
}
return 0;
}
这是01字典树
#include<cstdio>
using namespace std;
long long ans,n,tot,now,x,a[500005];
struct mmp{
int s,son[2];
}tree[5000005];
long long read()
{
long long x=0;char c;bool f;
f=false;
c=getchar();
if(c=='-') f=true;
while (c<'0'||c>'9') c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
if (f==true) x=x*-1;
return x;
}
void get(long long x,int &r,int k)
{
if(r==0) tot++,r=tot;
tree[r].s++;
if(k==-1) return;
if(x & (1ll<<k)) get(x,tree[r].son[1],k-1);
else get(x,tree[r].son[0],k-1);
return;
}
void check(long long S,int rt,int Rt,int k)
{
if((rt==0)||(Rt==0))return;
if((1ll<<(k+1))<=S)return;
if((1ll<<k)>=S)
{
ans=ans+1LL*tree[tree[rt].son[1]].s*tree[tree[Rt].son[0]].s+1LL*tree[tree[rt].son[0]].s*tree[tree[Rt].son[1]].s;
check(S,tree[rt].son[0],tree[Rt].son[0],k-1);
check(S,tree[rt].son[1],tree[Rt].son[1],k-1);
}
else
{
check(S-(1ll<<k),tree[rt].son[1],tree[Rt].son[0],k-1);
check(S-(1ll<<k),tree[rt].son[0],tree[Rt].son[1],k-1);
}
return;
}
int main(){
freopen("magic.in","r",stdin);
freopen("magic.out","w",stdout);
n=read();
tot=1;
for(int i=1;i<=n;i++)
{
int now=1;
x=read();
get(x,now,60);
}
now=1;
for(int i=1;i<=19;i++)
now=now*10,check(now,1,1,60);
ans=ans/2;
printf("%lld",ans);
}
还有可爱的线段树
#include<bits/stdc++.h>
using namespace std;
long long tree[400005],a[4000005],add[400005],n,m,x,y,z;
long long read() {
long long x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if(f=='-')f=-1;c=getchar();}
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
void up(long long now) {
tree[now]=tree[now<<1]+tree[now<<1|1];
}
void build(long long x,long long y,long long now) {
if (x==y) {
tree[now]=a[x];
return;
}
long long m=(x+y)>>1;
build(x,m,now<<1);
build(m+1,y,now<<1|1);
up(now);
}
void pushdown(long long llen,long long rlen,long long now) {
if (add[now]) {
add[now<<1]+=add[now];
add[now<<1|1]+=add[now];
tree[now<<1]+=add[now]*llen;
tree[now<<1|1]+=add[now]*rlen;
add[now]=0;
}
}
void updata(long long l,long long r,long long x,long long y,long long z,long long now) {
if (x>=l&&y<=r) {
tree[now]+=z*(y-x+1);
add[now]+=z;
return;
}
long long m=(x+y)>>1;
pushdown(m-x+1,y-m,now);
if (m>=l) updata(l,r,x,m,z,now<<1);
if (m<r) updata(l,r,m+1,y,z,now<<1|1);
up(now);
}
long long sum(long long l,long long r,long long x,long long y,long long now) {
if (x>=l&&y<=r) return tree[now];
long long m=(x+y)>>1;
long long ans=0;
pushdown(m-x+1,y-m,now);
if (l<=m) ans+=sum(l,r,x,m,now<<1);
if (r>m) ans+=sum(l,r,m+1,y,now<<1|1);
return ans;
}
int main() {
n=read();
m=read();
for (long long i=1; i<=n; i++)
a[i]=read();
memset(add,0,sizeof(add));
build(1,n,1);
for (long long i=1; i<=m; i++) {
x=read();
if (x==1) {
long long xx=read();
y=read();
z=read();
updata(xx,y,1,n,z,1);
} else {
long long xx=read();
y=read();
printf("%lld\n",sum(xx,y,1,n,1));
}
}
return 0;
}