【题目大意】

求∑φ(i)(1<=i<=N)。

【思路】

欧拉函数具有如下的重要推论:

当b是素数时

性质①若b|a,有φ(ab)=φ(a)*b;

性质②若b不|a,有φ(ab)=φ(a)*(b-1)。

由此可以得出递推求欧拉函数表的方法:

对于当前φ(i),若未被修改过,这说明它是素数,加入素数表。

对于每个i,枚举小于它的所有素数j。利用性质1和性质2求出φ(ij)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=+;
typedef long long ll;
int n,maxn;
int phi[MAXN],p[MAXN],input[MAXN];
ll s[MAXN];
int t=; void eular_table()
{
memset(phi,,sizeof(phi));
memset(p,,sizeof(p));
p[]=;p[]=;phi[]=;
for (int i=;i<=maxn;i++)
{
if (phi[i]==)
{
p[++p[]]=i;
phi[i]=i-;
}
for (int j=;j<=p[];j++)
{
if (i*p[j]<=maxn) phi[i*p[j]]=(i%p[j]==)? phi[i]*p[j] : phi[i]*(p[j]-);
//注意一定要保证i*p[j]没有超出数组上界,否则RE
else break;
}
}
} void printans()
{
s[]=;
for (int i=;i<=maxn;i++)
s[i]=s[i-]+phi[i];
for (int i=;i<t;i++)
printf("%lld\n",s[input[i]]);
} int main()
{
while (~scanf("%d",&n) && n!=)
{
input[t++]=n;
maxn=max(maxn,n);
}
eular_table();
printans();
return ;
}

【欧拉函数表】POJ2478-Farey Sequence的更多相关文章

  1. bzoj 2190 线性生成欧拉函数表

    首先我们知道,正方形内个是对称的,关于y=x对称,所以只需要算出来一半的人数 然后乘2+1就行了,+1是(1,1)这个点 开始我先想的递推 那么我们对于一半的三角形,一列一列的看,假设已经求好了第I- ...

  2. UVA - 11426 欧拉函数(欧拉函数表)

    题意: 给一个数 N ,求 N 范围内所有任意两个数的最大公约数的和. 思路: f 数组存的是第 n 项的 1~n-1 与 n 的gcd的和,sum数组存的是 f 数组的前缀和. sum[n]=f[1 ...

  3. UVa 11426 - GCD - Extreme (II) 转化+筛法生成欧拉函数表

    <训练指南>p.125 设f[n] = gcd(1, n) + gcd(2, n) + …… + gcd(n - 1, n); 则所求答案为S[n] = f[2]+f[3]+……+f[n] ...

  4. POJ2478 Farey Sequence

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15023   Accepted: 5962 Description The ...

  5. POJ2478 Farey Sequence —— 欧拉函数

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

  6. poj2478 Farey Sequence (欧拉函数)

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

  7. POJ2478 - Farey Sequence(法雷级数&&欧拉函数)

    题目大意 直接看原文吧.... The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rat ...

  8. poj2478——Farey Sequence(欧拉函数)

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18507   Accepted: 7429 D ...

  9. poj-2478 Farey Sequence(dp,欧拉函数)

    题目链接: Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14230   Accepted:  ...

随机推荐

  1. POJ3020:Antenna Placement(二分图匹配)

    Antnna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11093   Accepted: 5459 ...

  2. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path

    D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的 ...

  3. poj 2378 Tree Cutting 树形dp

    After Farmer John realized that Bessie had installed a "tree-shaped" network among his N ( ...

  4. Extend the size of ext3 partition online in VM

    1. Increase disk space in vCenter 2. scan disk on the Linux VM # fdisk -lu > /tmp/fdisk. # > / ...

  5. 用eval转化对象

    var str = '{"name": "tom","age": 12,"sex": "man"}' ...

  6. php2go - Go 实现 PHP 常用内置函数

    [转]http://www.syyong.com/Go/php2go-Use-Golang-to-implement-PHP-s-common-built-in-functions.html 使用Go ...

  7. codevs1245最小的N个和 小根堆

    这道题 将b排序一下 将a(i~n)和b1加起来入堆 每次出一次队(ax+bi) 将(ax+bi+1)入队就好了 能保证答案的正确性 找个自己证明吧 #include<cstdio> #i ...

  8. [BZOJ3698]XWW的难题解题报告|上下界网络流|有源汇最大流

    XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核.XWW给你出了这么一个难题:XWW给你一个N*N的正实数矩阵A,满足XW ...

  9. NPOI的使用Excel模板导出 可插入到指定行

    Excel模版建议把需要添加数据行的样式设置好 模版样式,导出后效果 [2017-11-22 对获取需插入数据的首行样式有时为空报错修改] /// <summary> /// 根据模版导出 ...

  10. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...