hdu 2824 欧拉函数 O(nlogn) 和O(n)
裸题
O(nlogn):
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=3000000+100;
int phi[maxn];
void init() {
for(int i=2;i<maxn;i++) phi[i]=i;
for(int i=2;i<maxn;i++)
if(phi[i]==i)
for(int j=i;j<maxn;j+=i)
phi[j]=(phi[j]/i)*(i-1);
}
int main() {
int a,b;
init();
while(scanf("%d%d",&a,&b)!=EOF) {
ll ans=0;
for(int i=a;i<=b;i++) ans+=(ll)phi[i];
printf("%lld\n",ans);
}
return 0;
}
O(n):
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=3000000+10;
/*线性筛O(n)时间复杂度内筛出maxn内欧拉函数值*/
int m[maxn],phi[maxn],p[maxn],pt;//m[i]是i的最小素因数,p是素数,pt是素数个数
void init() {
phi[1]=1;
int N=maxn;
int k;
for(int i=2; i<N; i++) {
if(!m[i])//i是素数
p[pt++]=m[i]=i,phi[i]=i-1;
for(int j=0; j<pt&&(k=p[j]*i)<N; j++) {
m[k]=p[j];
if(m[i]==p[j]) { //为了保证以后的数不被再筛,要break
phi[k]=phi[i]*p[j];
/*这里的phi[k]与phi[i]后面的∏(p[i]-1)/p[i]都一样(m[i]==p[j])只差一个p[j],就可以保证∏(p[i]-1)/p[i]前面也一样了*/
break;
} else
phi[k]=phi[i]*(p[j]-1);//积性函数性质,f(i*k)=f(i)*f(k)
}
}
}
int main() {
int a,b;
init();
while(scanf("%d%d",&a,&b)!=EOF) {
ll ans=0;
for(int i=a; i<=b; i++) {
ans+=(ll)phi[i];
}
printf("%lld\n",ans);
}
return 0;
}
hdu 2824 欧拉函数 O(nlogn) 和O(n)的更多相关文章
- hdu 2824(欧拉函数)
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
- hdu 2654(欧拉函数)
Become A Hero Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1395(欧拉函数)
2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 3307(欧拉函数+好题)
Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- 找新朋友 HDU - 1286 欧拉函数模板题
题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...
- *HDU 1286,2824欧拉函数
#include<iostream> #include<string> #include<cstdio> #include<cmath> #includ ...
- hdu 4983 欧拉函数
http://acm.hdu.edu.cn/showproblem.php?pid=4983 求有多少对元组满足题目中的公式. 对于K=1的情况,等价于gcd(A, N) * gcd(B, N) = ...
- hdu 4002 欧拉函数 2011大连赛区网络赛B
题意:求1-n内最大的x/phi(x) 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是 ...
随机推荐
- SQL循环表里的数据
select * into #t1 from Userinfodeclare @id varchar(2000);while (exists(select ProjectID from #t1))be ...
- websocket+rabbitMQ
拉取镜像:docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 -p 61613:616 ...
- windows openssl-1.1.1 编译静态库和动态库
一下为windows上安装过程 1.下载 openssl-1.1.0.tar.gz 2.安装 ActivePerl, 可以到http://www.activestate.com/activeperl/ ...
- js问题解决——cannot read property style of undefind
当遇到cannot read property style of undefine的问题如下: 那就说明你设定的这个变量名为空 比如我在文档里写的属性名为list_box 但是我获取的属性名称写错了 ...
- SQL-58 获取有奖金的员工相关信息。
题目描述 获取有奖金的员工相关信息.CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_date` date NOT NULL,`fi ...
- linux安装mysql(shell一键安装)
1. 相关文件(install_mysql.sh.my.cnf.mysqld相关内容在文中最后面) 2. 将上面的文件上传到linux服务器某一目录下 3.给install_mysql.sh赋执行权限 ...
- 将字符串类型的出生日期转为int类型的年龄
public static int getAgeByBirthday(String s) { Date birthday = null; SimpleDateFormat format = new S ...
- spyder常用功能
最近和同学讨论到spyder的使用技巧,所以就结合之前在网上看到网友的总结( https://blog.csdn.net/peiwang245/article/details/78528098)和自己 ...
- vs code 本地请求json
首先下载扩展live server 重新加载后 右下方工具栏会多出一个go live 点击go live,会默认启动chome,地址 http://127.0.0.1:5500 js脚本: $.aj ...
- Ubuntu 修改sudoers之后无法用sudo怎么恢复
进入终端 键入 pkexec visudo 修改sudoer.temp 实例如下 ## This file MUST be edited with the 'visudo' command as ro ...