题解:P17563 「FAOI-R15」Program of base(x) 2029

· · 题解

题目大意

给定一个数 n,表示在 k 进制的 2029 转化为十进制后的值,然后我们需要求 k 的值。

如果不存在,输出 035966_L3。

题目解法

根据题意,我们不难列出方程为 2 \times k^3 + 2 \times k + 9 = n。

因为本题中 n 与 k 都是整数,所以我们可以采用因式分解的做法。

根据原方程,我们可以得到 2 \times k \times (k^2 + 1) = n - 9,所以我们知道了当 n 为偶数时无解。

我们继续,我们将方程两边同时除以 2,得到 k \times (k^2 + 1) = \frac{n - 9}{2}。

对于方程左侧,我们来进行一个范围估计,设方程左侧为 x。

因为 k^2 < k^2 + 1,所以我们可以得到 k^3 < x。

因为 k < k + 1 且 k^2 + 1 < (k + 1)^2,所以我们可以得到 k^3 < x < (k + 1)^3。

那么我们就可以直接将方程右侧开三次方根,然后向下取整,就可以得到 k 近似的整数,但近似值与实际值相差不超过 1。但由于可能无解,所以我们需要再带进去检查一下。

最后我们需要和 @Hey3_smallZ 抢最优解。

代码

::::success[代码]

#include <bits/stdc++.h>
#ifdef __linux__
    #include <bits/extc++.h>
    #define getchar getchar_unlocked
    using namespace __gnu_cxx;
    using namespace __gnu_pbds;
#endif
#define int long long
#define lc (p << 1)
#define rc (p << 1 | 1)
#define pii pair<int, int>
#define pdd pair<double, double>
#define pss pair<string, string>
#define ull unsigned int
#define push_back emplace_back
#define push emplace
#define min basic_operation :: my_min
#define max basic_operation :: my_max
#define abs basic_operation :: my_abs
#define chkmin basic_operation :: my_chkmin
#define chkmax basic_operation :: my_chkmax
#define sqrt sqrtl
#define double long double
#define all(x) (x).begin(), (x).end()
#define exall(x, n) (x) + 1, (x) + 1 + (n)
#define siz(x) ((int)(x).size())
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define rp(i, a, b) for (register int i = (a); i < (b); ++i)
#define pr(i, a, b) for (register int i = (a); i > (b); --i)
#define rtep(i, a, b, c) for (register int i = (a); i <= (b); i += (c))
#define pter(i, a, b, c) for (register int i = (a); i >= (b); i -= (c))
#define inl_force __attribute__((always_inline)) inline
#define fir first
#define sec second
using namespace std;
namespace basic_operation {
    template<typename Tp> inl_force Tp my_min(const Tp& x, const Tp& y) { return (x < y ? x : y); }
    template<typename Tp> inl_force Tp my_max(const Tp& x, const Tp& y) { return (x > y ? x : y); }
    template<typename Tp> inl_force Tp my_abs(const Tp& x) { return (x >= 0 ? x : -x); }
    template<typename Tp> inl_force void my_chkmin(Tp& x, const Tp& y) { x = min(x, y); }
    template<typename Tp> inl_force void my_chkmax(Tp& x, const Tp& y) { x = max(x, y); }
}
namespace FastIO {
    template<typename Tp> inl_force Tp input() {
        register Tp x = 0;
        register int f = 1;
        register char ch = getchar();
        while (!isdigit(ch)) {
            if (ch == EOF)  return 0;
            if (ch == '-')  f = -1;
            ch = getchar();
        }
        while (isdigit(ch)) {
            x = (x << 3) + (x << 1) + (ch ^ 48);
            ch = getchar();
        }
        return x * f;
    }
    template<typename Tp> inline void output(Tp x) {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9)  output(x / 10);
        putchar((x % 10) ^ 48);
    }
    template<typename Tp> inl_force void outputll(Tp x) { output(x); putchar(' '); }
    template<typename Tp> inl_force void outputln(Tp x) { output(x); putchar('\n'); }
    inl_force double double_input() { double x; scanf("%Lf", &x); return x;}
    inl_force void double_output(const double& x) { printf("%Lf", x); }
    inl_force char char_input() { return getchar(); }
    inl_force void char_output(const char& x) { putchar(x); }
    inl_force string string_input() { static char buf[1000005]; scanf("%s", buf); return string(buf); }
    inl_force void string_output(const string& s) { fputs(s.c_str(), stdout); }
    #define input() (input<int>())
} using namespace FastIO;
constexpr int N = 1e5 + 5;
int T, n;
inl_force void solve() {
    n = input() - 9;
    if (n % 2 || n < 0) {
        puts("035966_L3");
        return ;
    }
    register int sq = (int)cbrt(n / 2);
    if (sq * sq * sq + sq == n / 2 && sq > 9)       outputln(sq);
    else    puts("035966_L3");
}
signed main() {
#ifndef ONLINE_JUDGE
    freopen("problem.in", "r", stdin);
    freopen("problem.out", "w", stdout);
#endif
    register clock_t time = clock();
    T = input();
    while (T--)    solve();
#ifndef ONLINE_JUDGE
    cerr << "\ntime: " << clock() - time << "ms\n";
#endif
    return 0;
}

::::