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是 ...
随机推荐
- go报错unimplemented: 64-bit mode not compiled in与mingw 64位安装报错ERROR res已解决
问题一:cc1.exe: sorry, unimplemented: 64-bit mode not compiled in 参考:https://www.cnblogs.com/lesroad/p/ ...
- work2:贪吃蛇
学号:2017*****7219 姓名:邰嘉琛我的码云贪吃蛇项目仓库:https://gitee.com/tjc666/sesnake/tree/master2) 给出你的各项任务完成时间估算与实际消 ...
- Python的进程与线程--思维导图
Python的进程与线程--思维导图
- Java NIO学习之Buffer
Bufer的capacity,position和limit: capacity: 表示buffer的容量. position: 写数据到Buffer中时: 表示当前的位置.初始的position值为0 ...
- SQLServer学习记录
use TestDataBase;go -- 派生表-- 第3页,每页5条数据select * from (select ROW_NUMBER() over(order by stuId) as nu ...
- pyCharm中BeautifulSoup应用
BeautifulSoup 是第三方库的工具,它包含在一个名为bs4的文件包中,需要额外安装,安装方式 非常简单,进入python的安装目录,再进入scripts子目录,找到pip程序, pip in ...
- Python 字典dict相关知识
字典是无序的,多次print输出的结果不一样. 字典的key可以是数字.字符串.元组.布尔值(True为1,False为0,不可以和其他key值重复):列表和字典不能作为key. 字典的value可以 ...
- Jest 学习笔记(一)之matchers
Jest官网地址 Jest是专门被facebook用于测试包括React应用在内的所有javascript代码,Jest旨在提供一个综合的零计算的测试体验. 因为没有找到文档,基于我个人的经验,Jes ...
- MSBuild 命令参数
Build a Visual Studio project or solution using MSBuild Command Line Arguments 常用命令行参数 详解: MSBuild ...
- css设置两行多余文字用..显示
display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; word-break: break-all;