【题目大意】

求∑φ(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. POJ1456:Supermarket(并查集+贪心)

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17634   Accepted: 7920 题目链接 ...

  2. sperman系数

    https://baike.baidu.com/item/spearman%E7%9B%B8%E5%85%B3%E7%B3%BB%E6%95%B0/7977847?fr=aladdin https:/ ...

  3. Sencha Touch MVC 中 store 的使用

    I have a UserStore that I want to load after succesful login of a user. I can't get this to work i.e ...

  4. input输入浮动提示

    html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  5. Java并发(4)- synchronized与CAS

    引言 上一篇文章中我们说过,volatile通过lock指令保证了可见性.有序性以及"部分"原子性.但在大部分并发问题中,都需要保证操作的原子性,volatile并不具有该功能,这 ...

  6. 【BZOJ2223&&3524】PATULJCI [主席树]

    PATULJCI Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description Input 第一行两个整数 ...

  7. 河南省第十届省赛 Binary to Prime

    题目描述: To facilitate the analysis of  a DNA sequence,  a DNA sequence is represented by a binary  num ...

  8. NYOJ 284 坦克大战 (广搜)

    题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...

  9. centos python2.6升级到2.7出现的问题

    centos自带的python版本为2.6,在使用tornado时会出现如下报错: NameError: global name 'memoryview' is not defined 因此需要升级到 ...

  10. 使用MXNet远程编写卷积神经网络用于多标签分类

    最近试试深度学习能做点什么事情.MXNet是一个与Tensorflow类似的开源深度学习框架,在GPU显存利用率上效率高,比起Tensorflow显著节约显存,并且天生支持分布式深度学习,单机多卡.多 ...