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

· · 题解

很点的一道题。

从作者的提示和数据范围来看,暴力肯定过不了。
所以我们考虑优化。

从题目分析,我们可以枚举底数并求其的三次方来代替开三次根号,并且不会有精度问题。
然后只要加一个判断,判断输入的询问数据是否超过当前区间,并循环就可以过了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef ll RD_TYPE;
#define rd() ({RD_TYPE __x(0);bool f(0);char ch(getchar());while(ch<'0'||ch>'9')f^=ch=='-',ch=getchar();while(ch>='0'&&ch<='9')__x=(__x<<1)+(__x<<3)+(ch^48),ch=getchar();f?-__x:__x; })
#define tpw(__x) (__x * __x * __x)

ll now = 2, ask, sum = 1, last = 1;
int main()
{
    int q = rd();
    while (q--)
    {
        ask = rd();
        while (last != ask)
        {
            if (ask < tpw(now))
                sum += (ask - last) * (now - 1), last = ask;
            else
            {
                sum += (tpw(now) - last - 1) * (now - 1);
                last = tpw(now) - 1;
                ++now;
            }
        }
        printf("%lld\n", sum);
    }
    return 0;
}