link

题意:

n个点的无向图,若$\gcd(x,y) \neq1​$则$(x,y)​$有边,统计$1\sim n​$构成的无向图两两点对最短路是之和是多少(两点不连通最短路记为0)?$n\leq 10^7​$。

题解:

先分类讨论一下:

  1. 1和$>\frac n2​$的素数是孤立点,排除掉,其余是一个联通块。
  2. $\gcd(x,y)\neq1\longrightarrow dis(x,y)=1​$
  3. 记$mi[x]$为x的最小素因子,$mi[x]\times mi[y]\leq n\longrightarrow dis(x,y)=2$
  4. 其余均可通过$x \longrightarrow 2\times mi[x] \longrightarrow 2\times mi[y] \longrightarrow y$实现$dis(x,y)=3$

分别考虑:

  1. 直接排除即可
  2. 方案数$=\sum_x x-1-\varphi(x)$
  3. 方案数$=\sum_{x,y}[\gcd(x,y)=1][mi[x]\times mi[y] \leq n]$
  4. 剩余点对

考虑第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的更多相关文章

  1. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [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 ...

  4. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. 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 ...

  7. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  9. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

随机推荐

  1. springboot动态多数据源切换

    application-test.properties #datasource -- mysql multiple.datasource.master.url=jdbc:mysql://localho ...

  2. linux 搭建testlink的问题总结

    testlink问题总结 1.要求环境centos7,安装php版本5.5以上(7.1.5),mysql5.6 ,5.7测试还不行(改变挺大的5.7表结构,字段啥的待研究),apache任意版本即可 ...

  3. 中软酒店管理系统CSHIS操作手册_数据结构_数据字典

    https://wenku.baidu.com/view/f6ca11f5ee06eff9aef807cb.html

  4. Oracle亿级数据查询处理(数据库分表、分区实战)

    大数据量的查询,不仅查询速度非常慢,而且还会导致数据库经常宕机(刚接到这个项目时候,数据库经常宕机o(╯□╰)o). 那么,如何处理上亿级的数据量呢?如何从数据库经常宕机到上亿数据秒查?仅以此篇文章作 ...

  5. python---自己来打通节点,链表,栈,应用

    但,, 没有调试通过. 思路是对的,立此存照. 关键就是用链表完全实现列表的功能, 替换了就应该OK的. # coding = utf-8 # 节点初始化 class Node: def __init ...

  6. Refs & DOM

    Refs 提供了一种访问在 render 方法中创建的 DOM 节点或 React 元素的方式. 在典型的 React 数据流中, 属性(props)是父组件与子代交互的唯一方式.要修改子组件,你需要 ...

  7. 移动端根据不同DPR加载大小不同的图片

    1.首先创建mixin.styl文件代码如下: bg-image($url) // 创建bg-image($url)函数 background-image: url($url + "@2x. ...

  8. C# 之 向服务器上传资源

    首先写客服端,winform 应用 或者 WPF 应用 模拟一个post提交: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// ...

  9. (二)Makefile——自动编译、清理、安装软件

    每次都要敲击冗长的编译命令,每次都要清理之前的编译中间结果和最终结果,在安装软件时复制剪切大量的动态库,在卸载软件时删除很多的动态库和配置文件.好吧,我被逼向了makefile. helloworld ...

  10. c_数据结构_顺序表

    #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define LIST_INIT_SIZE 100 // 线性表存储空间的初始分配量 #define ...