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. 波哥博客Url

    http://www.cnblogs.com/whatlonelytear/

  2. poj3254 炮兵阵地弱化版,记数类dp

    /* dp[i][j]表示到第i行的状态j有多少放置方式 */ #include<iostream> #include<cstring> #include<cstdio& ...

  3. Python中pass的用法、作用

    pass主要作用就是占位,让代码整体完整.如果定义一个函数里面为空或一个判断写好了之后还没想好满足条件需要执行执行什么逻辑,又想留着后面使用,但是函数和判断里面为空,那么就会报错,当你还没想清楚函数内 ...

  4. [转] linux学习第四十四篇:Nginx安装,Nginx默认虚拟主机,Nginx域名重定向

    Nginx安装 进入存放源码包的目录: cd /usr/local/src 下载源码包: wget http://nginx.org/download/nginx-1.12.1.tar.gz 解压: ...

  5. 【BZOJ4764】弹飞大爷

    题解: 这个应该还是比较简单的 首先比较容易想到用lct来维护 我们可以建立一个特殊点 然后我们要处理环 其实只要判断它和不和这个特殊点联通就行了 那么当它不是环了我们怎么还原呢 只要对每个在根节点记 ...

  6. nginx 日志 cron任务切割日志

    #vim cut_nginx_log.sh #cd /usr/local/nginx/logs/ #/bin/mv access.log access_$(date +%F).log #/usr/lo ...

  7. nginx 设置自签名证书以及设置网址http强制转https访问

    自签名证书可以在自己的内网环境或者非对外环境使用,保证通信安装 1.生产证书 直接使用脚本生产: 中途会提示书如1次域名和4次密码,把一下文件保存为sh文件,赋予x权限后 直接执行,根据提示输入. # ...

  8. 正则表达式匹配URL或者网址

    正则表达式 (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])? ...

  9. JMeter中BeanShell的实际应用

    使用Jmeter的BeanShell断言,把响应数据中的JSON跟数据库中的记录对比 很多时候我们需要把Response Data取到的 Json 字符串跟数据库里的对比,来验证接口的正确性,使用Be ...

  10. JMeter选择协议踩过的坑