线性筛-euler,强大O(n)
欧拉函数是少于或等于n的数中与n互质的数的数目
φ(1)=1(定义)
类似与莫比乌斯函数,基于欧拉函数的积性
φ(xy)=φ(x)φ(y)
由唯一分解定理
展开显然,得证
精髓在于对于积性的应用:
if(i%p[j]==){phi[i*p[j]]=phi[i]*p[j];break;}
phi[i*p[j]]=phi[i]*(p[j]-);
一个练手题Hdu1286
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
inline void read(int &ans){
ans=;char x=getchar();int f=;
while(x<''||x>''){if(x=='-')f=;x=getchar();}
while(x>=''&&x<='')ans=ans*+x-'',x=getchar();
if(f)ans=-ans;
} const int maxn=+;
int phi[maxn],p[maxn],flag[maxn],cnt;
void euler(int n){
phi[]=;
for(int i=;i<=n;i++){
if(!flag[i])p[++cnt]=i,phi[i]=i-;
for(int j=;j<=cnt && i*p[j]<=n;j++){
flag[i*p[j]]=;
if(i%p[j]==){phi[i*p[j]]=phi[i]*p[j];break;}
phi[i*p[j]]=phi[i]*(p[j]-);
}
}
} int main(){
euler();
int CN,N;
for(read(CN);CN--;)read(N),printf("%d\n",phi[N]);
return ;
}
Hdu1286
Hdu1787线性筛O(n),MLE,怎么办?在线算
int euler(int n){
int ans=n;
for(int i=;i*i<=n;i++){
if(n%i==)n/=i,ans-=ans/i;
while(n%i==)n/=i;
}
if(n>)ans-=ans/n;
return ans;
}
euler
AC code:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
inline bool read(int &ans){
ans=;char x=getchar();int f=;
while(x<''||x>''){if(x=='-')f=;x=getchar();}
while(x>=''&&x<='')ans=ans*+x-'',x=getchar();
if(f)ans=-ans;
return !!ans;
}
/*
const int maxn=(int)1e8+10;
int phi[maxn],p[maxn],flag[maxn],cnt;
void euler(int n){
phi[1]=1;
for(int i=2;i<=n;i++){
if(!flag[i])p[++cnt]=i,phi[i]=i-1;
for(int j=1;j<=cnt && i*p[j]<=n;j++){
flag[i*p[j]]=1;
if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;}
phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
}
*/ int euler(int n){
int ans=n;
for(int i=;i*i<=n;i++){
if(n%i==)n/=i,ans-=ans/i;
while(n%i==)n/=i;
}
if(n>)ans-=ans/n;
return ans;
} int main(){
//euler((int)1e8);cout<<euler(1)<<endl;
for(int N;read(N);)printf("%d\n",N-euler(N)-);
return ;
}
Hdu1787
Hdu2824,sigma(a,b) phi(x)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; typedef long long ll;
const int maxn=(int)3e6+;
ll phi[maxn];int p[maxn],cnt;bool flag[maxn];
void euler(int n){
phi[]=;
for(int i=;i<=n;i++){
if(!flag[i])p[++cnt]=i,phi[i]=i-;
for(int j=;j<=cnt && i*p[j]<=n;j++){
flag[i*p[j]]=;
if(i%p[j]==){phi[i*p[j]]=phi[i]*p[j];break;}
phi[i*p[j]]=phi[i]*(p[j]-);
}
}
for(int i=;i<=n;i++)phi[i]+=phi[i-];
} int main(){
euler((int)3e6);
for(int a,b;scanf("%d%d",&a,&b)==;)printf("%lld\n",phi[b]-phi[a-]);
return ;
}
Hdu2824
线性筛-euler,强大O(n)的更多相关文章
- The Euler function(线性筛欧拉函数)
/* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体 ...
- 线性筛-mobius,强大O(n)
首先,你要知道什么是莫比乌斯函数 然后,你要知道什么是积性函数 最后,你最好知道什么是线性筛 莫比乌斯反演 积性函数 线性筛,见上一篇 知道了,就可以愉快的写mobius函数了 由定义: μ(n)= ...
- jzp线性筛及其简单应用
前言: 很久以前看过了线性筛,没怎么注意原理,但是后来发现线性筛还有很有用的.. 比如上次做的一道题就需要找出每个数的最小质因子,先筛再找就太慢了..一看线性筛发现就可以直接在筛的过程中处理出来了! ...
- hdu_5750_Dertouzos(线性筛)
题目连接:hdu_5750_Dertouzos 题意: 给你一个n,一个d,问你比n小的数中有多少个数的最大的因子为d,比如6有因子1 2 3 最大的为3 题解: 当时比赛做这题的时候没考虑常数的优化 ...
- Codeforces 822D My pretty girl Noora - 线性筛 - 动态规划
In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlop ...
- bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...
- 欧拉函数O(sqrt(n))与欧拉线性筛素数O(n)总结
欧拉函数: 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. POJ 2407.Relatives-欧拉函数 代码O(sqrt(n)): ll euler(ll n){ ll ans=n; ...
- 积性函数&线性筛&欧拉函数&莫比乌斯函数&因数个数&约数个数和
只会搬运YL巨巨的博客 积性函数 定义 积性函数:对于任意互质的整数a和b有性质f(ab)=f(a)f(b)的数论函数. 完全积性函数:对于任意整数a和b有性质f(ab)=f(a)f(b)的数论函数 ...
- 【???】今天上午的考试题——区间dp和字符串/线性筛的综合应用
T3还没有打出来,就先放两道. ---------------------------------------------------------- T1:密码破译 温温手下的情报部门截获了一封加密信 ...
随机推荐
- PHP常见数组函数总结
一.数组的一些关于键名和值的基础操作函数 1.获取数组所有的键或值:array_keys() array_values() $arr_keys = array_keys($array); $arr_v ...
- Qt Installer Framework翻译(8)
好了,到这里翻译就结束了.各位可以下载源码,结合examples示例,使用repogen和binarycreator好好实操一下,就能掌握基础用法了.祝各位使用顺利. 官方文档网址:https://d ...
- shell登录 脚本 expect
作用 工作中,我们运行命令.脚本或程序时,这些命令.脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行. 利用expect,则可以根据程序的提示,模拟标准输入提供给程序,从而 ...
- python3练习100题——032
链接:http://www.runoob.com/python/python-exercise-example32.html 题目:按相反的顺序输出列表的值. 我的代码: for i in li[:: ...
- bat文件一键运行python自动化脚本
目标:建立一个双击即可运行自动化脚本的机制,而不用每次运行编译器,方便测试人员用户体验. 方法: 1. 将所有代码打包成exe文件,但一旦修改,又要重新打包. 2. 将运行代码写成bat文件,双击即执 ...
- mac或windows下Navicat Premium安装
找了很多地址都乱七八糟的说明一通还没啥用,好不容易找到一个靠谱的,记录一下,也方便一下和我一样苦苦寻找的人,亲测有用 https://www.52pojie.cn/thread-727433-1-1. ...
- Selenium原理
from selenium import webdriver:导入webdriver模块 当导入webdriver模块时,会执行\selenium\webdriver目录下的__init__.py ...
- 详解C/C++中的的:#pragma pack(push) 、#pragma pack(pop) 和#pragma pack()
前言 我们知道结构体内存对齐字节可以通过#pragma pack(n) 的方式来指定. 但是,有没有想过一个问题,某些时候我想4字节对齐,有些时候我又想1字节或者8字节对齐,那么怎么解决这个问题呢? ...
- Python tip
shutil.rmtree() 表示递归删除文件夹下的所有子文件夹和子文件.
- 巨杉Tech | 使用 SequoiaDB + Docker + Nodejs 搭建 Web 服务器
容器化技术的出现大大简化了应用开发人员在构建底层基础设施的工作.SequoiaDB 巨杉数据库于3.2.1版本正式推出了 Docker 容器化部署方案,本文将会基于 SequoiaDB 巨杉数据库与N ...