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. JAVA中的Token

    JAVA中的Token 基于Token的身份验证 来源:转载 最近在做项目开始,涉及到服务器与安卓之间的接口开发,在此开发过程中发现了安卓与一般浏览器不同,安卓在每次发送请求的时候并不会带上上一次请求 ...

  2. SpringMVC 框架完成图片上传到项目路径操作

    /** * 保存添加 * * @return */ @RequestMapping(value = "taizhang/add.action", method = { Reques ...

  3. MySQL慢查询 - 开启慢查询

    一.简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二.参数说明 slow_query_log 慢查询开启状态 slow_ ...

  4. 微信公众号开发调用自带地图 不显示(openLocation)

    1.需要在wx.config中声明需要使用的功能(openLocation) 例如: wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端aler ...

  5. Python os.walk() 方法遍历文件目录

    概述 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下. os.walk() 方法是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 在Un ...

  6. Java charAt() 方法

    charAt() 方法用于返回指定索引处的字符.索引范围为从 0 到 length() - 1. 参数 index -- 字符的索引. 返回值 返回指定索引处的字符. 实例 public class ...

  7. 将txt文本转换为excel格式

    将txt文本转换为excel格式,中间使用的列分割为 tab 键 一.使用xlwt模块 注:Excel 2003 一个工作表行数限制65536,列数限制256 需要模块:xlwt 模块安装:xlwt ...

  8. JAVA 数据类型数组

    普通int: public class Array { //成员变量 private int[] data; private int size; //构造函数,传入数组的容量capacity构造Arr ...

  9. PHP中self和this的用法区别

    PHP支持类和面向对象结构,PHP的类的静态函数和变量不与任何特定类的实例相关联(换句话说,一个对象).请看:类与对象的区别. 相反,静态函数和变量与类定义本身相关联.换言之,一个类的所有实例都共享相 ...

  10. NHibernate:no persister for 异常

    几种原因: 1.配置文件后缀名写错 mapping file 必须是.hbm.xml结尾 2.Web.config配置里面引用实体 <session-factory> .......... ...