学习笔记 整除分块
Date: 2021/10/12 Author: 杳鸢
首先,让我们引入一个式子
看起来,似乎很简单?如果将值域改为
我们将每一个
令
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
|---|---|---|---|---|---|---|---|---|---|
| 9 | 4 | 3 | 2 | 1 | 1 | 1 | 1 | 1 |
发现了什么规律?区间
由此,我们便可以通过每一块值域相同的区间,来快速计算上面的式子
代码如下:
//P1403
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n,ans=0;
int main(){
scanf("%lld",&n);
for(ll l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans+=(r-l+1)*(n/l);
}
printf("%lld\n", ans);
}
每一块的区间左端点为
例1 P2261 [CQOI2007]余数求和
题目大意
给出正整数
其中
解题过程
首先我们将
然后我们将
可以将
源代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n,k,ans;
int main(){
scanf("%lld %lld",&n,&k);
ans=n*k;
for(ll l=1,r;l<=n;l=r+1){
if(k/l!=0) r=min(k/(k/l),n);
else r=n;
ans-=(l+r)*(r-l+1)/2*(k/l);
}
printf("%lld\n", ans);
}
P2424 约数和
题目大意
令
解题过程
我们先从区间 [1,n] 看起,令
| 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | |||
| 3 | 3 | ||||
| 4 | |||||
| 5 | |||||
| 6 |
不难发现:
对
源代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll X,Y;
ll ans1,ans2;
int main(){
scanf("%lld %lld",&X,&Y);
ans1=ans2=0;
for(int l=1,r;l<=X-1;l=r+1){
r=min((X-1)/((X-1)/l),X-1);
ans1+=((X-1)/l)*(l+r)*(r-l+1)/2;
}
for(ll l=1,r;l<=Y;l=r+1){
r=min(Y/(Y/l),Y);
ans2+=(Y/l)*(l+r)*(r-l+1)/2;
}
printf("%lld\n", ans2-ans1);
}
P6583 回首过去
题目大意
给定正整数
解题过程
-
Subtask 1
理解题意之后,我们很容易得出下面这个式子
\sum_{i=1}^{n} \sum_{j=1}^{n} [i==gcd(i,j)\cdot2^a5^b] 其中
a,b 分别表示i 中含有因数2,5的个数直接暴力求解即可
-
Subtask 2
容易发现,对于
i ,答案为\lfloor \frac{n}{\frac{i}{2^a5^b}} \rfloor ,我们可得到\sum_{i=1}^{n}\lfloor\frac{n}{\frac{i}{2^a5^b}}\rfloor 其中
a,b 分别表示i 中含有因数2,5的个数根据式子求解即可
-
Subtask 3
我们将公式化简一下,可以得到
\sum^{n}_{i=1}\lfloor\frac{2^a5^bn}{i}\rfloor 其中
a,b 分别表示i 中含有因数2,5的个数有没有看到熟悉的东西?接下来我们将
2^a5^b 提出来,得到\sum_{a=0}^{40}\sum_{b=0}^{20}\sum_{i=1}^{2^a5^bi\le n}\lfloor\frac{n}{i}\rfloor\cdot[i\%2!=0\text{ and }i\%5!=0] i$中此时不含因数$2,5 我们来思考一下怎么将最外面两层循环替换掉:
我们固定
i ,对于i 有1\le2^a5^bi\le n 即1\le 2^a5^b\le \lfloor\frac{n}{i}\rfloor ,所以我们只需要对i 求出在[1,\lfloor\frac{n}{i}\rfloor] 内所有形如2^a5^b 的的个数,我们令f(x) 表示[1,x] 内所有形如2^a5^b 的个数,式子转化为:\sum_{i=1}^{n}f(\lfloor\frac{n}{i}\rfloor)\cdot\lfloor\frac{n}{i}\rfloor\cdot[i\%2!=0\text{ and }i\%5!=0] 至于
f(x) 如何去求呢?我们通过简单的计算,可得在
[1,10^{12}] 内,形如2^a5^b 的数共有371个。所以我们将每一个形如
2^a5^b 数字存入数组,进行排序,利用二分查找位置即可得到答案。接下来我们对
\lfloor\frac{n}{i}\rfloor 进行分块,设\lfloor\frac{n}{i}\rfloor 相同的区间为[l,r] 我们看到后面:
[i\%2!=0\text{ and }i\%5!=0] 在
[l,r] 中i 并不是连续的!考虑容斥老样子,我们先讨论
[1,n] 中的不含2,5 倍数的数字之和对于
2 的倍数,我们很容易得到个数n/2 对于
5 的倍数,我们很容易得到个数n/5 但是 有些数字既是
2 的倍数 又是5 的倍数,直接减去,会将这一部分多减一次,我们要加回来,即加上这两个数字的最小公倍数lcm(2,5)=10 的倍数个数n/10 最后得到
[1,n] 中不含2,5 的倍数数字之和:G(1,n)=n-\frac{n}{2}-\frac{n}{5}+\frac{n}{10} 那么求 $G(l,r)$ 也很简单了,通过前缀和计算答案: $$ G(l,r)=G(1,r)-G(1,l-1) $$ 所以最后每一块 $[l,r]$ 的答案就是 $$ G(l,r)\cdot\lfloor\frac{n}{i}\rfloor\cdot f(\lfloor \frac{n}{i} \rfloor) $$
源代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll Maxn=1e12+5;
ll a[1000],cnt=0;
ll n;
ll F(ll x){
return upper_bound(a+1, a+1+cnt,x)-a-1;
}
ll G(ll l,ll r){
ll res=r-l+1;
res-=(r/2)-(l-1)/2;
res-=(r/5)-(l-1)/5;
res+=(r/10)-(l-1)/10;
return res;
}
ll ans=0;
int main(){
for(ll i=1;i<=Maxn;i*=2){
for(ll j=i;j<=Maxn;j*=5){
a[++cnt]=j;
}
}
sort(a+1,a+1+cnt);
scanf("%lld",&n);
for(ll l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans+=F(n/l)*(n/l)*G(l,r);
}
printf("%lld\n", ans);
}
//特别注意,此处的F(x)同上文的f(x),G(l,r)同上文的G(l,r)