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. Annihilate(SA)

    题目描述 黑暗之主的蜈蚣几乎可以毁灭一切,因此小正方形陷入了苦战…… 小正方形现在需要减弱黑暗之主的攻击. 一个黑暗之主的攻击可以用一个仅有小写字母的字符串表示. 现在黑暗之主向小正方形发动了若干攻击 ...

  2. linux环境sed命令实例学习

    命令简介: sed(Stream Editor)被称作流编辑器.linux命令环境的“三剑客”(grep,sed,awk)之一,功能强大,可以根据命令来处理数据流中的数据,命令可以在命令行中,也可以出 ...

  3. jsp (1)

    一.jsp介绍: jsp是servlet的一种包装.是html+js+css+servlet. jsp文件无需配置,如果修改了jsp文件不需要reload应用. jsp访问方法:直接访问文件名.jsp ...

  4. Opennebula常用命令

    查看虚拟机状态信息: [oneadmin@localhost /]$ onevm list 查看虚拟机配置: [oneadmin@localhost /]$ onevm show 25 启动虚拟机: ...

  5. Python三大web框架简单介绍

    Django 是重量级框架:它封装的的功能常丰富非常多所以它是重量级,Django的文档最完善.市场占有率最高.招聘职位最多.Django提供全套的解决方案(full-stack framework ...

  6. (转)你应该知道的RPC原理

    背景:对于项目中的RPC框架,仅仅停留在使用层面,对于其底层的实现原理不是很清楚.这样的后果是很危险的,对于面试官来说,跟不知道这个东西一样. 转载自:https://www.cnblogs.com/ ...

  7. vue.js自定义组件directives

    自定义指令:以v开头,如:v-mybind. <input v-mybind /> directives:{ mybind:{ bind:function (el) { el.value ...

  8. 记录EXCEL格式和TXT文本格式之间的互转

    EXCEL格式转变成TXT文本格式 1.打开execl文档,点击文件另存为 2.选择txt保存 3.重命名文档,打开该txt文档 4.按Ctrl+H,将文档中空格转换成其他分割符,单击确定 TXT格式 ...

  9. JS事件(一)事件流&事件处理程序

    1.事件流描述的是从页面接收事件的顺序 IE和Netscape提出了几乎完全相反的事件流概念 IE:事件冒泡(由内而外) Netscape:事件捕获(由外向内) DOM2级事件规定事件流包括三个阶段: ...

  10. java的零拷贝机制

    转:https://blog.csdn.net/zhouhao88410234/article/details/77574689?fps=1&locationNum=9 为何要懂零拷贝原理?因 ...