题目链接

题目

题目描述

Eddy has solved lots of problem involving calculating the number of coprime pairs within some range. This problem can be solved with inclusion-exclusion method. Eddy has implemented it lots of times. Someday, when he encounters another coprime pairs problem, he comes up with diff-prime pairs problem. diff-prime pairs problem is that given N, you need to find the number of pairs (i, j), where \(\frac{i}{gcd(i, j)}\) and \(\frac{j}{gcd(i,j)}\) are both prime and i ,j ≤ N. gcd(i, j) is the greatest common divisor of i and j. Prime is an integer greater than 1 and has only 2 positive divisors.

Eddy tried to solve it with inclusion-exclusion method but failed. Please help Eddy to solve this problem.

Note that pair (i1, j1) and pair (i2, j2) are considered different if i1 ≠ i2 or j1 ≠ j2.

输入描述

Input has only one line containing a positive integer N.

\(1 ≤ N ≤ 10^7\)

输出描述

Output one line containing a non-negative integer indicating the number of diff-prime pairs (i,j) where i, j ≤ N

示例1

输入

3

输出

2

示例2

输入

5

输出

6

题解

知识点:组合数学,枚举,筛法。

考虑枚举每一对素数 \((i,j)\) 产生的贡献,其中显然 \(i\neq j\) 。

不妨先假设 \(i<j\) ,那么形如 \((ki,kj)\) 的一对数结果会是 \((i,j)\) ,其中 \(k \leq \left\lfloor \dfrac{n}{j} \right\rfloor\) 。

我们发现,对于任意的素数 \(i\) 满足 \(i<j\) ,答案都是 \(\left\lfloor \dfrac{n}{j} \right\rfloor\) ,因此我们可以只枚举 \(j\) ,那么以 \(j\) 较大数时产生的贡献,答案是 \((\pi(j)-1) \cdot \left\lfloor \dfrac{n}{j} \right\rfloor\) 。

最后不要忘记乘 \(2\) ,因为我们一开始设 \(i<j\) ,为了计数方便。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; const int N = 1e7 + 7;
bool vis[N];
vector<int> prime;
void get_prime(int n) {
for (int i = 2;i <= n;i++) {
if (!vis[i]) prime.push_back(i);
for (auto j : prime) {
if (i * j > n) break;
vis[i * j] = 1;
if (!(i % j)) break;
}
}
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
get_prime(n);
ll ans = 0;
for (int i = 0;i < prime.size();i++) {
ans += i * (n / prime[i]);
}
cout << ans * 2 << '\n';
return 0;
}

