整除分块
前置芝士:
1.脑子
2.电脑及其配件
整除分块:
我们常常需要求这种柿子:
暴力求是 毒瘤)出题人会将 n 的范围调成
我们先来一个暴力程序打表找找规律:
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
int n = 0, m = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-')
m = -1;
c = getchar();
}
while (isdigit(c)) {
n = (n << 1) + (n << 3) + c - '0';
c = getchar();
}
return n * m;
}
signed main() {
int n = read();
for (int i = 1; i <= n; i++)
cout << n / i << ' ';
cout << '\n';
return 0;
}
输入:
10
输出:
10 5 3 2 2 1 1 1 1 1
看似没规律,可是
所以可以根据此规律得出
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
int n = 0, m = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-')
m = -1;
c = getchar();
}
while (isdigit(c)) {
n = (n << 1) + (n << 3) + c - '0';
c = getchar();
}
return n * m;
}
signed main() {
int n = read(), ans = 0;
for (int l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
ans += (n / l) * (r - l + 1);//(r-l+1)是块的长度,可以自己脑补一下
}
cout << ans << '\n';
return 0;
}
同学非要做水题,就推荐这些水题做做:
UVA11526 H(n)
为什么就一道题呢?因为其他过水已隐藏。