[bzoj 2818]欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818
枚举最大公约数,对于每一个质数p,只需要求出1<=x,y<=(n/p)范围内gcd(x,y)=1的对数,而这个对数就是类似欧拉函数的一个前缀和。
#include<cstdio>
#include<cstring>
using namespace std; const int maxn=;
bool check[maxn];
int prime[maxn];
long long phi[maxn];
long long prephi[maxn]; void getPrime()
{
int N=;
memset(check,false,sizeof(check));
prime[]=;
phi[]=;
for (int i=;i<=N;i++)
{
if (!check[i])
{
prime[++prime[]]=i;
phi[i]=i-;
}
for (int j=;j<=prime[];j++)
{
if (i*prime[j]>N) break;
check[i*prime[j]]=true;
if (i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else
{
phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
}
prephi[]=phi[];
for (int i=;i<=N;i++) prephi[i]=prephi[i-]+2ll*phi[i];
} int main()
{
getPrime();
int n;
scanf("%d",&n);
long long ans=;
for (int i=;i<=prime[];i++)
{
if (prime[i]>n) break;
int r=n/prime[i];
ans+=prephi[r];
}
printf("%lld\n",ans);
return ;
}
[bzoj 2818]欧拉函数的更多相关文章
- [BZOJ]4805: 欧拉函数求和
解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i) ...
- BZOJ 4802 欧拉函数
4802: 欧拉函数 Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Outp ...
- BZOJ 4802 欧拉函数(Pollard_Rho)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题目大意] 已知N,求phi(N),N<=10^18 [题解] 我们用P ...
- BZOJ 2190 欧拉函数
思路: 递推出来欧拉函数 搞个前缀和 sum[n-1]*2+3就是答案 假设仪仗队是从零开始的 视线能看见的地方就是gcd(x,y)=1的地方 倒过来一样 刨掉(1,1) 就是ans*2+1 再加一下 ...
- BZOJ 4805: 欧拉函数求和 杜教筛
https://www.lydsy.com/JudgeOnline/problem.php?id=4805 给出一个数字N,求sigma(phi(i)),1<=i<=N https://b ...
- 【刷题】BZOJ 4805 欧拉函数求和
Description 给出一个数字N,求sigma(phi(i)),1<=i<=N Input 正整数N.N<=2*10^9 Output 输出答案. Sample Input 1 ...
- BZOJ 4802: 欧拉函数 (Pollard-Rho)
开始一直T,原来是没有srand- CODE #include<bits/stdc++.h> using namespace std; typedef long long LL; vect ...
- bzoj 2818 GCD 数论 欧拉函数
bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
随机推荐
- Python学习之property
Python中使用Property函数可以将类中的函数当作属性来调用. 案例 __metaclass__=type class Rectangle: def __init__(self): self. ...
- 10-C++远征之模板篇-学习笔记
C++远征之模板篇 将会学到的内容: 模板函数 & 模板类 -> 标准模板类 友元函数 & 友元类 静态数据成员 & 静态成员函数 运算符重载: 一切皆有可能 友元函数 ...
- 004---Python基本数据类型--元祖
元祖 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px ...
- JAVA 泛型方法<T>
public static void main(String[] args) throws Exception { String[] arr = new String[]{"1", ...
- DAG上dp思想
DAG上DP的思想 在下最近刷了几道DAG图上dp的题目.要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点.第二道是洛谷上的NOI导刊题 ...
- pxe无人值守安装linux机器笔记----摘抄
1. 基建工作 1.关闭防火墙 a)service iptables stop b)service ip6tables stop c)chkconfig iptables off d)chkconfi ...
- 用链表实现nodejs的内存对象管理
虽然javascript拥有垃圾收集,但是垃圾收集机制并不会自动释放持久对象,比如websocks连接. 为了能够在某些特定情况下中止一些连接(比如内存不足),显然要建立全局的对象管理器进行管理. 显 ...
- iframe底边多出4px或5px解决办法
问题: 在处理iframe框架自适应时,并且已经去掉iframe的边框,但仍然出现底边多出4px或5px高度的情况.如图 <div id="content"> < ...
- HDU 4587 TWO NODES(割点)(2013 ACM-ICPC南京赛区全国邀请赛)
Description Suppose that G is an undirected graph, and the value of stab is defined as follows: Amon ...
- POJ 2166 Heapsort(递推)
Description A well known algorithm called heapsort is a deterministic sorting algorithm taking O(n l ...