【codeforces 870F】Paths
Description
Input
Single integer n (1 ≤ n ≤ 107).
Output
Print the sum of d(u, v) over all 1 ≤ u < v ≤ n.
Examples
6
8
10
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.
#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的更多相关文章
- 【codeforces 792D】Paths in a Complete Binary Tree
[题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- selenium js
这几天的任务量比较大,还有一个挺棘手的网站cfda,不巧的是数据量还挺大,40W关于企业信息.上来就是debugger pause,调试中断,开始还是挺懵逼的,但这个还算简单毕竟google,百度,就 ...
- vue router获取整条路径参数
$route.path 当前路由对象的路径,如'/vi$route.query 请求参数,如/foo?user=1获取到query.user = 1$route.router 所属路由器以及所属组件信 ...
- poj3614 Sunscreen
贪心题. 如何找出正确的贪心策略呢? 我一开始是以为按照l排序,然后从1到n遍历,挑最大的满足的防晒霜.后来发现不行.挑最小的也不行. 看了题解发现是从n到1遍历. 为什么? 因为i-1的l比i的l承 ...
- 【洛谷P2127】序列排序
题目大意:给定一个长度为 N 的序列,序列中的数两两不相同,每次可以交换序列中任意两个数,代价为这两个数的和,问将序列调整为升序,最少的代价是多少. 题解:考虑这个问题的一个子问题,这个序列为 N 的 ...
- python datetime 字符串 时间戳
#把datetime转成字符串 def datetime_toString(dt): return dt.strftime("%Y-%m-%d-%H") #把字符串转成dateti ...
- 最短路 次短路 k短路(k很小)
最短路 luogu 3371 https://www.luogu.org/problemnew/show/P3371 #include <cstdio> #include <cstd ...
- 【译】6. Java反射——Getter和Setter
原文地址:http://tutorials.jenkov.com/java-reflection/getters-setters.html ============================== ...
- c#反射(1)
反射可以读取程序集中代码的内容,程序集一般指(dll或exe文件). 反射中Type类,这个类太强大了,可以获取到另一个类的名称,命名空间,程序集,以及这个类中的字段,属性,方法.可以方便我们查看某个 ...
- MFC调用halcon生成的cpp内容
打开VS,文件——新建——项目——Visual C++——MFC——MFC应用程序,注意下图,其他默认.窗体1个Button.1个Picture Control [VS配置Halcon] 1.若hal ...
- Luogu P3157 [CQOI2011]动态逆序对
题目链接 \(Click\) \(Here\) 这个题有点卡常数..我的常数比较大所以是吸着氧气跑过去的... 题意:计算对于序列中每个位置\(p\),\([1,p-1]\)区间内比它大的数的个数,和 ...