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 ...
随机推荐
- java----java工具类
System: Runtime: Random: Scanner: Arrays: MessageFormat: Math: 日期: Comparable: cloneable接口: 数字处理: MD ...
- Centos系统压力测试 ab 命令安装与使用
Apache安装包中自带的压力测试工具 Apache Benchmark(简称ab) 简单易用,这里就采用 ab作为压力测试工具了. 1.独立安装 ab运行需要依赖apr-util包,安装命令为: y ...
- Python中的函数介绍
调用函数 python中有很多内置函数,我们可以直接调用,内置函数能直接在官网查看:https://docs.python.org/3/library/functions.html#abs 定义函数 ...
- 区分TCP包的顺序
确认TCP包的顺序: 使用抓包工具抓包之后,通常按照时间先后排序的,而不是数据的内容逻辑先后.查找内容的先后的关键在于查看TCP中的Sequence number和Acknowledgment num ...
- Idea和PyCharm激活破解
1. 先去百度去官网下载专业版IDE, Idea 和PyCharm激活方法一样 2. 下载破解包, 点击下载 3. 将下载的jar包放到这个安装目录的bin目录下面 4. 在bin目录下面的文件pyc ...
- python目录和文件的列表清单
import os # 通过给定的文件夹,列举出这个文件夹当中,所有的文件,以及文件夹,子文件夹当中的所有文件def listFilesToTxt(dir,file): # 1.列举出,当前给定的文件 ...
- tensorflow实现RNN及Word2Vec
参考:<tensorflow实战> 首先介绍一下Word2Vec Word2Vec:从原始语料中学习字词空间向量的预测模型.主要分为CBOW(Continue Bags of Words) ...
- 开始写博客,学习Linq(1)
摘自<linq实战>原文: 软件很简单.它可以归结为两件事情:代码和数据. 开发软件却并非那么简单,其中很重要的一项任务就是编写处理数据的代码. 无论选择了哪种语言,在程序开发得某个时候你 ...
- Java-把日期字符串转换成另一种格式的日期字符串
package com.example.demo.utils; import java.text.ParseException; import java.text.SimpleDateFormat; ...
- 第四次作业之oop
第四次作业 四则运算 类 输入类:用户输入题数和答案,语言选择. 生成类:随机数字,运算符,生成表达式. 读取类:读取表达式,计算正确答案. 界面类:选择语言,输出正确题数和答案. 类与类之间是如何进 ...