裸题

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. FORTH 虚拟机内部结构

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. input输入框失去焦点,软键盘关闭后,滚动的页面无法恢复到原来位置

    H5微信页面开发,软键盘弹起后,若原输入框被遮挡,页面整体将会上移,然而当输入框失焦,软键盘收起后,页面未恢复,导致弹框里的按钮响应区域错位. 解决方案:给输入框(或select选择框)添加失去焦点的 ...

  3. c# 类的历遍和历遍操作

    string id = Request.Form["id"]; string type = Request.Form["type"]; string info ...

  4. spring中ApplicationListener的用法

    1.实现ApplicationListener接口,并重写onApplicationEvent方法 @Component public class RSAKeyInitListener impleme ...

  5. kali linux 配置嵌入式开发环境

    kali linux 2018.2 x64 一.支持i386库 如果你是64位的Kali Linux系统,用如下命令添加i386架构支持到你的开发环境. dpkg --add-architecture ...

  6. java 五十条数据分为一组

    public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for(int ...

  7. better-scroll

    better-scroll会将默认事件阻止掉,如果自己写的部分需要有点击事件,需要在参数里加上click:true. 同时,在PC上或某些手机端,由于未成功将touchend事件move掉,点击事件会 ...

  8. chrome console.log失效

    把红框里的内容去掉就可以了 那个框是过滤..

  9. CentOS系统Nginx安装配置,随时更新

    ./configure --prefix=/etc/nginx \ #指定安装目录 --sbin-path=/usr/sbin/nginx \ #指定执行路径--conf-path=/etc/ngin ...

  10. [dedecms]隐藏栏目不生成静态页面

    重点文件在:/data/cache/inc_catalog_base.inc.php文件,里面存放的是栏目ID 先找到 /dede/sys_cache_up.php(后台更新缓存的地方),找到里面的U ...