n<=10000000的图,满足:如果(i,j)>1就连一条边权1的无相变,问所有d(u,v) (u<=v)--u到v的最短路之和。

首先1和>n/2的质数都是孤立的点。然后两个数x,y如果(x,y)>1最短路就1,如果(x,y)=1且x,y都不是1或>n/2的质数一定能走,具体这么走:$P_x$--x的最小质因子,那就$x->p_x*p_y->y$,那这样都走不了,还有:$x->p_x*2->p_y*2->y$这种一定走得了,因为x或y如果是合数那$p_x$最大是根号的,再*2根本爆不了;如果是质数那由于<=n/2,所以自己*2一定不会爆,因此就统计1的路径数,2的路径数和3的路径数即可。

1的路径数:$\sum_{1<=x<=n,1<=y<=n}(x,y)>1=\sum_{x=1}^{n}x-1-\varphi (x)$。

3的路径数用总的减掉1和2的。

2的路径数,也就是满足$(x,y)=1$且$p_x*p_y<=n$且$x>1,y>1$且x,y都不是大于n/2的质数的:

(1)x,y都是合数:那直接枚举合数,然后$\sum_{y是合数}^{n} \varphi (y)- (<=y的质数) + (x的质因子数)-1$,注意到这一条说的质数、质因子都是包括>n/2的。

(2)x质y合:那也枚举合数,$\sum_{y是合数}^{n} s_y-(y的质因子数)$,其中$s_y$表示比$x*p_y<=n$的质数x的数量,注意到这一条说的质数、质因子都是不包括>n/2的。

(3)x质y质,那枚举质数,$\sum_{y是质数}^{n} x*y<=n$,即$x<=n/y$,注意到这里枚举的质数是<=n/2的,而且这里统计的x也是<=n/2的。

OK!

 #include<string.h>
#include<stdlib.h>
#include<stdio.h>
//#include<assert.h>
#include<algorithm>
//#include<iostream>
using namespace std; int n;
#define maxn 10000011
int prime[maxn/],lp=,phi[maxn],small[maxn],sum[][maxn],sonofbitch[][maxn]; bool notprime[maxn];
void pre(int n)
{
phi[]=; sum[][]=sum[][]=;
for (int i=;i<=n;i++)
{
sum[][i]=sum[][i-]+(!notprime[i] && i*<=n);
sum[][i]=sum[][i-]+(!notprime[i]);
if (!notprime[i]) {prime[++lp]=i; phi[i]=i-; sonofbitch[][i]=(i*<=n);
sonofbitch[][i]=; small[i]=i;}
for (int j=;j<=lp && 1ll*i*prime[j]<=n;j++)
{
notprime[i*prime[j]]=; small[i*prime[j]]=prime[j];
if (i%prime[j])
{
phi[i*prime[j]]=phi[i]*(prime[j]-);
sonofbitch[][i*prime[j]]=sonofbitch[][i]+(prime[j]*<=n);
sonofbitch[][i*prime[j]]=sonofbitch[][i]+;
}
else
{
phi[i*prime[j]]=phi[i]*prime[j];
sonofbitch[][i*prime[j]]=sonofbitch[][i];
sonofbitch[][i*prime[j]]=sonofbitch[][i];
break;
}
}
}
} int main()
{
scanf("%d",&n); pre(n);
#define LL long long
LL tot1=,tot2=,tot3=,m=n-(sum[][n]-sum[][n])-,tot=m*1ll*(m-)/; for (int i=;i<=n;i++) tot1+=i--phi[i]; for (int i=;i<=n;i++)
if (notprime[i])
tot2+=phi[i]-sum[][i]+sonofbitch[][i]-+sum[][n/small[i]]-sonofbitch[][i];
else if (i*<=n) tot2+=sum[][min(i-,n/i)]; tot3=tot-tot1-tot2; printf("%lld\n",tot1+tot2*+tot3*);
return ;
}

Codeforces870F. 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. shell 调试 2例

    1.############# #!/bin/ksh if [ ! -z $TNS_ADMIN ]; then      export TNS_ADMIN=`dirname $TNS_ADMIN`   ...

  2. MS SqlServer 通过数据库日志文件找回已删除的记录

    1.建立演示数据(创建数据库数据表添加基础数据) 1.1 创建数据库 1.2 创建数据表 1.3填充数据 1.4做数据库完整备份 2.模拟误删除.记录操作时间.备份数据库日志 2.1删除数据并记录操作 ...

  3. 构建微服务开发环境2————安装IntelliJ IDEA

    [内容指引] 下载IntelliJ IDEA安装包: Mac版IDEA安装: Windows版IDEA按装. 一.下载IntelliJ IDEA安装包 访问官方网址:https://www.jetbr ...

  4. 自学 iOS - 三十天三十个 Swift 项目 第三天

    做了这个小demo 之后  感觉OC 和swift 还是有很大的差别的 自己还是要去多看些swift的语法 用的不是很熟练 1.这个demo 的资源文件 我都是用原工程的 2.同样的自定义cell 的 ...

  5. javascript中闭包与作用域的理解

    很多js的框架与插件编写都用到了闭包,所以,阅读和掌握闭包很有必要.最近学习vue框架时,经常会猜想很多功能的native js实现,很多都应用到了闭包,闭包除了目前已知的一些特性,如:可以保持局部变 ...

  6. Android开发使用的常见第三方框架汇总

    本文转载:http://blog.csdn.net/liuhaomatou/article/details/44857005 1.volley 项目地址 https://github.com/sman ...

  7. 解决VS2010提示warning C4068: 未知的杂注

    出现原因是#pragma声明问题,加上#pragma warning(disable:4068)即可 #pragma warning(disable:4068)#pragma execution_ch ...

  8. html5开发移动混合App系列2-开发环境搭建(windows)

    Java下载: http://www.oracle.com/technetwork/java/javase/downloads/index.html 环境变量: JAVA_HOME=C:\Progra ...

  9. 数据库系统概论(1)——Chap. 1 Introduction

    数据库系统概论--Introduction 一.数据库的4个基本概念 数据(data):数据是数据库中存储的基本单位.我们把描述事物的符号记录称为数据.数据和关于数据的解释是不可分的,数据的含义称为数 ...

  10. at, batch, atq, atrm - 排队、检查或删除以后要执行的作业

    总览 at [-V] [-q 队列] [-f 文件] [-mldbv] 时间 at -c 作业 [作业...] atq [-V] [-q 队列] [-v] atrm [-V] 作业 [作业...] b ...