洛谷 - P2568 - GCD - 欧拉函数
https://www.luogu.org/problemnew/show/P2568
统计n以内gcd为质数的数的个数。
求 \(\sum\limits_p \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n} [gcd(i,j)==p]\)
一开始还以为要莫比乌斯反演.
推了半天不知道怎么求,遂看题解:
$\sum\limits_p \sum\limits_{i=1}{n}\sum\limits_{j=1}{n} [gcd(i,j)p] =\sum\limits_p \sum\limits_{i=1}{\frac{n}{p}}\sum\limits_{j=1}{\frac{n}{p}} [gcd(i,j)1] $
一个有序数对 \((i,j),(i>j)\) 与 \(i\) 互质的数 \(j\) 的个数也就是 \(\varphi(i)\) ,画一个正方形可以知道对调 \((i,j)\) 求出一样的结果.
但是当 $ i1&&j1 $ 时被重复计数了,要减去
那么答案就是 $\sum\limits_p (2*\sum\limits_{i=1}^{\frac{n}{p}}\varphi(i) - 1) $
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 10000005
int phi[N],pri[N],cntpri=0;
bool notpri[N];
ll prefix[N];
void sieve_phi(int n) {
notpri[1]=phi[1]=1;
prefix[0]=0;
prefix[1]=1;
for(int i=2; i<=n; i++) {
if(!notpri[i])
pri[++cntpri]=i,phi[i]=i-1;
for(int j=1; j<=cntpri&&i*pri[j]<=n; j++) {
notpri[i*pri[j]]=1;
if(i%pri[j])
phi[i*pri[j]]=phi[i]*phi[pri[j]];
else {
phi[i*pri[j]]=phi[i]*pri[j];
break;
}
}
prefix[i]=prefix[i-1]+phi[i];
}
}
ll solve(ll n){
ll ans=0;
for(int i=1;i<=cntpri;i++){
if(pri[i]<=n){
ans+=2ll*(prefix[n/pri[i]])-1ll;
}
}
return ans;
}
int main() {
sieve_phi(10000000+1);
int n;
while(cin>>n) {
ll ans=solve(n);
cout<<ans<<endl;
}
}
洛谷 - P2568 - GCD - 欧拉函数的更多相关文章
- 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)
P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...
- 洛谷 - P2158 - 仪仗队 - 欧拉函数
https://www.luogu.org/problemnew/show/P2158 好像以前有个妹子收割铲也是欧拉函数. 因为格点直线上的点,dx与dy的gcd相同,画个图就觉得是欧拉函数.但是要 ...
- 洛谷P2568 GCD(线性筛法)
题目链接:传送门 题目: 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 ...
- 洛谷 P2568 GCD
https://www.luogu.org/problemnew/show/P2568#sub 最喜欢题面简洁的题目了. 本题为求两个数的gcd是素数,那么我们将x和y拆一下, 假设p为$gcd(x, ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- POJ 2773 Happy 2006【GCD/欧拉函数】
根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- Bzoj-2818 Gcd 欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...
- BZOJ2818: Gcd 欧拉函数求前缀和
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...
随机推荐
- 怎样高速编译mediatek\operator以下代码
mediatek\operator以下有单独的apk.也有overlay的数据,单独的apk会配置anroid.mk,找到相应的路径直接build. 假设是overlay,则编译原来应用的路径,比如 ...
- iOS菜鸟学习--怎样避免两个button同一时候响应
在測试应用时.有时会变态的将两个UIButton同一时候按住来測试.结果就是两个button会同一时候响应,会出现同一时候push两个viewcontroller等非正常情况.为了避免用户误操作造成这 ...
- Mvc Autofac构造器注入
新建MVC项目,添加程序集引用 定义接口ILog public interface ILog { string Save(string message); } 类TxtLog实现接口ILog publ ...
- LeetCode(125)题解--Valid Palindrome
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...
- 创建私有maven库
在公司搭建私有maven库,我为了在本机做测试,我选择Sonatype nexus oss,在docker里面安装 1.Mac上安装docker 下载地址:https://github.com/boo ...
- UIBezierPath(转)
@import url(/css/cuteeditor.css); @import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com ...
- SAM初步
SAM(Suffix Automaton),后缀自动机. SAM是种十分神奇的数据结构,我认为他的主要神奇之处,在于最大限度的利用了分类思想. SAM上有两种边,代表两种转移方式. 一种是树边,一种是 ...
- 基于sys文件系统的LED驱动的移植【原创】
基于RK3188平台LED驱动程序的移植的移植.如有不正确之处,欢迎大家指点. 本文的LED驱动程序不是通过打开设备节点来访问和控制LED的,是通过sys文件系统来控制LED. 板子上有四盏灯以及对应 ...
- BluetoothLE-Multi-Library 一个能够连接多台蓝牙设备的库,它可以作为client端,也可以为server端。支持主机/从机,外围设备连接。
github地址:https://github.com/qindachang/BluetoothLE-Multi-Library BluetoothLE-Multi-Library 一个能够连接多台蓝 ...
- hdu-5750 Dertouzos(数论)
题目链接: Dertouzos Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...