题目链接

题目

题目描述

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. 洛谷 P9916 「RiOI-03」Just a Q. (Easy ver.) 题解

    前言 Div.2 Rank \(13\) 获奖了,题目也好评. 解法 题目链接:\(\color{Purple}\texttt{P9916「RiOI-03」Just a Q. (Easy ver.)} ...

  2. Dubbo入门1:Spirngboot+Dubbo2.6.0整合

    整合springboot+dubbo2.6.0 demo 本文简要说明了springboot和dubbo整合的配置文件的写法 目录结构 整体目录 如下图所示:整体项目是一个父子工程,common作为一 ...

  3. SNMP 使用总结

    转载请注明出处: 1.SNMP简介 SNMP(Simple Network Management Protocol,简单网络管理协议)是一种用于网络设备和系统的管理协议.它允许网络管理员监控和管理网络 ...

  4. Redis scan等命令的学习与研究

    Redis scan等命令的学习与研究 摘要 背景跟前几天说的一个问题类似. 为了验证自己的设想, 所以晚上继续写脚本进行了一轮次的验证. 不过上次讨论时,打击好像都没听懂我说的 所以这次准备从基础开 ...

  5. [转帖]find排除一个或多个目录的方法

    find排除一个或多个目录的方法 百度就是垃圾,搜索结果千篇一律,错抄错.google一下,总结find排除某个目录的方法: How to exclude a directory in find . ...

  6. redis 6源码解析之 sds

    redis使用sds(simple dynamic string)实现了字符串的存储.sds实际上就是TLV格式的数据结构.其数据结构主要分为如下5种,主要分为首部和数据部分,首部给出了type和le ...

  7. (数据科学学习手札122)Python+Dash快速web应用开发——内网穿透篇

    由我开源的先进Dash组件库feffery-antd-components正处于早期测试版本阶段,欢迎前往官网http://fac.feffery.tech/了解更多 1 简介 这是我的系列教程Pyt ...

  8. Fabric网络升级(总)

    原文地址在这里. 在fabric网络中,升级nodes和通道至最新版本需要四步: 备份账本和MSPs. 以滚动的方式将orderer升级到最新版. 以滚动的方式将peers升级到最新版. 将order ...

  9. IServiceBehavior, IOperationBehavior,IParameterInspector

    1 public class MyOperationBehavior:Attribute, IOperationBehavior 2 { 3 public void AddBindingParamet ...

  10. 深度学习应用篇-推荐系统[11]:推荐系统的组成、场景转化指标(pv点击率,uv点击率,曝光点击率)、用户数据指标等评价指标详解

    深度学习应用篇-推荐系统[11]:推荐系统的组成.场景转化指标(pv点击率,uv点击率,曝光点击率).用户数据指标等评价指标详解 1. 推荐系统介绍 在网络技术不断发展和电子商务规模不断扩大的背景下, ...