Description

You are given a positive integer n. Let's build a graph on vertices 1, 2, ..., n in such a way that there is an edge between vertices u and v if and only if gcd(u,v)≠1. Let d(u, v) be the shortest distance between u and v, or 0 if there is no path between them. Compute the sum of values d(u, v) over all 1 ≤ u < v ≤ n.The gcd (greatest common divisor) of two positive integers is the maximum positive integer that divides both of the integers.

Input

Single integer n (1 ≤ n ≤ 107).

Output

Print the sum of d(u, v) over all 1 ≤ u < v ≤ n.

Examples

input
6
output
8
input
10
output
44

Note

All shortest paths in the first example:

There are no paths between other pairs of vertices.The total distance is 2 + 1 + 1 + 2 + 1 + 1 = 8.

题意:
给定数字n,建立一个无向图。对于所有1~n之间的数字,当数字gcd(u,v)≠1时将u、v连一条边,边权为1。d(u, v)表示u到v的最短路,求所有d(u, v)的和,其中1 ≤ u < v ≤ n。
 
分析:
对于1以及所有大于n/2的的质数,与其他数字均不联通,直接剔除。
对于剩下的数字:
1.当gcd(u,v)≠1时,d(u, v)==1。即对于数字u,小于u且d(u, v)==1的数字个数为x - 1 - φ(x)。
2.令p[u]表示数字u的最小质因子,则当p[u]·p[v] ≤ n时,d(u, v)==2。维护数组num、sum,num[i]代表最小质因子为i的数字个数,sum数组为num数组的前缀和。统计Σnum[i]·sum[n/i]可以覆盖所有p[u]·p[v] ≤ n的情况,其中减去自身与自身被统计的情况,剩下的所有数对都被统计了两次,其中包含gcd(u,v)≠1的情况,需进行相应处理,详见代码。
3.剩下的数对最短路一定为3,因为 u→2·p[u]→2·p[v]→v这条路一定存在。可通过数对总数减去d(u, v)==1与d(u, v)==2的情况得到。
 
 #include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int N=1e7+;
int n,m,tot,now,pri[N],p[N],phi[N],num[N],sum[N];
LL one,two,three;
int main()
{
scanf("%d",&n);
phi[]=;
for(int i=;i<=n;i++)
{
if(!p[i]){p[i]=pri[++tot]=i;phi[i]=i-;}
for(int j=;j<=tot;j++)
{
if(i*pri[j]>n)break;
p[i*pri[j]]=pri[j];
if(i%pri[j]==){phi[i*pri[j]]=phi[i]*pri[j];break;}
else phi[i*pri[j]]=phi[i]*(pri[j]-);
}
}
for(int i=;i<=n;i++)one+=i--phi[i];
for(int i=;i<=n;i++)num[p[i]]++;
for(int i=;i<=n;i++)sum[i]=sum[i-]+num[i];
for(int i=;i<=n;i++)two+=1ll*num[i]*sum[n/i];
for(int i=;i<=n;i++)if(1ll*p[i]*p[i]<=n)two--;
two=two/-one;m=n-;
for(int i=tot;i>=;i--)
if(pri[i]*>n)m--;
else break;
three=1ll*m*(m-)/-one-two;
printf("%I64d\n",one+two*+three*);
return ;
}

【codeforces 870F】Paths的更多相关文章

  1. 【codeforces 792D】Paths in a Complete Binary Tree

    [题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【25.64%】【codeforces 570E】Pig and Palindromes

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  5. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  6. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  7. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  8. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  9. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

随机推荐

  1. 关于一些没做出来的SBCF题

    这里是一些我SB没做出来的CF水题. 其实这些题思维量还不错,所以写在这里常来看看…… 不一定每题代码都会写. CF1143C Queen 其实只要注意到如果一个点开始能被删,那一直就能被删:一个点开 ...

  2. 对于Spring中注入对象失败的问题

    1.今天进行了一段代码,经过检查发现config配置文件没问题,注入注解也编写正确,但是RedisTemplate就是会抛出空指针异常. 进过一系列检查发现,其在用户使用该类的时候采用的是new方式创 ...

  3. 【洛谷P1059 明明的随机数】

    题目描述明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着 ...

  4. python zip()函数的使用

    解释: 后缀为zip的文件肯定都见过吧?zip是打包压缩好的一个文件,所以,zip()函数也简单的理解为打包压缩函数,将不同个数相同类型的字段结合在一起. 官方定义为:zip() 函数用于将可迭代的对 ...

  5. postman 介绍

  6. 生产环境Linux常用命令【随时更新】

    1. 查询文件中的关键字并高亮显示[查询当前目录关键字为elasticsearch的日志文件] find ./ -name "my-elasticsearch.log" | xar ...

  7. Dubbo新版管控台

    地址:https://github.com/apache/incubator-dubbo-ops 下载下来,解压 打开cmd 注意:它的前端用到了Vue.js,打包需要npm,所以你要有node.js ...

  8. PHP生成四角图片

    <?php /** 圆角 $radius = 100; $img = imagecreatetruecolor($radius, $radius); // 创建一个正方形的图像 $bgcolor ...

  9. python基础四-文件读取

    文件读取 open()接受一个参数:要打开的文件名, 并返回一个表示文件的对象, 存储到后面的变量中 python会在当前执行文件所在目录查找 可以使用绝对路径, 在linux中使用'/', 在win ...

  10. ssm框架中从controller传值给jsp的方式

    第一种方式是 通过session 第二种如下: 如何将controller层值传递到JSP页面 @RequestMapping(value="/result",method=Req ...