题意:$\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd(i,j)\epsilon prime]$。

对于这类题一般就是枚举gcd,可得:

=$\sum_{d\epsilon prime}\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd(i,j)==d]$

=$\sum_{d\epsilon prime}\sum_{i=1}^{{\lfloor \frac{n}{d}\rfloor}}\mu(i){\lfloor \frac{n}{id}\rfloor}{\lfloor \frac{n}{id}\rfloor}$

预处理素数,莫比乌斯前缀和,后面部分整除分块。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e7+;
bool p[N];
int pri[N],mu[N],tot;
void init() {
mu[]=;
for(int i=;i<N;i++) {
if(!p[i]) pri[tot++]=i,mu[i]=-;
for(int j=;j<tot&&i*pri[j]<N;j++) {
p[i*pri[j]]=true;
if(i%pri[j]==) {
mu[i*pri[j]]=;
break;
}
else mu[i*pri[j]]=-mu[i];
}
}
for(int i=;i<N;i++) mu[i]+=mu[i-];
}
ll cal(int n) {
ll ans=;
for(int l=,r;l<=n;l=r+) {
r=n/(n/l);
ans+=1LL*(mu[r]-mu[l-])*(n/l)*(n/l);
}
return ans;
}
int main() {
init();
int n;
scanf("%d",&n);
ll ans=;
for(int i=;i<tot&&pri[i]<=n;i++) {
ans+=cal(n/pri[i]);
}
printf("%lld\n",ans);
return ;
}

洛谷 P2568 GCD(莫比乌斯反演)的更多相关文章

  1. 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)

    P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...

  2. 洛谷 - P2568 - GCD - 欧拉函数

    https://www.luogu.org/problemnew/show/P2568 统计n以内gcd为质数的数的个数. 求 \(\sum\limits_p \sum\limits_{i=1}^{n ...

  3. 洛谷P2568 GCD(线性筛法)

    题目链接:传送门 题目: 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 ...

  4. [洛谷P2568]GCD

    题目大意:给你$n(1\leqslant n\leqslant 10^7)$,求$\displaystyle\sum\limits_{x=1}^n\displaystyle\sum\limits_{y ...

  5. 洛谷P3935 Calculating (莫比乌斯反演)

    P3935 Calculating 题目描述 若xx分解质因数结果为\(x=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n},令f(x)=(k_1+1)(k_2+1)\cdots ...

  6. 洛谷P2568 GCD(莫比乌斯反演)

    传送门 这题和p2257一样……不过是n和m相同而已…… 所以虽然正解是欧拉函数然而直接改改就行了所以懒得再码一遍了2333 不过这题卡空间,记得mu开short,vis开bool //minamot ...

  7. 洛谷 P2568 GCD

    https://www.luogu.org/problemnew/show/P2568#sub 最喜欢题面简洁的题目了. 本题为求两个数的gcd是素数,那么我们将x和y拆一下, 假设p为$gcd(x, ...

  8. 洛谷 P2568 GCD 题解

    原题链接 庆祝一下:数论紫题达成成就! 第一道数论紫题.写个题解庆祝一下吧. 简要题意:求 \[\sum_{i=1}^n \sum_{j=1}^n [gcd(i,j)==p] \] 其中 \(p\) ...

  9. 洛谷P2398 GCD SUM (数学)

    洛谷P2398 GCD SUM 题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 给出n求sum. gcd(x,y)表示x,y的最大公约数. 输入输出格式 输入 ...

随机推荐

  1. kafka例子程序

    //生产端 产生数据 /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor li ...

  2. 解决hive无法传参问题思路

    由于hive执行过程中参数必须写死,无法进行传递参数.利用shell脚本和java编程进行解决 #!/bin/sh#date = `date -d -1days '+%Y-%m-%d'`if [ $# ...

  3. 图像通道、Scalar、分离、合成通道

    http://lib.csdn.net/article/opencv/33264 http://blog.csdn.net/laohu_tiger/article/details/17359777 h ...

  4. bzoj 2503 相框——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2503 我也知道应该只关注度数. #include<iostream> #incl ...

  5. TZ_10_spring-sucrity 服务器和页面的权限控制

    1.在服务器端我们可以通过Spring security提供的注解对方法来进行权限控制. Spring Security在方法的权限控制上支持三种类型的注解,JSR-250注解.@Secured注解和 ...

  6. Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: No Persistence provider for EntityManager named null

    swing Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: No Pe ...

  7. hdoj 1325 Is It A Tree? 【并查集】

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shengweisong/article/details/34099151 做了一上午,最终ac了 w ...

  8. linux-jdk-mysql-tomcat安装

    1.JDK安装 注意:rpm与软件相关命令 相当于window下的软件助手 管理软件 步骤: 1)查看当前Linux系统是否已经安装java 输入 rpm -qa | grep java 1)卸载两个 ...

  9. linux系统服务

    系统服务分类,根据其使用的方法来分,可以被分为三类 a.由init控制的服务:基本都是系统级别的服务,运行级别这一章讲的就是这一类的服务 b.由System V启动脚本启动的服务:和我们打交道最多的一 ...

  10. Javascript实现信息滚动效果的方法

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...