素筛模板

 1 #include<iostream>
 2 #include<cstring>
 3
 4 #define MAX_N 10000000
 5
 6 using namespace std;
 7
 8 bool flag[MAX_N];
 9 int prime[MAX_N];
10
11 void solve()
12 {
13     memset(flag,1,sizeof(flag));
14     flag[1] = 0;
15     int con=0;
16     for(int i = 2; i < MAX_N; i++)
17     {
18         if(flag[i])
19         {
20             prime[con++] = i;
21             for(int j = 2*i; j < MAX_N; j+=i)
22                 flag[j] = 0;
23         }
24     }
25 }
26 int main()
27 {
28     solve();
29     return 0;
30 }
时间: 2024-10-24 07:10:03

素筛模板的相关文章

素数筛 模板

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 int prim[3000000]={2,3,5}; 8 //素数是分为基本素数{2,3}.阳素数{6N+1,N>=1}形式的.阴素数{6N-1,N>=1}形式的 9 //为了代码的好写,在这里这样写的 : 10 //数除了{2,3,5}为素数,其他的数可以写成6N,6N+1

ACdream 1112 Alice and Bob(素筛+博弈SG函数)

Alice and Bob Time Limit:3000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu Submit Status Practice ACdream 1112 Description Here  is Alice and Bob again ! Alice and Bob are playing a game. There are several numbers. First, Alice choose

zoj 2723 Semi-Prime(素筛打表+搜索优化)

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2723 题目描述: Prime Number Definition An integer greater than one is called a prime number if its only positive divisors (factors) are one and itself. For instance, 2, 11, 67, 89 are pri

线性筛模板

蒟蒻要开始打数论模板了orz 线性筛都忘了怎么打,我太弱啦! #pragma GCC optimize("O2") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<set> #in

杜教筛 模板

转自: http://blog.leanote.com/post/totziens/%E8%8E%AB%E6%AF%94%E4%B9%8C%E6%96%AF%E5%8F%8D%E6%BC%94%E4%B8%8E%E6%9D%9C%E6%95%99%E7%AD%9B 1. 求$\sum\limits_{i=1}^n\mu(i),n\leqslant 10^{11}$直接求不好求,但是我们有$\sum\limits_{i=1}^n\sum\limits_{d|i}\mu(d)=1$化一下蛤:$\su

【线性筛】洛谷P3383线性筛模板

思路: 如果我们要筛出 [1, n] 内的所有素数,使用 [1, √n] 内的素数去筛就可以了 设bool型数组 a,a[i] 表示 i 是否被某个素数筛过 从 2 开始枚举每个数 i: 若 a[i] = false,表示 i 没有更小的素因子,从而知道 i 是素数.枚举 i 的所有倍数 j,令 a[j] = 1 这样就可以在线性复杂度内预处理出比较大的区间的素数 代码如下: #include<cstdio> #include<iostream> using namespace s

GCD&amp;&amp;素筛&amp;&amp;快速幂 --A - Pseudoprime numbers

Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes,

杜教筛模板

1 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 #include<map> 7 #define ll long long 8 #define N 2000005 9 using namespace std; 10 const int now=1<<20; 11 ll phi[N]; 12 int pr[N],mi

欧拉线性筛模板

memset(mindiv,0,sizeof(mindiv)); for(int i=2;i<=n;i++){ if(!mindiv[i]) prime[++tot]=mindiv[i]=i; for(int j=1;j<=tot&&prime[j]<=mindiv[i]&&(k=prime[j]*i)<=n;j++) mindiv[k]=prime[j]; }