-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sqrt-decomposition.md): 新增向上取整的数论分块相关内容
- Loading branch information
Showing
7 changed files
with
85 additions
and
12 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
docs/math/code/sqrt-decomposition/sqrt-decomposition_1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <stdio.h> | ||
|
||
long long H(int n) { | ||
long long res = 0; // 储存结果 | ||
int l = 1, r; // 块左端点与右端点 | ||
while (l <= n) { | ||
r = n / (n / l); // 计算当前块的右端点 | ||
res += (r - l + 1) * 1LL * | ||
(n / l); // 累加这一块的贡献到结果中。乘上 1LL 防止溢出 | ||
l = r + 1; // 左端点移到下一块 | ||
} | ||
return res; | ||
} | ||
|
||
main() { | ||
int t, n; | ||
for (scanf("%d", &t); t--; scanf("%d", &n), printf("%lld\n", H(n))); | ||
} |
22 changes: 22 additions & 0 deletions
22
docs/math/code/sqrt-decomposition/sqrt-decomposition_2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stdio.h> | ||
|
||
#include <algorithm> | ||
#define N 100009 | ||
int n, a[N], maxn; | ||
long long ans[N]; | ||
|
||
main() { | ||
scanf("%d", &n); | ||
for (int i = 1; i <= n; scanf("%d", a + i), maxn = std::max(maxn, a[i]), ++i); | ||
for (int i = 0; i < n; ++i) | ||
for (int l = 1, r;; l = r + 1) { | ||
r = std::min(l < a[i] ? (a[i] - 1) / ((a[i] - 1) / l) : N, | ||
l < a[i + 1] ? (a[i + 1] - 1) / ((a[i + 1] - 1) / l) | ||
: N); // 二维数论分块 | ||
if (r == N) break; | ||
int x = (a[i + 1] - 1) / l - std::max(a[i] - 1, 0) / l; | ||
if (x > 0) ans[l] += x, ans[r + 1] -= x; // 累加贡献 | ||
} | ||
++ans[0]; // ⌈a/l⌉=(a-1)/l+1的式子当a=0时不成立,需要修正 | ||
for (int i = 1; i <= maxn; printf("%lld ", ans[i] += ans[i - 1]), ++i); | ||
} |
2 changes: 2 additions & 0 deletions
2
docs/math/examples/sqrt-decomposition/sqrt-decomposition_1.ans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
10 | ||
27 |
3 changes: 3 additions & 0 deletions
3
docs/math/examples/sqrt-decomposition/sqrt-decomposition_1.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2 | ||
5 | ||
10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
10 6 4 3 2 2 1 |
2 changes: 2 additions & 0 deletions
2
docs/math/examples/sqrt-decomposition/sqrt-decomposition_2.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
3 | ||
5 2 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters