时间复杂度是否更优
查看原帖
时间复杂度是否更优
764666
LostKeyToReach楼主2024/6/13 21:21
//  #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define ll long long
#define FORL(a, b, c, s) for (decltype(c - b) a = b; a <= c; a += s)
#define FORR(a, b, c, s) for (decltype(c - b) a = b; a >= c; a -= s)
#define writes(x) write(x), putchar(' ')
#define writeln(x) write(x), putchar('\n')
static char buf[100000], * pa(buf), * pb(buf);
#define gc pa == pb && (pb = (pa = buf) + fread(buf, 1, 100000, stdin), pa == pb) ? EOF : *pa++
using namespace std;
template <typename T> void read(T& x) {
    T t = 0, sgn = 1;
    char ch = gc;
    while (!isdigit(ch)) {
        if (ch == '-') sgn = -sgn;
        ch = gc;
    }
    while (isdigit(ch)) {
        t = (t << 3) + (t << 1) + (ch ^ 48);
        ch = gc;
    }
    x = sgn * t;
}
template <typename T, typename ...Args> void read(T& tmp, Args &...tmps) {
    read(tmp); read(tmps...);
}
template <typename T> void write(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
template <typename T, typename ...Args> void write(T& tmp, Args &...tmps) {
    writes(tmp);
    // putchar(' ');
    writes(tmps...);
}
int check(int x, int n) {
    int res = 0, cnt = 0;
    while (cnt * x <= n) {
        res = res + (cnt + 1) * x;
        ++cnt;
    }
    if (res > n) res = res - cnt * x;
    return res;
}
ll x, y, z, k;
ll maxn;
int main() {
    int t;
    read(t);
    while (t--) {
        read(x, y, z, k);
        maxn = 0;
        for (ll i = 1; i * i * i <= k; i++) {
            if (k % i == 0) {
                ll now = k / i;
                for (ll j = 1; j * j <= now; j++) {
                    if (now % j == 0) {
                        ll a = i, b = j, c = now / j;
                        if (x >= a && y >= b && z >= c) maxn = max(maxn, (x - a + 1) * (y - b + 1) * (z - c + 1));
                        if (x >= a && y >= c && z >= b) maxn = max(maxn, (x - a + 1) * (y - c + 1) * (z - b + 1));
                        if (x >= b && y >= a && z >= c) maxn = max(maxn, (x - b + 1) * (y - a + 1) * (z - c + 1));
                        if (x >= b && y >= c && z >= a) maxn = max(maxn, (x - b + 1) * (y - c + 1) * (z - a + 1));
                        if (x >= c && y >= a && z >= b) maxn = max(maxn, (x - c + 1) * (y - a + 1) * (z - b + 1));
                        if (x >= c && y >= b && z >= a) maxn = max(maxn, (x - c + 1) * (y - b + 1) * (z - a + 1));
                    }
                }
            }
        }
        writeln(maxn);
    }
}

这段代码时间复杂度是 O(k23)O(k^{\frac{2}{3}}),是否比官方题解更优?

2024/6/13 21:21
加载中...