(第三场) H Diff-prime Pairs 【数论-素数线性筛法+YY】

The Skyline Problem解题心得 The Skyline Problem解题心得 原图链接:https://leetcode.com/problems/the-skyline-problem/description/ 题目复述 如下图所示,已知多个矩形建筑物的左右边界位置以及高度(建筑物都是贴地的)[Li, Ri, Hi] ;由此可见多个矩形建筑物在二维的平面上存在重叠的可能,在这个题目里我们需要求得重叠后的建 阅读详情

题目链接

题目描述 

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+10;

bool is_prime[MAXN];
int num[MAXN];
int N;

void sivev(int N)      ///素数表
{
    int p = 0;
    for(int i = 0; i <= N; i++) is_prime[i] = true;
    is_prime[0] = is_prime[1] = 0;
    for(int i = 2; i <= N; i++)
    {
        if(is_prime[i])
        {
            num[i] = 1;
            for(int j = 2*i; j <= N; j+=i) is_prime[j] = 0;
        }
    }
}

int main()
{
    scanf("%d", &N);
    sivev(N);
    for(int i = 1; i <= N; i++)
        num[i]+=num[i-1];

    long long int ans = 0;
    for(int g = 1; g <= N; g++)
    {
        ans+=num[N/g]*(num[N/g]-1);
    }

    printf("%lld\n", ans);

    return 0;
}

 

全国排污许可证数据 from tkinter import * # 导入窗口控件 import tkinter.filedialog import requests from lxml import etree from tkinter import ttk from bs4 import BeautifulSoup import webbrowser # 调用浏览器打开网页 from tkinter import messagebox # 弹出提示框 from openpyxl import Workbook imp. 阅读详情

相关推荐

UE4项目迁移指南:如何无缝导入外部.uasset文件并保持材质关联

本文详细解析了将外部.uasset文件无缝导入UE4项目的专业方法。针对手动复制文件时常见的材质关联丢失问题,提供了使用引用查看器进行诊断和修复的实用技巧,并推荐使用引擎内置的“迁移”功能确保资源依赖关系的完整性,帮助开发者高效整合项目资源。

t8u9v0的博客 1047

H、Diff-prime Pairs----简单数论欧拉筛+前缀和+划分思维

题目链接 主要思想:先把素数筛出来,然后前缀和统计前i个数里有多少素数,然后每次除以最大公约数。 素数对中的素数是不能相同的,其次我们枚举每一个公约数,因为素数对的倍数也是符合题目要求,res += sum[n / g] * (sum[n / g] - 1) #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using

ZZU_LGY 721

AI辅助专利撰写实战:用Codex 30分钟生成发明专利初稿

这次我们来看一个用 Codex 从零开始撰写发明专利的实战项目。对于技术研发人员、产品经理或初创团队来说,专利申请是保护创新成果、构建技术壁垒的关键一步。然而,撰写一份高质量的专利申请书,尤其是权利要求书和说明书,往往需要专业的法律和技术语言,耗时耗力。这个项目演示了如何利用 AI 工具 Codex,在短时间内生成一份结构完整、逻辑清晰、具备授权潜力的专利申请文件初稿。 整个过程的核心不是让 AI 完全替代专利代理人,而是将其作为强大的“技术翻译”和“内容生成”助手。它能将你的技术构思、流程图、核心算法或产

weixin_32181267的博客 421

3-H Diff-prime Pairs

链接:https://www.nowcoder.com/acm/contest/141/H 来源:牛客网   题目描述 Eddy has solved lots of problem involving calculating the number of coprime pairs within some range. This problem can be solved with inclu...

d1183的博客 269

别再花钱买商业软件了!这4款免费DIC工具,从散斑生成到应变分析全搞定

本文介绍了4款免费开源的DIC(数字图像相关技术)工具,涵盖从散斑生成到应变分析的全流程,帮助用户零成本构建专业级DIC工作流。这些工具在精度、算法透明度和生态整合方面具有优势,适用于材料力学和实验力学研究,可替代昂贵的商业软件。

weixin_42525369的博客 171

Netty UDP

UDP是一种无连接的协议,它并不在通信双方之间建立连接,而是直接将数据包从发送端发送到接收端。UDP不保证数据包的可靠传输,也不保证数据包的顺序,因此其传输速度通常比TCP快,且系统开销较小。然而,这也意味着UDP在某些情况下可能会丢失数据包或数据包到达的顺序错乱。

王小工小工历程 2028

牛客暑期第三场-H(diff-prime pairs)

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. ...

love_wjk的博客 294

金融行业大数据可视化:实时监控与风险预警系统构建

随着金融业务数字化转型,日均产生的交易数据、市场数据、客户行为数据呈指数级增长(单家银行日交易数据量可达TB级)。数据实时性不足(分钟级延迟难以应对高频交易风险)风险特征挖掘浅层(仅基于规则引擎,漏报率高达30%+)可视化分析能力薄弱(静态报表无法支持动态决策)本文聚焦构建具备毫秒级数据响应智能风险建模交互式可视化决策能力的新一代系统,覆盖数据采集、实时处理、多维可视化、智能预警四大核心模块,适用于银行、证券、保险等金融细分领域。章节核心内容技术亮点核心概念可视化系统架构/数据流模型。

AI天才研究院 1523

