当前位置:网站首页>数论整除分块讲解 例题:2021陕西省赛C
数论整除分块讲解 例题:2021陕西省赛C
2022-07-24 17:11:00 【Morgannr】
引入
一般一个算法的引入都是为了解决一类问题,那么这个问题是什么?
求解:
式子很简单,可以直接
求,但是如果
为
甚至
呢,这个时候就要引入 整除分块的概念。
这个算法不能算是一种高深算法,不要掌握什么其他知识就能学,类似于贪心算法。
首先我们可以先写一个 暴力算法(任何算法几乎都是暴力引申的)
n = 20;
for (int i = 1; i <= n; i++) {
printf("%d ", n / i);
}
得出的 答案 是:20 10 6 5 4 3 2 2 2 2 1 1 1 1 1 1 1 1 1 1,你发现了 相同数是连续的一段
那么其实你就可以把这些数划分成若干段,每一段中的
的值都相同
而
的个数是
级别的,那么 时间复杂度就可以优化到 
如何确定每一段的左右边界呢,其实很简单,假设左边界是
,那么右边界显然就是 
而下一个左边界是 上一个右边界加
,最开始的左边界是
这是 数论分块 在 OI wiki 上的讲解:数论整除分块
根据 OI wiki 上的讲解,我们可以知道,数论分块 可以快速计算一些含有 除法向下取整的和式,形如:
(这点很重要,是可以扩展的,只要形如此式就可以用整除分块求)
当可以在
时间内计算
或 已经预处理出
的前缀和 时,数论分块 就可以在
的时间内计算上述和式的值。
核心:将
相同的数打包同时计算。
代码:
for (int l = 1, r; l <= n; l = r + 1) {
r = min(n, n / (n / l));//注意min
ans += (r - l + 1) * (n / l);
}
例题:陕西省第九届ACM竞赛 C--GCD(我做的第一道整除分块)
题目描述
Bob is interested in GCDs (Greatest Common Divisors). Formally, for some positive integers (a1,...,ak), we denote gcd(a1,...,ak) as the greatest positive integer d, such that d∣ai for each 1≤i≤k
Bob has chosen an interval [l,r]. He is going to choose k distinct integers from the interval and compute their GCD. There are many integers that can be the final answer, and Bob is curious in the number of such integers. Please write a program to tell him the answer.
输入描述:
The first and only line of input consists of three integers l,r,k(1≤l≤r≤,2≤k≤r−l+1)
输出描述:
Print a single integer, indicating your answer.
输入
5 9 2
输出
3
说明
For the sample, it is possible to get 1,2,3 as the final GCD, by choosing numbers (7,9),(6,8) and (6,9). It turns out that any other integer cannot become the GCD, thus the answer is 3.
题意:
![]()
思路:(注意下图 l 和 1 的区别)

#include<bits/stdc++.h>
using namespace std;
#define int long long
int l, r, k;
bool check(int gcd) {
return (r / gcd - (l - 1) / gcd) >= k;
}
signed main()
{
cin >> l >> r >> k;
int ans = 0;
for (int i = 1, j; i <= r; i = j + 1)
{
if (i < l) j = min(r / (r / i), (l - 1) / ((l - 1) / i));
else j = r / (r / i);
ans += (j - i + 1) * (check(i));
}
cout << ans << '\n';
return 0;
}
边栏推荐
- 【时序逻辑电路】——计数器
- 安全:如何为行人提供更多保护
- Buffer overflow vulnerability lab experiment record
- DBF menu driver: librarydatabaseproject
- Mysql增删改查、检索与约束(详细教学)
- [GNN report] Tencent AI Lab Xu TingYang: graph generation model and its application in molecular generation
- [sequential logic circuit] - counter
- 什么是模糊理论,基础,流程
- Programming language exercises (I)
- AutoCAD - join merge command
猜你喜欢

调整图像亮度的滚动条演示实验

AXI协议(3):AXI架构的握手机制和实现细节

Live review | wonderful playback of Apache pulsar meetup (including PPT download)

The latest Zhejiang construction safety officer simulation question bank and answers in 2022

Qsqldatabase: solution of qmmysql driver not loaded

AXI协议(2):AXI架构的五个通道和两种事务

自定义类型:枚举
[redis] -1. two ways of setting up environment based on docker

An example of using viewthatfits adaptive view in swiftui 4.0

Mysql增删改查、检索与约束(详细教学)
随机推荐
The orders in the same city are delivered in the same city, and the order explosion is still handy!
GDB online debugging of work notes
Code random notes_ Linked list_ 707 design linked list
The third edition of New Horizon College English reading and Writing Tutorial 4 graduation examination site (units 1,2,3,5,6)
Mysql增删改查、检索与约束(详细教学)
Sword finger offer 48. the longest substring without repeated characters
Small end format and big end format (little endian & big endian)
Problems encountered in upgrading chrome to version 80 - solutions to system login failure
QML coding specification
IP第十三天笔记
Internet Download Manager配置
Pat class A - check in and check out
Cann training camp learns the animation stylization and AOE ATC tuning of the second season of 2022 model series
ShardingSphere数据库读写分离
Method of querying comma separated strings in a field by MySQL
Axi protocol (1): introduction to AMBA bus, introduction to Axi concept and background, characteristics and functions of Axi protocol
HCNP Routing&Switching之DHCP中继
The latest Zhejiang construction safety officer simulation question bank and answers in 2022
Pat a - correct spelling
The industrial information security center takes the lead in building a data circulation platform, and ant group and other manufacturers provide technical support