题目链接
思路 && 代码
数论分块
算是数论分块的模板题了吧
20分做法
纯暴力,直接枚举,然后每个数 O ( n ) O(\sqrt{n}) O(n) 判断,时间复杂度 O ( n n ) O(n \sqrt{n}) O(nn)
需要注意不要闷着头一直枚举到 n \sqrt{n} n,如果 n n n的约数 i i i的平方恰好等于 n n n,只加一个就足够了
int x, y, ans;
signed main() {
x = read(), y = read();
for (int i = x; i <= y; i++) {
for (int j = 1; j <= sqrt(i); j++) {
if (!(i % j)) {
if (j * j == i) ans += j;
else ans += j + (i / j);
}
}
}
cout << ans << '\n';
return 0;
}
60分做法
也是一种暴力 思路是转枚举约数为枚举倍数
容易得出一个数
i
i
i 在
n
n
n中一共有
⌊
n
i
⌋
\lfloor\frac{n}{i}\rfloor
⌊in⌋ 个
i
i
i的倍数,那么
i
i
i在
n
n
n中的贡献为
⌊
n
i
⌋
∗
i
\lfloor\frac{n}{i}\rfloor * i
⌊in⌋∗i
所以我们可以先求出
1
∼
y
1\sim y
1∼y中每个
i
i
i的贡献,然后再减去
1
∼
x
−
1
1\sim x-1
1∼x−1中每个
i
i
i的贡献
答案就是
∑
i
=
1
y
⌊
y
i
⌋
∗
i
−
∑
i
=
1
x
−
1
⌊
x
−
1
i
⌋
∗
i
\sum\limits_{i = 1}^{y} \lfloor\frac{y}{i}\rfloor * i - \sum\limits_{i = 1}^{x - 1} \lfloor\frac{x - 1}{i}\rfloor * i
i=1∑y⌊iy⌋∗i−i=1∑x−1⌊ix−1⌋∗i
时间复杂度 O ( y ) O(y) O(y)
int x, y, ans1, ans2;
signed main() {
x = read(), y = read();
for (int i = 1; i <= y; i++) ans1 += (y / i) * i;
for (int i = 1; i <= x - 1; i++) ans2 += ((x - 1) / i) * i;
cout << ans1 - ans2 << '\n';
return 0;
}
正解
我们来看一下 ⌊ n i ⌋ ( 1 ≤ i ≤ n ) \lfloor\frac{n}{i} \rfloor(1 \leq i \leq n) ⌊in⌋(1≤i≤n)的取值有什么规律,假设 n n n为 18 18 18,那么每个 ⌊ n i ⌋ \lfloor\frac{n}{i} \rfloor ⌊in⌋的值分别为
18 , 9 , 6 , 4 , 3 , 3 , 2 , 2 , 2 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 18,9,6,4,3,3,2,2,2,1,1,1,1,1,1,1,1,1 18,9,6,4,3,3,2,2,2,1,1,1,1,1,1,1,1,1
很显然,有很多 ⌊ n i ⌋ \lfloor\frac{n}{i} \rfloor ⌊in⌋的值是一样的,所以我们可以用数论分块来做
关于数论分块,如果不会可以去看Luckyblock的博客,内容详实,证明详细,讲的非常棒(实名墙裂推荐)
如果你看完了这篇博客,那么~~(我觉得我就没必要讲了)~~我们就继续讲
显然,我们现在的任务就是求出 ⌊ n i ⌋ \lfloor\frac{n}{i} \rfloor ⌊in⌋值相同的区间 l , r l,r l,r,初始化 l l l为 1 1 1,每次枚举新的值得时候 l = r + 1 l=r+1 l=r+1,那么问题来了, r r r怎么求呢?
其实并不难,上面的博客里也讲证明了,如果让我证明也就是把他的博客里的证明搬过来,就是
r
=
n
/
(
n
/
l
)
r=n/(n/l)
r=n/(n/l)
l l l和 r r r都明白了,那么答案每次怎么累加呢?
应该是 a n s + = ans+= ans+=此时的约数 * 这个区间的约数个数和
约数就是 n / l n / l n/l,因为 l l l就是当前数列的下标,所以 n / l n/l n/l就是约数,约数个数和也比较显然,就是 ∑ i = l r i \sum\limits_{i = l}^{r}i i=l∑ri,利用等差数列求和公式,就可以 O ( 1 ) O(1) O(1)求出这个式子的答案
等差数列求和公式的两种写法
a 1 a_1 a1是首项, a n a_n an是末项, d d d是公差, S S S是等差数列的和,那么写法就有以下两种
- S = n a 1 + n ( n − 1 ) 2 d S = na_1+\frac{n(n-1)}{2}d S=na1+2n(n−1)d
- S = n ( a 1 + a n ) / 2 S=n(a_1+a_n)/2 S=n(a1+an)/2
最后的答案就是算出的 1 ∼ y 1\sim y 1∼y的贡献减去 1 ∼ x − 1 1\sim x-1 1∼x−1的贡献,然后这道题就做完啦~
时间复杂度 O ( n ) O(\sqrt{n}) O(n)
/*
Author:loceaner
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int A = 1e7 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int x, y;
inline int sum(int l, int r) {
// return (r - l + 1) * (l + r) / 2;
return 1ll * ((r - l + 1) * l) + 1ll * ((r - l + 1) * (r - l) / 2);
}
inline int solve(int n) {
int ans = 0;
for (int l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
ans += (n / l) * sum(l, r);
}
return ans;
}
signed main() {
x = read(), y = read();
cout << solve(y) - solve(x - 1) << '\n';
return 0;
}

本文介绍了洛谷P2424题目的解法,包括20分、60分及正解策略。20分做法是枚举约数判断,60分做法通过枚举倍数计算贡献。正解利用数论分块优化,通过求解相同⌊in⌋值的区间来减少时间复杂度,最终实现O(n)的时间复杂度解题。

185

被折叠的 条评论
为什么被折叠?



