CF871D Paths
题意:
n个点的无向图,若$\gcd(x,y) \neq1$则$(x,y)$有边,统计$1\sim n$构成的无向图两两点对最短路是之和是多少(两点不连通最短路记为0)?$n\leq 10^7$。
题解:
先分类讨论一下:
- 1和$>\frac n2$的素数是孤立点,排除掉,其余是一个联通块。
- $\gcd(x,y)\neq1\longrightarrow dis(x,y)=1$
- 记$mi[x]$为x的最小素因子,$mi[x]\times mi[y]\leq n\longrightarrow dis(x,y)=2$
- 其余均可通过$x \longrightarrow 2\times mi[x] \longrightarrow 2\times mi[y] \longrightarrow y$实现$dis(x,y)=3$
分别考虑:
- 直接排除即可
- 方案数$=\sum_x x-1-\varphi(x)$
- 方案数$=\sum_{x,y}[\gcd(x,y)=1][mi[x]\times mi[y] \leq n]$
- 剩余点对
考虑第3种情况怎么求:
“看到$\gcd$想反演”:
$$
\begin{aligned}
ans&=\sum_{d=1}^n\mu(d)\sum_{d|x}\sum_{d|y}[mi[x]\times mi[y]\leq n]\\
&=\sum_{x=1}^n\sum_{y=1}^n[mi[x]\times mi[y]\leq n]+\sum_{d=2}^n\mu(d)\sum_{d|x}\sum_{d|y}[mi[x]\times mi[y]\leq n]
\end{aligned}
$$
前一部分比较容易求解,直接开桶维护前缀和即可;
后一部分再分类讨论:
1. $d\leq \sqrt{n}$:由于$d|x$,所以必定有$mi[x]\leq mi[d]$,所以对于任意$d|x,d|y$都有$mi[x]\times mi[y]\leq n$,所以可行方案数为$(\frac n2)^2$。
2. $d>\sqrt{n}$:由于$d|x,d|y$,若设$x=k_1d,y=k_2d$,那么有$k_1,k_2\leq \sqrt{n}$,故只有当$k_1=k_2=1$且d为质数时$mi[x]\times mi[y]>n$,可行方案数为$(\frac n2)^2-1$。
那么就只要枚举d就可以$\mathcal{O}(1)$算答案了。
使用线性筛求积性函数$\varphi(i)$和$\mu(i)$。至此本题解决。
复杂度$\mathcal{O}(n)$。
code:
#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define ll long long using namespace std; const int N=1e7+;
int n,cnt,phi[N],mu[N],p[N/],vis[N],T[N],pre[N];
ll sum1,sum2,sum3,ans,all; void sieve(int n){
phi[]=mu[]=;
rep (i,,n){
if (!vis[i]) p[++cnt]=vis[i]=i,phi[i]=i-,mu[i]=-;
for (int j=;j<=cnt&&i*p[j]<=n;j++){
vis[i*p[j]]=p[j];
if (i%p[j]==){phi[i*p[j]]=phi[i]*p[j]; break;}
phi[i*p[j]]=phi[i]*(p[j]-);
mu[i*p[j]]=-mu[i];
}
}
} int main(){
scanf("%d",&n); sieve(n);
rep (i,,n) if (vis[i]!=i||i<=n/) all++; all=all*(all-)/; //所有非0数对
rep (i,,n) sum1+=i--phi[i]; ans+=sum1;
rep (i,,n) if (vis[i]!=i||i<=n/) T[vis[i]]++;
rep (i,,n) pre[i]=pre[i-]+T[i];
rep (i,,n) if (vis[i]!=i||i<=n/) sum2+=pre[n/vis[i]]; //不考虑gcd(x,y)=1的条件
int m=sqrt(n);
rep (i,,n){ //减去gcd(x,y)>1的对数:枚举i为>1的gcd
ll tmp=;
tmp+=(ll)(n/i)*(n/i); //vis[ki]<=vis[i]故两个vis[ki]相乘必定<=n
if (i>m&&vis[i]==i) tmp--; //vis[ki]<=vis[k],而k<=m,故只有当k1=k2=1且i为质数时vis[k1i]*vis[k2i]>n
sum2+=mu[i]*tmp;
}
sum2/=; ans+=sum2*;
ans+=(all-sum1-sum2)*;
printf("%lld\n",ans);
return ;
}
CF871D Paths的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- LeetCode-62-Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
随机推荐
- Spring JDBC概述
1.jdbc 概述 Spring JDBC是Spring框架的持久层子框架.用于对数据库的操作(增删改查). 而JdbcTemplate它是spring jdbc子框架中提供的一个操作类,用于对原始J ...
- Nginx详解二十一:Nginx深度学习篇之配置苹果要求的openssl后台HTTPS服务
配置苹果要求的证书: 1.服务器所有的连接使用TLS1.2以上的版本(openssl 1.0.2) 2.HTTPS证书必须使用SHA256以上哈希算法签名 3.HTTPS证书必须使用RSA2048位或 ...
- Spring Boot的Listener机制的用法和实现原理详解
之前在介绍了在spring-boot启动过程中调用runner的原理,今天我们介绍另外一种可以实现相似功能的机制:spring-boot的Listener机制. 通过注册Listener,可以实现对于 ...
- HTML CSS JavaScript 工作笔记
1. onclick方法如何传递多个参数 "<a href='#' onclick=\"applied_status('" + ids + "', '&q ...
- 【TensorFlow】基于ssd_mobilenet模型实现目标检测
最近工作的项目使用了TensorFlow中的目标检测技术,通过训练自己的样本集得到模型来识别游戏中的物体,在这里总结下. 本文介绍在Windows系统下,使用TensorFlow的object det ...
- shell 处理文件脚本
[root@centos-6 ~]# cat info_file.txt lys:28:shanxi zhy:28:shanxi [root@centos-6 ~]# cat info_file2.t ...
- .Net分布式锁
项目中一般使用lock作为锁,以便于多线程操作确保库内数据统一.但是如果分布式部署项目,则这种锁就是去了意义,这时可以使用redis或memcache的add方法作为分布式锁. 栗子
- Description Resource Path Location Type Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix. spark-MT line 1 Maven Co
1.相信大家新建的maven项目,然后添加好依赖(即修改了pom.xml文件以后就会出现如下所示的错误): Description Resource Path Location Type Projec ...
- gradle构建时跳过指定tests
https://blog.csdn.net/u012672646/article/details/58598532
- SQL Server数据库存储过程中拼接字符串注意的问题
在SQL Server数据库中书写复杂的存储过程时,一般的做法是拼接字符串,最后使用EXEC sp_executesql '拼接的字符串' 查询出结果. 先看一段代码: -- ============ ...