题解:P15765 [JAG 2025 Summer Camp #1] Sum of Floor(N/ij)
gitxiaozheng · · 题解
双倍经验和三倍经验。
枚举自然是简单的,但是整除分块还是最直观的。
以下记所求式子为
设:
则容易注意到
于是
事实上,对于
因此我们可以在计算 long long 就足以通过了。
想要进一步缩小常数只需使用记忆化大法,每计算一个 map 存起来,避免重复的
整体时间复杂度大概是
::::success[AC code & 提交记录]
//edit by devc++
//by gitxiaozheng/dakada
#include<bits/stdc++.h>
using namespace std;
#define L long long
#define iotype L //zigai
#define space putchar(' ')
#define enter putchar('\n')
inline iotype qcin();
void qcout(iotype);
map<L,L> fn;
inline L F(int n){
if(fn[n]!=0){
return fn[n]; //之前计算过的直接返回
}
L res=0ll;
int m=sqrt(n);
for(int i=1;i<=m;i++){
res+=(L)(n/i);
}
return fn[n]=((res<<1)-m*m); //记忆化
}
inline L S(int n){
L res=0ll;
for(int l=1,r;l<=n;l=r+1){
r=n/(n/l);
res+=1ll*(r-l+1)*F(n/l);
}
return res;
}
int main(){
//Your code is here...
for(int T=qcin();T;T--){
int a=qcin();
qcout(S(a));
enter;
}
return 0;
}
inline iotype qcin(){
iotype k=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-'){
f=-1;
}
c=getchar();
}
while(c>='0'&&c<='9'){
k=k*10+c-'0';
c=getchar();
}
return k*f;
}
void qcout(iotype x){
static int num[20];
int tot=0;
if(x<0){
putchar('-');
x=-x;
}
while(x>=10){
num[++tot]=x%10;
x/=10;
}
putchar(x+'0');
for(int i=tot;i>=1;i--){
putchar(num[i]+'0');
}
}
| 提交记录。 |
|---|