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 ...
随机推荐
- Java中的日志框架
日志框架的介绍和使用 常见的日志框架:JUL(Java.util.logging),JCL(jakarta commons logging),SLF4J,jboss-logging,Log4j,Log ...
- Setting 之dashboard 点击跳转流程
设置的主界面的可以通过修改xml中的dashboard_categaries.xml 文件实现,在DashboardSummary.java 文件中的rebuildUI()方法中将xml对应的实体类转 ...
- SQL Server Sleeping会话占用内存资源浅析?
在SQL Server中,会话的状态有运行(Running).睡眠(Sleeping).休眠(Dormant).Preconnect 等状态,有时候你会在数据库中看到很多会话处于睡眠(Sleepi ...
- Outlook 客户端无法通过 MAPI over HTTP协议 连接
随着Exchange 版本更新升级,是否进行验证客户端建立MapiHttp连接所需的服务器设置已正确配置.即使服务器,负载均衡器和反向代理的所有设置都正确,您可能会遇到连接到Exchange Serv ...
- Python+selenium之带unittest的脚本分析
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...
- python基础教程总结12——数据库
1. Python 数据库 API 很多支持SQL标准的数据库在Python中都有对应的客户端模块.为了在提供相同功能(基本相同)的不同模块之间进行切换(兼容),Python 规定了一个标准的 DB ...
- 如何处理错误消息Please install the Linux kernel header files
Please install the Linux kernel "header" files matching the current kernel 当我启动minilkube时遇 ...
- ABAP,Java和JavaScript的序列化,反序列化
ABAP 1. ABAP提供了一个工具类cl_proxy_xml_transform,通过它的两个方法abap_to_xml_xstring和xml_xstring_to_abap实现两种格式的互换. ...
- SAP C4C Opportunity和SAP ERP Sales流程的集成
首先在C4C里创建一个新的Opportunity: 给这个Opportunity添加一个新的产品: 点按钮:Request Pricing, 从ERP抓取pricing数据,点按钮之前Negotiat ...
- 字符编码:BSTR
typedef wchar_t WCHAR; typedef WCHAR OLECHAR; typedef OLECHAR __RPC_FAR *BSTR;;