http://poj.org/problem?id=2478

http://acm.hdu.edu.cn/showproblem.php?pid=2824

欧拉函数模板裸题,有两种方法求出所有的欧拉函数,一是筛法,而是白书上的筛法。

首先看欧拉函数的性质:

  1. 欧拉函数是求小于n且和n互质(包括1)的正整数的个数。记为φ(n)。
  2. 欧拉定理:若a与n互质,那么有a^φ(n) ≡ 1(mod n),经常用于求乘法逆元。
  3. 若p是一个质数,那么φ(p) = p-1,注意φ(1) = 1。
  4. 欧拉函数是积性函数:(wikipedia:http://zh.wikipedia.org/wiki/%E7%A9%8D%E6%80%A7%E5%87%BD%E6%95%B8
    1. 若m与n互质,那么φ(nm) = φ(n) * φ(m)。
    2. 若n = p^k且p为质数,那么φ(n) = p^k - p^(k-1) = p^(k-1) * (p-1)。(证明还用说么。。。一共p^k能被p整除的就有p^(k-1)个。。所以总数减去即可。。
  5. 当n为奇数时,有φ(2*n) = φ(n)。
  6. $\sum_{d|n} \varphi (d) = n$,这个性质很重要!

基于素数筛的求欧拉函数的重要依据:

设a是n的质因数

若(n%a == 0 && (n/a)%a == 0) 则 φ(n) = φ(n/a)*a; (性质4的1和2推出,将n变成p1^a1*p2^a2...的形式,那么当前是a,即a^ax,那么根据φ(nm) = φ(n) * φ(m)先拆成m=a^ax,即φ(m)=a^(x-1)*(a-1),这样就是当a|m时乘上a-1,否则乘上a

若(n%a == 0 && (n/a)%a != 0) 则 φ(n) = φ(n/a)*φ(a)。(性质4的1推出)

素数筛:

poj 2478:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1000005;
bool isnotprime[N];
int prime[N], phi[N], cnt;
void init() {
phi[1]=1;
for1(i, 2, N-1) {
if(!isnotprime[i]) prime[++cnt]=i, phi[i]=i-1;
for(int j=1; j<=cnt && i*prime[j]<N; ++j) {
isnotprime[i*prime[j]]=1;
if(i%prime[j]==0) { phi[i*prime[j]]=phi[i]*prime[j]; break; }
else phi[i*prime[j]]=phi[i]*phi[prime[j]];
}
}
} int main() {
init(); int n;
while(n=getint(), n) {
long long ans=0;
for1(i, 2, n) ans+=phi[i];
printf("%lld\n", ans);
}
return 0;
}

hdu 2824:g++是I64d我也是醉了。。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=3000015;
bool isnotprime[N];
int prime[N], phi[N], cnt;
void init() {
phi[1]=1;
for1(i, 2, N-1) {
if(!isnotprime[i]) prime[++cnt]=i, phi[i]=i-1;
for(int j=1; j<=cnt && i*prime[j]<=N-1; ++j) {
int p=prime[j];
isnotprime[i*p]=1;
if(i%p==0) { phi[i*p]=phi[i]*p; break; }
else phi[i*p]=phi[i]*phi[p];
}
}
} int main() {
int l, r; init();
while(~scanf("%d%d", &l, &r)) {
long long ans=0;
for1(i, l, r) ans+=phi[i];
printf("%I64d\n", ans);
}
return 0;
}

还有一种筛法,不需要求素数。。。有待研究。复杂度比前一种多了两个log,是nloglogn的。。。orz。还是用线性的素数筛吧。。

欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function的更多相关文章

  1. hdu 2824 The Euler function(欧拉函数)

    题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...

  2. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)

    http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...

  4. hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数

    hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...

  5. HDU - 2824 The Euler function 欧拉函数筛 模板

    HDU - 2824 题意: 求[a,b]间的欧拉函数和.这道题卡内存,只能开一个数组. 思路: ϕ(n) = n * (p-1)/p * ... 可利用线性筛法求出所有ϕ(n) . #include ...

  6. hdu 2824 The Euler function 欧拉函数打表

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

  7. hdu 2824 The Euler function

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

  8. HDU——2824 The Euler function

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

  9. poj 2478 Farey Sequence 欧拉函数前缀和

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K       Description The Farey Sequence Fn for ...

  10. POJ 2478 Farey Sequence

     名字是法雷数列其实是欧拉phi函数              Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. ThreadPoolExecutor中策略的选择与工作队列的选择(java线程池)

    工作原理 1.线程池刚创建时,里面没有一个线程.任务队列是作为参数传进来的.不过,就算队列里面有任务,线程池也不会马上执行它们. 2.当调用 execute() 方法添加一个任务时,线程池会做如下判断 ...

  2. nginx根据token做频率限制

    在 nginx.conf 文件添加配置 limit_conn_log_level error; limit_conn_status ; limit_conn_zone $cookie_gray_DF_ ...

  3. Centos网络时好时超时问题解决

    近期公司使用的Centos突然出现网络不稳定现象,有公网,内部可以PING通外网,但是外部PING这个IP时,经常丢包现象,而且一丢包就是连续性的长达七八次,甚至十几次. 这个问题折腾了很长时间,因为 ...

  4. websphere设置jvm参数

    http://www.quiee.com.cn/archives/592/ websphere 选择服务器-> 应用程序服务器-> Server1-> 进程定义->Java 虚 ...

  5. [Asp.net]Calendar+JqueryUi实现日程管理(右键菜单,添加,编辑,删除,源码)

    引言 出差终于回来了,这篇文章算是这个月的博客的开篇吧. 上篇文章:[Asp.net]Calendar+JqueryUi实现日程管理——添加日程 上篇文章主要贴了一些该项目的界面,这里面,将主要代码也 ...

  6. 你所不知道的 CSS 阴影技巧与细节 滚动视差?CSS 不在话下 神奇的选择器 :focus-within 当角色转换为面试官之后 NPOI 教程 - 3.2 打印相关设置 前端XSS相关整理 委托入门案例

    你所不知道的 CSS 阴影技巧与细节   关于 CSS 阴影,之前已经有写过一篇,box-shadow 与 filter:drop-shadow 详解及奇技淫巧,介绍了一些关于 box-shadow  ...

  7. 用户 'NT AUTHORITY\IUSR' 登录失败

    今天在用VS20012发布XAF ASP.NET的程序时,在iis 调用SQLSERVER Express2008数据库时,总是出现错误“用户 'NT AUTHORITY\IUSR' 登录失败”,后来 ...

  8. Java类载入器 ClassLoader的解析

    //參考 : http://www.ibm.com/developerworks/cn/java/j-lo-classloader/ 类载入器基本概念 类载入器是 Java 语言的一个创新,也是 Ja ...

  9. 基于Bootstrap的Asp.net Mvc 分页的实现(转)

    最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这 边是模拟了一 ...

  10. 基于注解配置spring

    1 对 bean 的标注基于注解方式有3个注解 @Component @Repository 对DAO类进行标注 @Service 对Service类进行标注 @Controller  对Contro ...