标签:筛法 mil nbsp break namespace set data rand stream
一、数学相关
1.筛法
(1)埃氏筛
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int n,m,cnt; 7 bool check[10000010]; 8 int prime[10000010]; 9 void Eratosthenes(){ 10 check[0]=1; 11 check[1]=1; 12 for(int i=2;i<=n;i++) 13 if(!check[i]){ 14 prime[++cnt]=i; 15 for(int j=2*i;j<=n;j+=i) check[j]=1; 16 } 17 } 18 int main(){ 19 scanf("%d%d",&n,&m);//n-查询范围 m-查询个数 20 Eratosthenes(); 21 int a; 22 for(int i=1;i<=m;i++){ 23 scanf("%d",&a); 24 if(!check[a]) printf("Yes\n"); 25 else printf("No\n"); 26 } 27 return 0; 28 }
(2)线性筛
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int n,m,cnt; 7 bool check[10000010]; 8 int prime[10000010]; 9 void work(){ 10 check[0]=1; 11 check[1]=1; 12 for(int i=2;i<=n;i++){ 13 if(!check[i]) prime[++cnt]=i; 14 for(int j=1;j<=cnt&&i*prime[j]<=n;j++){ 15 check[i*prime[j]]=1; 16 if(!(i%prime[j])) break; 17 } 18 } 19 } 20 int main(){ 21 scanf("%d%d",&n,&m);//n-查询范围 m-查询次数 22 work(); 23 int a; 24 for(int i=1;i<=m;i++){ 25 scanf("%d",&a); 26 if(!check[a]) printf("Yes\n"); 27 else printf("No\n"); 28 } 29 return 0; 30 }
2.Miller-Rabin素数测试
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 int n,m; 8 long long num(long long x){ 9 return ((double)rand()/RAND_MAX*x+0.5); 10 } 11 long long qpow(long long a,long long b,long long p){ 12 long long ret=1; 13 while(b){ 14 if(b&1) ret=a*ret%p; 15 b>>=1; 16 a=a*a%p; 17 } 18 return ret; 19 } 20 long long multiply(long long a,long long b,long long p){ 21 long long ret=0; 22 while(b){ 23 if(b&1) ret=(ret+a)%p,b--; 24 b>>=1; 25 a=(a+a)%p; 26 } 27 return ret; 28 } 29 bool check(long long r,long long x){ 30 long long xx=x-1; 31 int cnt=0; 32 while(!(xx%2)){ 33 xx/=2; 34 cnt++; 35 } 36 long long mi=qpow(r,xx,x); 37 if(mi==1||mi==x-1) return 1; 38 while(cnt--){ 39 mi=multiply(mi,mi,x);//!!! 40 if(mi==x-1) return 1; 41 } 42 return 0; 43 } 44 45 bool miller_rabin(long long x){ 46 if(x==2) return 1; 47 if(x<2||!(x%2)) return 0; 48 for(int i=1;i<=50;i++){//洛谷亲测i<=3可过 49 long long r=num(x-2)+1; 50 if(!check(r,x)) return 0; 51 } 52 return 1; 53 } 54 int main(){ 55 scanf("%d%d",&n,&m); 56 long long a; 57 for(int i=1;i<=m;i++){ 58 scanf("%lld",&a); 59 if(miller_rabin(a)) printf("Yes\n"); 60 else printf("No\n"); 61 } 62 return 0; 63 }
二、图论
1.dinic最大流
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int inf=0x3f3f3f3f; 8 int n,m,s,t,cnt,ans; 9 struct data{ 10 int nextt,to,dis; 11 }edge[200010]; 12 int head[10010],flor[10010]; 13 void add(int start,int end,int dd){ 14 edge[cnt].nextt=head[start]; 15 edge[cnt].to=end; 16 edge[cnt].dis=dd; 17 head[start]=cnt++; 18 } 19 void insert(int start,int end,int dd){ 20 add(start,end,dd); 21 add(end,start,0); 22 } 23 bool bfs(){ 24 memset(flor,-1,sizeof(flor)); 25 flor[s]=0; 26 queue<int>q; 27 q.push(s); 28 while(!q.empty()){ 29 int p=q.front(); 30 q.pop(); 31 for(int i=head[p];i!=-1;i=edge[i].nextt) 32 if(flor[edge[i].to]<0&&edge[i].dis>0){ 33 flor[edge[i].to]=flor[p]+1; 34 q.push(edge[i].to); 35 } 36 } 37 if(flor[t]<0) return 0; 38 return 1; 39 } 40 int dfs(int x,int flow){ 41 int tmp=0,ret=0;; 42 if(!flow||x==t) return flow; 43 for(int i=head[x];i!=-1;i=edge[i].nextt) 44 if(flor[edge[i].to]==flor[x]+1&&edge[i].dis>0){ 45 int tmp=dfs(edge[i].to,min(edge[i].dis,flow)); 46 edge[i].dis-=tmp; 47 edge[i^1].dis+=tmp; 48 ret+=tmp; 49 flow-=tmp; 50 } 51 return ret; 52 } 53 void dinic(){ 54 while(bfs()) ans+=dfs(s,inf); 55 } 56 int main(){ 57 scanf("%d%d%d%d",&n,&m,&s,&t); 58 memset(head,-1,sizeof(head)); 59 int u,v,d; 60 for(int i=1;i<=m;i++){ 61 scanf("%d%d%d",&u,&v,&d); 62 insert(u,v,d); 63 } 64 dinic(); 65 printf("%d",ans); 66 return 0; 67 }
标签:筛法 mil nbsp break namespace set data rand stream
原文地址:http://www.cnblogs.com/sdfzxh/p/7148171.html