题目链接

题目描述

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  and  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 ≤ 107

输出描述:

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

case 1

input:

3

output:

2

case 2

input:

5

output:

6

题目大意:求 1~N 内满足 i / gcd(i, j) , j / gcd(i, j) 的有序对 i,j 的个数。

思路:

官方题解:

Main Idea: Math, Prime Sieve, Prefix Sum

If gcd(i_1, j_1) is not equal to gcd(i_2, j_2), (i_1, j_1) won’t be equal to (i_2, j_2).
The answer will be sum of number of diff-prime pairs whose gcd is g for each g.

Iterate each g, and find two distinct prime p_1, p_2. Then, (g p_1, g p_2) will be an answer if g p_1 <= N and g p_2 <= N. It will be reduced to find the number of prime within (N/g). It can be done by prime sieve and prefix sum.
Since p_1 not equal to p_2, it’s obvious that gcd(g p_1, g p_2)=g
Overall Time complexity: O(N) Overall Space complexity: O(N)

一开始暴力i, j,结果也很暴力直接超时,剪枝也没办法,O(N^2)肯定不用想了

其实这道题的做法是暴力最大公因数 gcd(i, j), 而不是直接暴力i, j, 因为 (i_1, j_1) 和 (i_2, j_2) 不相同说明他们的公因数也不同。所以我们只需要枚举1~N内的公因数 g 再寻找公因数相同下在区间(2, N/g)素数对组合情况就可以了,可以预处理用埃氏筛法打素数表,至于素数对组合,根据排列组合 sum[N/g]*(sum[N/g]-1), sum为前缀和,可以线性预处理出来。所以整道题的算法都是线性的。

AC code:

///2018年牛客暑假多校训练赛第三场 H
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e7+; bool is_prime[MAXN];
int num[MAXN];
int N; void sivev(int N) ///素数表
{
int p = ;
for(int i = ; i <= N; i++) is_prime[i] = true;
is_prime[] = is_prime[] = ;
for(int i = ; i <= N; i++)
{
if(is_prime[i])
{
num[i] = ;
for(int j = *i; j <= N; j+=i) is_prime[j] = ;
}
}
} int main()
{
scanf("%d", &N);
sivev(N);
for(int i = ; i <= N; i++)
num[i]+=num[i-]; long long int ans = ;
for(int g = ; g <= N; g++)
{
ans+=num[N/g]*(num[N/g]-);
} printf("%lld\n", ans); return ;
}

(第三场) H Diff-prime Pairs 【数论-素数线性筛法+YY】的更多相关文章

  1. 2019牛客暑期多校训练营(第三场) - D - Big Integer - 数论

    https://ac.nowcoder.com/acm/contest/883/D \(A(n)\) 是由n个1组成的一个整数. 第一步:把 \(A(n)\) 表示为 \(\frac{10^n-1}{ ...

  2. HDU暑假多校第三场H.Monster Hunter

    一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...

  3. 2019牛客多校训练第三场H.Magic Line(思维)

    题目传送门 大致题意: 输入测试用例个数T,输入点的个数n(n为偶数),再分别输入n个不同的点的坐标,要求输出四个整数x1,y1,x2,y2,表示有一条经过点(x1,y1),(x2,y2)的直线将该二 ...

  4. [题解]Magic Line-计算几何(2019牛客多校第三场H题)

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...

  5. 2019 牛客多校第三场 H Magic Line

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题目大意 给定 N 个不同的整数点,N 为偶数,求一条直线,这条直线能把这 N 个点对半分开,输出这条直线 ...

  6. 2019牛客多校第三场H Magic Line 思维

    Magic Line 题意 给出n(偶)个整点 整点范围1000,找出一条直线,把n个点分成均等的两部分 分析 因为都是整数,并且范围比较小,所以直接按x排序找到在中间那一部分,并且把中间那一部分的点 ...

  7. [题解] 2019牛客暑期多校第三场H题 Magic Line

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:二维平面上有n个不同的点,构造一条直线把平面分成两个点数相同的部分. 题解:对这n个点以x为第一关键 ...

  8. 2019牛客暑期多校训练营(第三场)H题目

    题意:给你一个N×N的矩阵,求最大的子矩阵 满足子矩阵中最大值和最小值之差小于等于m. 思路:这题是求满足条件的最大子矩阵,毫无疑问要遍历所有矩阵,并判断矩阵是某满足这个条件,那么我们大致只要解决两个 ...

  9. poj2689 Prime Distance(素数区间筛法)

    题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...

随机推荐

  1. mysql大数据表删除操作锁表,导致其他线程等待锁超时(Lock wait timeout exceeded; try restarting transaction;)

    背景: 1.有一个定时任务,每10分钟入一批统计数据: 2.另一个定时任务,每天定时清理7天前数据,此定时任务每天01:18:00执行: 现象: 每天01:20:00的统计数据入库失败,异常信息如下, ...

  2. Beam概念学习系列之SDKs

    不多说,直接上干货! https://beam.apache.org/get-started/beam-overview/ Beam SDK 提供了一个统一的编程模型,来处理任意规模的数据集,其中包括 ...

  3. nyoj 10——skiing————————【记忆化搜索】

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  4. 图像文字识别(OCR)用什么算法小结

    说明:主要考虑深度学习的方法,传统的方法不在考虑范围之内. 1.文字识别步骤 1.1detection:找到有文字的区域(proposal). 1.2classification:识别区域中的文字. ...

  5. 弱类型dynamic与var

    dynamic与var都可代替任何类型 var关键字是C# 3.0开始新增的特性,称为推断类型. 1.必须在定义时初始化 2.一但初始化完成就不能再给变量赋与初始化值类型不同的值 3.var要求是局部 ...

  6. Java 访问权限控制- protected 关键字

    protected 关键字的真正内涵 文章来源:http://blog.csdn.net/justloveyou_/article/details/61672133 很多介绍Java语言的书籍(包括& ...

  7. web应用开发周期

    web应用开发周期 1. 前期准备 2. 编码 3. 上线 4. 数据分析 5. 持续交付 6. 遗留系统 7. 回顾与新架构 重构的一般性因素 1. 系统难以维护 2. 系统技术栈难以符合业务需求 ...

  8. oracle学习篇二:常用SQL

    ------------------------1.简单的SQL查询--------------------------select * from emp;select empno,ename,job ...

  9. scss-&父选择器标识符

    在使用选择器嵌套的时候有一种情况需要特别注意,先看一段scss代码实例: .text a { color: blue; :hover { color: red } } 也许写此段代码目的是为了将其编译 ...

  10. JavaScript中双叹号(!!)作用

    经常看到这样的例子: var a: var b=!!a a默认是undefined.!a是true,!!a则是false,所以b的值是false,而不再是undefined,也非其它值,主要是为后续判 ...