裸题

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)的更多相关文章

  1. hdu 2824(欧拉函数)

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  3. hdu 2654(欧拉函数)

    Become A Hero Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. hdu 1395(欧拉函数)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. hdu 3307(欧拉函数+好题)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  6. 找新朋友 HDU - 1286 欧拉函数模板题

    题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...

  7. *HDU 1286,2824欧拉函数

    #include<iostream> #include<string> #include<cstdio> #include<cmath> #includ ...

  8. hdu 4983 欧拉函数

    http://acm.hdu.edu.cn/showproblem.php?pid=4983 求有多少对元组满足题目中的公式. 对于K=1的情况,等价于gcd(A, N) * gcd(B, N) = ...

  9. 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是 ...

随机推荐

  1. ACCESS 查询重复记录

    In (SELECT [全称] FROM [New14] As Tmp GROUP BY [全称],[账号],[银行] HAVING Count(*)>1  And [账号] = [New14] ...

  2. Mongodb Mysql NoSQL的区别和联系

    MongoDB 什么是MongoDB? MongoDB是一个基于分布式文件存储的数据库,由C++语言编写,皆在为WEB应用提供可扩展的高性能数据存储解决方案 MongoDB是一个介于关系数据库和非关系 ...

  3. linux安装命令出错(could not resolve host mirrorlist.centos.org)

    本次问题出现在虚拟机安装的centos7系统之后,使用yum命令安装wget命令(yum -y install wget),出现could not resolve host mirrorlist.ce ...

  4. Python3的基础

    Python的3.0版本,常被称为Python 3000,或简称Py3k. 关于Python版本的下载:https://www.python.org/,以及Anaconda的下载:https://ww ...

  5. Visual Assist X 中使用doxygen的模板注释

    http://blog.csdn.net/dhifnoju/article/details/44947213 Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,完全支持C.C+ ...

  6. 浅谈java 之 Map

    先来一张Map的类继承图 Map :Hashtable .HashMap .LinkedHashMap .TreeMap 的比较   1.Hashtable的方法实现了synchronized 是线程 ...

  7. vue搭建

    ~感觉再不学点新技能,马上就要被淘汰了== 1.先安装node,直接点击下一步就OK  node官网   https://nodejs.org/en/ 2.安装完成之后,打开命令行工具,快捷键是cmd ...

  8. impala和kudu使用的小细节

    七堇年:我们要有最朴素的生活与最遥远的梦想 . 即使明日天寒地冻,路远马亡.   加油! 之前入门的小错误总结,建表都会出错,真的好尴尬 还是要做好笔记 第一个错误: error:AnalysisEx ...

  9. Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks 阅读笔记

    Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks (使用循环一致的对抗网络的非配对图像-图 ...

  10. MySQL存储过程中实现执行动态SQL语句

    sql语句中的任何部分都可以作为参数. DROP PROCEDURE if exists insertdata; delimiter //CREATE PROCEDURE insertdata(IN ...