跳到主要内容

题解:P10373 [AHOI2024 初中组] 立方根

· 阅读需 1 分钟
lailai
Blogger

原题链接

解题思路

令:

t=x3t=\left\lfloor \sqrt[3]x \right\rfloor

则:

j=1xj13=i=1xj3=j=1t31j3+j=t3xj3=i=1t1j=i3(i+1)31j3+j=t3xj3=i=1t1j=i3(i+1)31i+j=t3xt=(i=1t1((i+1)3i3)i)+(xt3+1)t=(i=1t13i3+3i2+i)+(xt3+1)t=34t412t314t2t4+(x+1)t=(x+1)tt4+2t3+t24\begin{aligned} \sum_{j=1}^x \left\lfloor j^{\frac{1}{3}} \right\rfloor &= \sum_{i=1}^x \left\lfloor \sqrt[3]j \right\rfloor \\ &= \sum_{j=1}^{t^3-1} \left\lfloor \sqrt[3]j \right\rfloor + \sum_{j=t^3}^{x} \left\lfloor \sqrt[3]j \right\rfloor \\ &= \sum_{i=1}^{t-1} \sum_{j=i^3}^{(i+1)^3-1} \left\lfloor \sqrt[3]j \right\rfloor + \sum_{j=t^3}^{x} \left\lfloor \sqrt[3]j \right\rfloor \\ &= \sum_{i=1}^{t-1} \sum_{j=i^3}^{(i+1)^3-1} i + \sum_{j=t^3}^{x} t \\ &= \left ( \sum_{i=1}^{t-1} \left((i+1)^3-i^3\right) \cdot i \right ) + (x-t^3+1) \cdot t \\ &= \left ( \sum_{i=1}^{t-1} 3i^3+3i^2+i \right ) + (x-t^3+1) \cdot t \\ &= \frac{3}{4} t^4- \frac{1}{2} t^3 -\frac{1}{4} t^2-t^4+(x+1) \cdot t \\ &= (x+1) \cdot t - \frac{t^4+2t^3+t^2}{4} \end{aligned}

由于 cbrt(x)pow(x,1.0/3) 可能会有精度误差,可以手写二分开立方根。

参考代码

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
ll cube_root(ll x)
{
ll l=0,r=100000;
while(l<r)
{
ll mid=l+r>>1;
if(mid*mid*mid>x)r=mid;
else l=mid+1;
}
return l-1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin>>q;
while(q--)
{
ll x;
cin>>x;
ll t=cube_root(x);
cout<<(x+1)*t-(t*t*t*t+2*t*t*t+t*t)/4<<'\n';
}
return 0;
}