线性筛-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:密码破译 温温手下的情报部门截获了一封加密信 ...
随机推荐
- 【Unity|C#】基础篇(19)——集合库(Collections)
[学习资料] <C#图解教程>(第6章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.c ...
- 【Unity|C#】基础篇(13)——特性(Attribute)
[学习资料] <C#图解教程>(第24章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
- Python元组详解
元组的特征 元组类型的名字是tuple 元组的一级元素不可被修改.不能增加或者删除: 元组和列表的书写区别是将中括号改成了小括号: 为方便区分元组和普通方法的参数,一般在元组的最后一个元素后保持加一个 ...
- caffe+win10+git使用sh文件
在windows下是否可以执行sh文件呢,搜了一下,可以安装了git就可以执行,当然这不是唯一答案. 然后联想到caffe下有一些.sh文件可以尝试,就用create_mnist.sh尝试把. cre ...
- Nginx proxy_set_header 配置注意事项
转载自:https://www.jianshu.com/p/fd16b3d10752 如果没有特别注意 proxy_set_header 配置,使用 proxy_set_header 可能会引起以下问 ...
- [SDOI2008] 校门外的区间 - 线段树
U T 即将区间 \(T\) 范围赋值为 \(1\) I T 即将区间 \(U - T\) 范围赋值为 \(0\) D T 即将区间 \(T\) 赋值为 \(0\) C T 由于 \(S=T-S=T( ...
- [Arc083D/At3535] Restoring Road Network - 最短路,结论
[Arc083D/At3535] 有 \(N\) 个城市,城市与城市之间用长度为整数的无向道路连接. 现有一考古学家找到了一张 \(N×N\) 的表 \(A\) ,这张表代表了这 \(N\) 座城市两 ...
- linux - 查看 python 版本
命令 python -V 结果
- python:复制文件及文件夹
#!/usr/bin/python# -*- coding:utf-8 -*- import shutil #shutil.copy(文件1,文件2)#将源内容复制到目标文件中.d.txt不存在则创建 ...
- mysql对表中数据根据某一字段去重
要删除重复的记录,就要先查出重复的记录,这个很容易做到 注意:这是查出所有重复记录的第一条记录,需要保留,因此需要添加查询条件,查出所有的重复记录 ) ) 然后 delete from cqssc w ...