O(n) 筛选素数

#include<bits/stdc++.h>
using namespace std;
const int M = 1e6 + 10 ; int mindiv[M] ;//每个数的最小质因数
int prim[M] , pnum ;//存素数
bool vis[M] ; void prim () {
for (int i = 2 ; i < M ; i ++) {
if (!vis[i]) {
mindiv[i] = i ;
prim[ pnum++ ] = i ;
}
for (int j = 0 ; j < pnum ; j ++) {
if ( i*prim[j] >= M ) break ;
vis[ i*prim[j] ] = 1 ;
mindiv[i] = prim[j] ;
if (i % prim[j] == 0) break ;
}
}
} int main () {
prim () ;
return 0 ;
}

  欧拉函数:phi[i] 为<= i 的范围内与i互质的数的数量

  欧拉埃筛,写起来简单,复杂度O(log(log(N)))(zstu 幻神):

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 1e6 + 10 ; int n, m, T; int euler[M]; void Euler () {
for(int i = 0; i < M ; i ++) euler[i] = i;
for(int i = 2; i < M ; i ++){
if(euler[i] == i) {
for (int j = i; j < M ; j += i) {
euler[j] = euler[j] - euler[j]/i;
}
}
}
} int main(){
Euler ();
int n ;
while (~ scanf ("%d" , &n)) printf ("%d\n" , euler[n]) ;
return 0;
}

  

欧拉线筛,写起来复杂点,(墨迹了我半天)复杂度O(N):

#include<bits/stdc++.h>
using namespace std;
const int M = 1e6 + 10 ;
int prim[M] , pnum ;
bool vis[M] ;
int phi[M] ; void Euler () {
for (int i = 2 ; i < M ; i ++) {
if (!vis[i]) {
prim[ pnum++ ] = i ;
phi[i] = i - 1;
}
for (int j = 0 ; j < pnum ; j ++) {
int x = i * prim[j] ;
if (x >= M ) break ;
vis[x] = 1 ;
if (i % prim[j] == 0) {
int y = i , cnt = 0 , z = prim[j] ;
while (y % prim[j] == 0) cnt ++ , y /= prim[j] , z *= prim[j] ;
if (y == 1) phi[x] = x - x/prim[j] ;
else phi[x] = phi[y] * phi[z] ;
break ;
}
else phi[x] = phi[i] * phi[ prim[j] ] ;
}
}
} int main () {
Euler () ;
int n ;
while (~ scanf ("%d" , &n)) printf ("%d\n" , phi[n]) ;
return 0 ;
}

  线性欧拉跟新:

#include<cstdio>
#include<iostream>
using namespace std;
int prime[100005],phi[1000005];
int main(){
int i,j;
for(i=2;i<1000002;++i){
if(!phi[i]){
phi[i]=i-1;
prime[++prime[0]]=i;
}
for(j=1;j<=prime[0]&&(long long)i*prime[j]<1000002;++j)
if(i%prime[j])phi[i*prime[j]]=phi[i]*(prime[j]-1);
else{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
}
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
printf("%d\n",phi[n+1]);
}
}

  

素数的线性筛 && 欧拉函数的更多相关文章

  1. The Euler function(线性筛欧拉函数)

    /* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体 ...

  2. 积性函数&线性筛&欧拉函数&莫比乌斯函数&因数个数&约数个数和

    只会搬运YL巨巨的博客 积性函数 定义 积性函数:对于任意互质的整数a和b有性质f(ab)=f(a)f(b)的数论函数. 完全积性函数:对于任意整数a和b有性质f(ab)=f(a)f(b)的数论函数 ...

  3. [bzoj 2190][SDOI2008]仪仗队(线性筛欧拉函数)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2190 分析:就是要线性筛出欧拉函数... 直接贴代码了: memset(ans,,sizeof ...

  4. Bzoj 2186: [Sdoi2008]沙拉公主的困惑 乘法逆元,线性筛,欧拉函数,数论

    2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2560  Solved: 857[Submit][St ...

  5. BZOJ 2190 仪仗队(线性筛欧拉函数)

    简化题意可知,实际上题目求得是gcd(i,j)=1(i,j<=n)的数对数目. 线性筛出n大小的欧拉表,求和*2+1即可.需要特判1. # include <cstdio> # in ...

  6. poj1248 (线性筛欧拉函数)(原根)

    强烈鸣谢wddwjlss 题目大意:给出一个奇素数,求出他的原根的个数,多组数据. 这里先介绍一些基本性质 阶 设\((a,m)=1\),满足\(a^r \equiv 1 \pmod m\)的最小正整 ...

  7. BZOJ 2818 GCD 素数筛+欧拉函数+前缀和

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=n且Gcd(x,y)为素数的数对( ...

  8. noip复习——线性筛(欧拉筛)

    整数的唯一分解定理: \(\forall A\in \mathbb {N} ,\,A>1\quad \exists \prod\limits _{i=1}^{s}p_{i}^{a_{i}}=A\ ...

  9. Farey Sequence (素筛欧拉函数/水)题解

    The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/ ...

随机推荐

  1. MyEclipse 8.5 注册码 生成代码

    import java.io.*; public class MyEclipseGen { private static final String LL = "Decompiling thi ...

  2. JS 复制对象

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...

  3. CentOS7安装mongoDB数据库

    CentOS7安装mongoDB数据库 时间:2015-03-03 16:45来源:blog.csdn.net 作者:进击的木偶 举报 点击:8795次 mongoDB是目前发展比较好的NOSQL数据 ...

  4. CF 445B DZY Loves Chemistry(并查集)

    题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second     memory limit per test:256 megabytes D ...

  5. RNN 入门教程 Part 2 – 使用 numpy 和 theano 分别实现RNN模型

    转载 - Recurrent Neural Networks Tutorial, Part 2 – Implementing a RNN with Python, Numpy and Theano 本 ...

  6. response压缩响应

    思路:1.通过filter向目标页面传递一个自定义的response对象 2..在这个response对象中通过重写getOutputStream方法和getWriter方法使目标资源调用 该方法输出 ...

  7. C#读写文本文件

    static public string Read(string path) { StreamReader sr = new StreamReader(path,Encoding.Default); ...

  8. spring-boot-note

    1 java配置和注解配置相结合,不需要任何的xml配置即可 2 spring tool suite 3 src/main/resources/banner.txt http://patorjk.co ...

  9. angular 兼容ie7 实现

    <script src="~/Content/js/angular.min.js"></script><script src="~/Cont ...

  10. HTML教程

    HTML文档可以包含的内容 通过不同的标签,HTML文档可以包含不同的内容,比如文本,链接,图片,列表,表格,表单,框架等. 文本 HTML对文本的支持是最丰富的,你可以设置不同级别的标题,分段和换行 ...