NC17247 H、Diff-prime Pairs的更多相关文章

  1. 深入理解react中的虚拟DOM、diff算法

    文章结构: React中的虚拟DOM是什么? 虚拟DOM的简单实现(diff算法) 虚拟DOM的内部工作原理 React中的虚拟DOM与Vue中的虚拟DOM比较 React中的虚拟DOM是什么?   ...

  2. 【Linux命令】文本文件编辑命令10个(cat、more、less、head、tail、tr、wc、stat、cut、diff)

    目录 cat查看文档 more可分页查看文档 less相比较more功能更强大 head查看文档的前N行 tail查看文档的后N行或试试刷新查看 tr替换文本字符 wc统计文本行数 stat查看文档存 ...

  3. dos2unix、diff命令

    一.dos2unix:将DOS格式文件转化成UNIX格式文件 语法: dos2unix [选项] [文件...] [-n INFILE输出文件...]           unix2dos [选项] ...

  4. Linux基础篇,文本数据的比较与排序:sort、uniq、comm、diff

    一.sort sort命令用于将文本文件内容以行排序 sort [选项参数] [-o<输出文件>] [-t<分隔字符>] [+<起始栏位> -<结束栏位> ...

  5. 如何在项目中引入 #include .h、.lib、 .dll、.cpp (转)

    源:http://blog.csdn.net/vippolka/article/details/8552735 在项目中引入.h..lib和dll.以及.cpp 1..h的引入 解决办法1:把  XX ...

  6. 【Git】(2)---checkout、branch、log、diff、.gitignore

    常用命令 一.命令 1.checkout 切换分支 git checkout 分支名 #切换分支 #如果在当前分支上对文件进行修改之后,没有commit就切换到另外一个分支b, 这个时候会报错,因为没 ...

  7. stdafx.h、stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h? Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编

    sstdafx.h.stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h? Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编译程序也要 ...

  8. stdafx.h是什么用处, stdafx.h、stdafx.cpp的作用

    http://blog.csdn.net/songkexin/article/details/1750396 stdafx.h头文件的作用 Standard Application Fram Exte ...

  9. stdafx.h、stdafx.cpp的作用

    这两个文件用于建立一个预编译的头文件".PCH"和一个预定义的类型文件"STDAFX.OBJ".由于MFC体系结构非常大,各个源文件中都包含许多头文件,如果每次 ...

  10. sed初理多行合并+sed之G、H、g、h使用+sed n/N使用说明

    转载:[shell]sed处理多行合并 - seyjs - 博客园 (cnblogs.com) 文件格式 table=t1 name owner address table=t2 id text co ...

随机推荐

  1. 域名解析类型及dig,nslookup进行Dns解析过程查看

    本文为博主原创,未经允许不得转载: 通常我们在windows系统下查看域名是不是可以正常访问,是通过cmd命令打开dos窗口,使用ping 命令来查看域名是不是可以正常访问,使用 ping 命令正常访 ...

  2. 如何取消VSCODE文件夹折叠

    1.问题 如图所示,文件夹折叠在一起,导致我无法在父文件夹中新建一个文件夹,而是只能在子文件夹中新建文件夹 2.解决 原因:文件夹以紧凑方式呈现,取消即可 1. 打开设置,在里面搜索Explorer: ...

  3. 【TouchGFX】visua studio 自定义路径宏

    很好奇 touchgfx 的 visual studio 工程文件中路径符号 $(TouchGFXReleasePath)是哪里定义的,经查这就是一个宏替换 自定义宏方式  

  4. mongo-连接失败

    连接mongo失败 默认情况下,mongo最大支持65535个连接 查询当前支持的连接数 db.serverStatus.connections { "current" : 3,/ ...

  5. [转帖]MySQL多版本并发控制机制(MVCC)-源码浅析

    https://zhuanlan.zhihu.com/p/144682180 MySQL多版本并发控制机制(MVCC)-源码浅析 前言 作为一个数据库爱好者,自己动手写过简单的SQL解析器以及存储引擎 ...

  6. [转帖]GRUB2 配置文件详解

    https://www.cnblogs.com/fluidog/p/15176726.html 1. GRUB2配置文件 GRUB2 的配置文件通常为 /boot/grub2/grub.cfg,虽然此 ...

  7. [转帖]绕过CDN查看网站真实IP

    https://www.itblogcn.com/article/viewcdnip.html   这是一个总结帖,查了一下关于这个问题的国内外大大小小的网站,对其中说的一些方法总结归纳形成. 首先, ...

  8. [粘贴]github-redis-rdb-cli

    redis-rdb-cli A tool that can parse, filter, split, merge rdb and analyze memory usage offline. It c ...

  9. [转帖]Jmeter插件之ServerAgent服务器性能监控工具的安装和使用

    https://www.cnblogs.com/pachongshangdexuebi/p/13354201.html 一.前言    性能测试时我们关注的重要指标是:并发用户数,TPS,请求成功率, ...

  10. [转帖]备份VCSA内置Postgresql数据库

    首先命令行远程登录到VCSA服务器,然后执行如下命令停掉VCSA的核心服务vmware-vpxd: vCenterServerAppliance:~ # service vmware-vpxd sto ...