牛客网暑期ACM多校训练营(第三场) H Diff-prime Pairs(欧拉筛法)

求pair(i,j) : 满足 i/gcd(i,j) 和 j/gcd(i,j) 都是素数的 个数 在 n 内解析:求1-n以内的所有素数,那么对于任意一对素数,x, y(x < y),他们都共能生成2*n/y个符合条件的数对。(例如n = 10, x = 2, y = 3, 则共有(2,3), (4,6), (6,9)这三对, 每一对数交换位置又能构成新的一对(3,2),(6,4),(...

weixin_30861459的博客 155

【WorkBuddy专栏27】WorkBuddy 和 CodeBuddy 到底什么关系——一篇文章终结所有混淆

WorkBuddy 和 CodeBuddy 到底什么关系——一篇文章终结所有混淆

AI人人懂 2774

基于STM32+华为云IOT设计的云平台监控系统_102

本次设计结合现代生活的具体需求,设计出一套基于华为云IOT设计的云平台监控系统,可以远程实时查看家里光照强度、温湿度等信息,支持在云端控制家里的电器设备,实现远程控制。

7645

牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs

链接:https://www.nowcoder.com/acm/contest/141/H 来源:牛客网 题目描述 Eddy has solved lots of problem involving calculating the number of coprime pairs within some range. This problem can be solved with inclusi...

LOVE&AI的博客 382

MATLAB基础应用精讲-【图像处理】GNN图神经网络(附python代码实现)

图神经网络由于其在处理非欧空间数据和复杂特征方面的优势,受到广泛关注并应用于推荐系统、知识图谱、交通道路分析等场景。图结构可描述复杂关系,如基因结构、通信网络、交通路线、社交网络等。图计算挖掘结构信息,但无法学习节点特征。神经网络在欧氏空间数据上表现优越,但无法直接应用于非欧氏空间的图数据。图神经网络结合图计算和神经网络优势,处理非欧氏空间数据及其复杂特征,应用于网络链接预测、推荐系统和交通道路分析等场景。实际应用中,图数据规模庞大,如百亿级节点、千亿级边,存储开销超过10TB。

qq_36130719的博客 1142

Diff-prime Pairs(思维+素数筛)

题目链接:https://ac.nowcoder.com/acm/contest/7865/J 题目描述 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. Som

qq_41685509的博客 1044

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

H、Diff-prime Pairs

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 proble

水蛙菌 7523

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

题解 | Removing Stones-2019牛客暑期多校训练营第三场G题

题目来源于牛客竞赛:https://ac.nowcoder.com/acm/contest/discuss 题目描述: 输入描述: 输出描述: 示例1: 题解: 代码: #include<cstdio> #include<cstring> #include<algorithm> #include<cassert> using namespa...

lastbye的博客 364

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

牛客网暑期ACM多校训练营(第三场)H题 Diff-prime Pairs

2018牛客网暑期ACM多校训练营第三场H题 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 imple...

qq_42515626的博客 298

牛客暑期训练营 第三场 H 题

来源:牛客网   题目描述 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...

暴走的二哈 351

题解 | digits 2-2019牛客暑期多校训练营第五场A题

题目来源于牛客竞赛:https://ac.nowcoder.com/acm/contest/discuss 题目描述: 输入描述: 输出描述: 示例1: 题解: 代码: /*{{{*/ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include&...

lastbye的博客 220

python中distinct,使用Python查找给定数组中Distinct Prime的总数

The problem is like this -You have given an array A having N integers. Let say G is the product of all elements of A. You have to find the number of distinct prime divisors of G.Example -Input :A = 1,...

weixin_31201481的博客 814

FZU——数字游戏

Problem Description 给一个N位的正整数,该数不包含前导0,先让你调整其中每个数字的位置,得到另一个n位的数,并且使得这个数越小越好,而且这个数不能包含前导0。比如543210可以变成102345,而12345保持不变才是最优结果。  Input 第一行一个整数T(T 每组数据先输入一行一个整数N(1  Output 每组数据对应一行输出,即调整数

丙三醇 1529

牛客网暑期ACM多校训练营(第三场) H Diff-prime Pairs

题目链接   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...

包子不卖包子 400

牛客 Diff-prime Pairs (数论+埃氏筛+前缀和)

题目描述 输入描述: 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 示例1 输入 3 输出 2 示例2 输入 5 输出 6 首先附上官方题解: Main Idea: Math, P

少年白马 468

牛客网暑期ACM多校训练营(第三场)H.Diff-prime Pairs 思维

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 proble

xiuya19的博客 298

牛客多校赛第三场-h题

8. 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 time...

s_h_w_s_n_g的博客 328

1007. 素数对猜想 (20)(java实现)

最开始提交的时候最后一组数据一直运行超时,相同代码换成c++也是,百思不得其解。。最后上课的时候想到判断是否是素数的时候,自己之前循环的num/2,但其实到100000就要循环5000次,之前刚开始写的时候没有考虑数比较大的情况,但其实循环到num的平方根,num比较大循环的次数就少很多了。为什么可以循环到数的平凡根就可以了->https://zhidao.baidu.com/question/7

ccccccccccs的博客 1341
上一篇: POJ 2299 【树状数组 离散化】
下一篇: POJ 1745 【0/1 背包】
莜莫oO
博客等级 码龄9年 7粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值