版本比较
题解:P15765 [JAG 2025 Summer Camp #1] Sum of Floor(N/ij)
正文
增加 60 行删除 27 行
1123456345633 行未改动404142434445494647514849535450515657525359605455565758596061626365666764656667697068697071737273747576777978798081828 行未改动9192939496959697989910 行未改动11011111211311411511611711811912012112212312412512612712812913013113213313411613513613711912013813914014114212212312414314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417580 行未改动显然这个 $T$ ~~非常大,达到了惊人的~~ $100$。启发我们不能用带 $T$ 的做法。直接整除分块套整除分块是 $O(Tn^{\frac 34})$。注意到 $ij\le n$ 才有贡献。
### 方法 1
显然有:
$$\gdef \so { \implies }\gdef \P { \mathbb{P} }\gdef \id { \mathrm{id} }33 行未改动\ea$$
于是我们只需要筛出 $d$ 即可。### 方法 1
显然数据范围不允许线性筛,于是我们考虑预处理一部分,其他的暴力。考虑平衡复杂度。$d(x)$ 函数前缀和可以 $O(\sqrt n)$ 计算。也能线性筛得到。
通常,预处理出 $n^{\frac{3}4}$ 可以做到 $O(n^{\frac 34})$。大约是 $5.62341\times 10^6$ 级别,很好通过。考虑 $<B$ 的线性筛,$\ge B$ 的暴力算。
:::info[分析]{open}我们的 `pre_d(x)` 函数:对于单个 $n$ 最多有 $\dfrac{n}B$ 次计算。
- 当 $x\le n^{\frac 34}$ 时,时间复杂度为 $O(1)$。- 当 $x> n^{\frac 34}$ 时,时间复杂度为 $O(\sqrt n)$。于是平衡一下:
情况 2 总共只有 $n^{\frac 14}$ 种,所以总时间复杂度为 $O(n^{\frac 14}\d n^{\frac 12}=n^{\frac 34})$:::$$B=T\cdot \dfrac{n\sqrt n}{B}\\\qquad\\\Rightarrow B=\sqrt {nT} \sqrt[4]n$$
复杂度 $O(\sqrt Tn^{\frac 34})$
感谢 @Bingxiu2 提出复杂度问题。
```cpp#include<bits/extc++.h>using namespace std;const int maxn=8e6+10;bitset<maxn> isp;int p[maxn],cnt=0,d[maxn],Cnt[maxn],pred[maxn];//cnt:最小质因子次数const int maxn=3e7+10;bool isp[maxn];int p[maxn],cnt=0,d[maxn],Cnt[maxn],pred[maxn],B;//cnt:最小质因子次数__gnu_pbds::gp_hash_table<int,long long> S_d;inline void get(){ isp.set();inline void get(int V){ for(int i=0;i<V;i++)isp[i]=1; isp[1]=0; pred[1]=d[1]=1; for(int i=2;i<maxn;i++){ for(int i=2;i<V;i++){ if(isp[i]){ p[++cnt]=i; d[i]=2; Cnt[i]=1; } for(int j=1,pr=p[j];j<=cnt&&i*pr<maxn;j++,pr=p[j]){ for(int j=1,pr=p[j];j<=cnt&&i*pr<V;j++,pr=p[j]){ isp[i*pr]=0; if(i%pr){ d[i*pr]=d[i]*d[pr]; Cnt[i*pr]=1;8 行未改动 }}#define endl '\n'inline long long pre_d(int n){ if(n<maxn)return pred[n]; if(n<B)return pred[n]; if(S_d[n])return S_d[n]; long long ans=0; for(int l=1,r;l<=n;l=r+1){ r=n/(n/l);10 行未改动 } return ans;}#define endl '\n'namespace FAST_IO{ #ifdef __linux__ #define getchar getchar_unlocked #define putchar putchar_unlocked #else #define getchar _getchar_nolock #define putchar _putchar_nolock #endif inline long long read(){ int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=(x << 1) + (x << 3)+ch-48;ch=getchar();} return x*f; }//快读 inline void write(long long x){ if(x<0){putchar('-');x=-x;} if(x>9)write(x/10); putchar(x%10+'0'); }//快输}using namespace FAST_IO;signed main(){ int T; int T,T0; ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>T; get(); T=read(); T0=T; int MAXN=0; vector<int> quer; while(T--){ int n; cin>>n; cout<<work(n)<<endl; quer.push_back(read()); MAXN=max(MAXN,quer.back()); } B=(int)sqrtl(1ll*T0*sqrt(MAXN)*MAXN)/3+10; // putchar('B'); // write(B); // putchar('\n'); // B=2e7; get(B); for(int i:quer){ write(work(i)); putchar('\n'); } return 0;} ```### 方法 2显然数据范围不允许线性筛,考虑使用杜教筛优化。
由于有 $\mu*d=1$ 的性质,我们可以杜教筛筛出 $\mu$ 的前缀和,再利用它筛出 $d$ 的前缀和即可。
时间复杂度 $O(n^{\frac 23})$。
但是……有点大炮打蚊子。:::warning{open}常数巨大!:::
```cpp#include<bits/extc++.h>using namespace std;const int maxn=1e7+10;80 行未改动