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

求欧拉函数的模板。

初涉欧拉函数,先学一学它主要的性质。

1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数。

记为φ(n)。

2.欧拉定理:若a与n互质。那么有a^φ(n) ≡ 1(mod n),经经常使用于求幂的模。

3.若p是一个质数,那么φ(p) = p-1。注意φ(1) = 1。

4.欧拉函数是积性函数:

若m与n互质,那么φ(nm) = φ(n) * φ(m)。

若n = p^k且p为质数,那么φ(n) = p^k - p^(k-1) = p^(k-1) * (p-1)。

5.当n为奇数时,有φ(2*n) = φ(n)。

6.基于素数筛的求欧拉函数的重要根据:

设a是n的质因数,若(N%a == 0 && (N/a)%a == 0) 则 φ(N) = φ(N/a)*a; 若(N%a == 0 && (N/a)%a != 0) 则φ(N) = φ(N/a)*(a-1)。



该题就是基于性质六,在线性时间内求欧拉函数。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#define LL long long
#define _LL __int64
#define eps 1e-8
#define PI acos(-1.0) using namespace std;
const int maxn = 1000010;
const int INF = 0x3f3f3f3f; int n;
LL num[maxn]; LL phi[maxn]; //相应φ(i)
int flag[maxn]; //flag[i] = 0说明i是素数。否则不是素数
int prime[maxn];//存素数 void get_phi()
{
int i,j,k;
memset(flag,0,sizeof(flag));
phi[1] = 1;
k = 0; for(i = 2; i <= maxn; i++)
{
if(!flag[i]) //i是素数
{
phi[i] = i-1;
prime[++k] = i;
}
for(j = 1; j <= k && prime[j]*i <= maxn; j++)
{
flag[i*prime[j]] = 1;
if(i % prime[j] == 0)
phi[i*prime[j]] = phi[i] * prime[j];
else phi[i*prime[j]] = phi[i] * (prime[j]-1);
}
}
} int main()
{
get_phi();
num[1] = 0;
for(int i = 2; i <= maxn; i++)
num[i] = num[i-1] + phi[i]; while(~scanf("%d",&n)&&n)
printf("%lld\n",num[n]); return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)的更多相关文章

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

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

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

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

  3. POJ2478 Farey Sequence —— 欧拉函数

    题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K To ...

  4. poj2478 Farey Sequence (欧拉函数)

    Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...

  5. poj2478 Farey Sequence 欧拉函数的应用

    仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值  的总和,为什么呢,因为要构成 a/b 然后不能约分  所以 gcd(a,b)==1,所以  分母 b的 欧拉函数值   ...

  6. UVA12995 Farey Sequence [欧拉函数,欧拉筛]

    洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...

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

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

  8. Poj 2478-Farey Sequence 欧拉函数,素数,线性筛

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 5647 D ...

  9. POJ-2478-Farey Sequence(欧拉函数)

    链接: https://vjudge.net/problem/POJ-2478 题意: The Farey Sequence Fn for any integer n with n >= 2 i ...

随机推荐

  1. SLIC superpixel实现分析

    http://infoscience.epfl.ch/record/149300这是SLIC算法的官网,网站有和SLIC相关的资源. SLIC主要运用K-means聚类算法进行超像素的处理,聚类算法中 ...

  2. 不显示系统错误对话框SetErrorMode(要学会搜索)

    关闭程序时报dde server window错误有人碰到过吗,用的别人的一个OCX控件,把这个控件去掉就不会报这个错误 //不显示系统错误对话框 SetErrorMode(SEM_NOGPFAULT ...

  3. 【Cocos2d-X开发学习笔记】第01期:PC开发环境的详细搭建

    本文使用的是cocos2d-x-2.1.4版本 ,截至目前为止是最新稳定版 所谓的开发环境就是制作游戏的地方,打个比方读者就会十分清楚了.比如提到做饭,人们都会想到厨房.这是 因为厨房有炉灶.烟机.水 ...

  4. 日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo

    日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo <%@ page contentType="t ...

  5. codechef Sums in a Triangle题解

    Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...

  6. 如何不屏蔽Android系统的返回按键

    比如: 第一种方法: public boolean onKeyDown(int keyCode, KeyEvent event) {         // TODO Auto-generated me ...

  7. Eclipse扩展点实践之添加快捷菜单项(Command方式实现)

    有两种方式,一种是Action的方式,另一种是Command的方式(这两种方式的区别详见:http://wiki.eclipse.org/FAQ_What_is_the_difference_betw ...

  8. 用链表实现栈----《数据结构与算法分析----C语言描述》

    一.头文件: #ifndef _STACK_LINK_H_ #define _STACK_LINK_H_ struct stack_record; typedef struct stack_recor ...

  9. Java程序猿笔试面试之String1

    1.怎样实现字符串的反转比如:"how are you"--->"you are how" public class InverseString { pu ...

  10. Android SDK 5.0 这个语句带来折腾 - 生命在于折腾!

    Android SDK 5.0  带来的这番折腾 - 生命在于折腾! 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一 ...