hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数
hdu1787,直接求欧拉函数
#include <iostream>
#include <cstdio>
using namespace std;
int n;
int phi(int n){
int ans=n;
for(int i=2; i*i<=n; i++)
if(n%i==0){
ans -= ans / i;
while(n%i==0) n /= i;
}
if(n>1) ans -= ans / n;
return ans;
}
int main(){
while(scanf("%d", &n)!=EOF){
if(!n) break;
printf("%d\n",n-phi(n)-1);
}
return 0;
}
poj2478,欧拉函数递推,证明可以看这里或者是算法竞赛进阶指南
\(n \log n\) 筛
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n;
long long phi[1000005];
void shai(){
for(int i=2; i<=1000000; i++)
phi[i] = i;
for(int i=2; i<=1000000; i++)
if(phi[i]==i)
for(int j=i; j<=1000000; j+=i)
phi[j] = phi[j] / i * (i - 1);
}
int main(){
shai();
for(int i=2; i<=1000000; i++)
phi[i] += phi[i-1];
while(scanf("%d", &n)!=EOF){
if(!n) break;
printf("%lld\n", phi[n]);
}
return 0;
}
线性筛:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, pri[1000005], cnt;
ll phi[1000005];
bool isp[1000005];
void shai(){
memset(isp, true, sizeof(isp));
isp[0] = isp[1] = false;
for(int i=2; i<=1000000; i++){
if(isp[i]) pri[++cnt] = i, phi[i] = i - 1;
for(int j=1; j<=cnt; j++){
if(i*pri[j]>1000000) break;
isp[i*pri[j]] = false;
if(i%pri[j]==0){
phi[i*pri[j]] = phi[i] * pri[j];//感性理解:12:1 5 7 11
break;
}
else phi[i*pri[j]] = phi[i] * (pri[j] - 1);//积性函数
}
}
}
int main(){
shai();
for(int i=2; i<=1000000; i++)
phi[i] += phi[i-1];
while(scanf("%d", &n)!=EOF){
if(!n) break;
printf("%lld\n", phi[n]);
}
return 0;
}
上面那种得到了素数,下面这种得到了最小质因子
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, pri[1000005], cnt, val[1000005];
long long phi[1000005];
void shai(){
for(int i=2; i<=1000000; i++){
if(!val[i]){
val[i] = i;
pri[++cnt] = i;
phi[i] = i - 1;
}
for(int j=1; j<=cnt; j++){
if(pri[j]>val[i] || pri[j]>1000000/i) break;
val[i*pri[j]] = pri[j];
phi[i*pri[j]] = phi[i]*(i%pri[j]?(pri[j]-1):pri[j]);
}
}
}
int main(){
shai();
for(int i=2; i<=1000000; i++)
phi[i] += phi[i-1];
while(scanf("%d", &n)!=EOF){
if(!n) break;
printf("%lld\n", phi[n]);
}
return 0;
}
hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数的更多相关文章
- poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)
http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...
- poj 2478 Farey Sequence 欧拉函数前缀和
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Description The Farey Sequence Fn for ...
- POJ2478 Farey Sequence —— 欧拉函数
题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K To ...
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- poj2478 Farey Sequence 欧拉函数的应用
仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值 的总和,为什么呢,因为要构成 a/b 然后不能约分 所以 gcd(a,b)==1,所以 分母 b的 欧拉函数值 ...
- UVA12995 Farey Sequence [欧拉函数,欧拉筛]
洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- Poj 2478-Farey Sequence 欧拉函数,素数,线性筛
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 5647 D ...
- POJ-2478-Farey Sequence(欧拉函数)
链接: https://vjudge.net/problem/POJ-2478 题意: The Farey Sequence Fn for any integer n with n >= 2 i ...
随机推荐
- Easy UI 关联表的字段展示
刚接触的easy UI ,发现展示关联表的字段的时候,卡住了 好一段时间,后来通过qq群询问,终于得到答案 实体Record public class Record:Base { public Gui ...
- 【js类库Raphaël】使用raphael.js根据点坐标绘制平滑曲线
一.可供参考的文档资料. raphaeljs官网:http://raphaeljs.com/ w3c关于path的介绍:http://www.w3.org/TR/2003/REC-SVG11-200 ...
- 本号讯 | 永不消失的协作“空间站”开课;微软推出微软云Azure文档网站
8月29日,针对企业常面临的“协同办公”困难,开展以“还有这种操作?永不消失的协作'空间站'”为主题的协同办公培训课. 课程内容包含:在Office 365环境中,如何利用Teams与Groups等功 ...
- python+selenium之处理HTML5的视频播放
from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get(" ...
- POJ 1845 Sumdiv (数学,乘法逆元)
题意: 给出数字A和B,要求AB的所有因子(包括AB和1)之和 mod 9901 的结果. 思路: 即使知道公式也得推算一阵子. 很容易知道,先把分解得到,那么得到,那么的所有因子之和的表达式如下: ...
- POJ 2184 Cow Exhibition 奶牛展(01背包,变形)
题意:有只奶牛要证明奶牛不笨,所以要带一些奶牛伙伴去证明自己.牛有智商和幽默感,两者可为负的(难在这),要求所有牛的智商和之 / 幽默感之和都不为负.求两者之和的最大值. 思路:每只牛可以带或不带上, ...
- 洛谷 P2827 蚯蚓
题目描述 本题中,我们将用符号\lfloor c \rfloor⌊c⌋表示对c向下取整,例如:\lfloor 3.0 \rfloor= \lfloor 3.1 \rfloor=\lfloor 3.9 ...
- python3.6 配置COCO API出错解决方案
使用Anaconda Prompt进行安装 问题出现的背景:在尝试使用mask-rcnn时,遇到了这个问题,最终解决掉了
- 2019全套Java视频 免费赠送
本人今年刚看完这套课程找到工作了 待遇还不错 现在送给大家 网盘链接:https://pan.baidu.com/s/1cEK6WoXS4F9SRgj1bZclqg提取码:bjl8希望对大家有用 一起 ...
- apropos linux
Apropos adj. 恰当的,关于,就...而言 adv. 顺便地,恰当地 All my suggestions apropos the script were accepted. 我所有有关该剧 ...