题解:P10414 [蓝桥杯 2023 国 A] 2023 次方

· · 题解

可以暴力,慢慢等(很久)

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int LEN = 2e4 + 10;
static const int Per = 8, N = LEN / Per + 5;
static const int Base = 1e8;
typedef long long ll;
typedef complex<double> Cpx;
namespace FFT {
    const double pi = acos(-1);
    const int FFT_BASE = 1e4;
    int rev[LEN];
    ll tmp[LEN];
    Cpx a[LEN], b[LEN], w[LEN];
    void FFT(Cpx *a, int len) {
        for (int i = 0; i < len; ++i) if (i > rev[i]) swap(a[i], a[rev[i]]);
        for (int d = 1; d < len; d <<= 1)
            for (int m = d << 1, i = 0; i < len; i += m)
                for (int j = 0; j < d; ++j) {
                    Cpx tmp = a[i + j + d] * w[len / m * j];
                    a[i + j + d] = a[i + j] - tmp;
                    a[i + j] += tmp;
                }
    }
    void Solve(int *x, int &lenx, const int *y, int leny) {
        for (int i = 0; i <= lenx; ++i) a[i << 1] = Cpx(x[i] % FFT_BASE, 0), a[i << 1 | 1] = Cpx(x[i] / FFT_BASE, 0);
        for (int i = 0; i <= leny; ++i) b[i << 1] = Cpx(y[i] % FFT_BASE, 0), b[i << 1 | 1] = Cpx(y[i] / FFT_BASE, 0);
        lenx = lenx << 1 | 1; leny = leny << 1 | 1;
        int len = 1; while (len <= lenx + leny) len <<= 1;
        for (int i = lenx + 1; i < len; ++i) a[i] = Cpx(0, 0);
        for (int i = leny + 1; i < len; ++i) b[i] = Cpx(0, 0);
        for (int i = 0; i < len; ++i) w[i] = Cpx(cos(2 * pi * i / len), sin(2 * pi * i / len));
        for (int i = 0; i < len; ++i) rev[i] = rev[i >> 1] >> 1 | (i & 1 ? len >> 1 : 0);
        FFT(a, len); FFT(b, len);
        for (int i = 0; i < len; ++i) a[i] *= b[i]; reverse(a + 1, a + len);
        FFT(a, len);
        for (int i = 0; i << 1 <= lenx + leny; ++i) tmp[i] = (ll)(a[i << 1].real() / len + 0.5) + (ll)(a[i << 1 | 1].real() / len + 0.5) * FFT_BASE;
        lenx = (lenx + leny) >> 1;
        tmp[lenx + 1] = 0;
        for (int i = 0; i <= lenx; ++i) {
            x[i] = tmp[i] % Base;
            tmp[i + 1] += tmp[i] / Base;
        }
        if (tmp[lenx + 1]) ++lenx, x[lenx] = tmp[lenx];
        while (lenx && x[lenx] == 0) --lenx;
    }
}

class Bigint {
private:
    //other function
    inline void AppendInt(string &ans, int x) const {
        static char build_string_tmp[10]; int tot = 0;
        for (; x; x /= 10) build_string_tmp[tot++] = x % 10 + 48;
        if (!tot) ans.append(1, '0');
        for (int i = tot; i; --i) ans.append(1, build_string_tmp[--tot]);
    }
    inline int DigitNum(int x) const {
        int ans = 0;
        if (x >= 10000) ans += 4, x /= 10000;
        if (x >= 100) ans += 2, x /= 100;
        return ans + (x >= 10) + 1;
    }
    inline void DelPre0() {
        while (len && v[len] == 0) --len;
    }

public:
    bool is_pos;
    int v[N], len;
    //by default, 0 is positive and 0 has length 0.
    inline bool IsZero() const {
        return len == 0 && v[len] == 0;
    }
    inline void AppendEnd(int x) {
        if (!IsZero()) ++len;
        for (int i = len; i; --i) v[i] = v[i - 1];
        v[0] = x;
    }

    //Constructors
    Bigint(): is_pos(1), len(0) {
        v[0] = 0;
    }
    Bigint(ll x) {
        is_pos = x < 0 ? x = -x, 0 : 1;
        len = -1;
        for (; x; x /= Base) v[++len] = x % Base;
        if (len == -1) v[++len] = 0;
    }
    Bigint(const Bigint &a) {
        is_pos = a.is_pos;
        len = a.len;
        memcpy(v, a.v, (len + 1) * sizeof *v);
    }
    Bigint(const string &str) {
        string::const_iterator bg = str.begin(), ed = str.end();
        is_pos = *bg == '-' ? ++bg, 0 : 1;
        len = -1;
        for (int qu = Base; ; qu *= 10) {
            if (qu == Base) v[++len] = 0, qu = 1;
            v[len] += (*--ed ^ 48) * qu;
            if (bg == ed) break;
        }
    }
    Bigint& operator=(const Bigint &a) {
        is_pos = a.is_pos;
        len = a.len;
        memcpy(v, a.v, (len + 1) * sizeof *v);
        return *this;
    }

    //Add
    Bigint& operator++() { return *this += 1; }
    Bigint operator++(int) { Bigint tmp(*this); *this += 1; return tmp; }
    Bigint& operator+=(ll x) {
        if ((x >= 0) == is_pos) {
            if (x < 0) x = -x;
            for (int i = 0; i <= len && x; ++i) {
                x += v[i];
                v[i] = x % Base;
                x /= Base;
            }
            while (x) v[++len] = x % Base, x /= Base;
        } else {
            if (x < 0) x = -x;
            for (int i = 0; i <= len && x; ++i) {
                v[i] -= x % Base;
                x /= Base;
                if (v[i] < 0) ++x, v[i] += Base;
            }
            while (x) v[++len] = -(x % Base), x /= Base;
            DelPre0();
            if (len == 0 && v[0] == 0) is_pos = 1;
            if (v[len] < 0) {
                is_pos ^= 1;
                for (int i = 0; i <= len; ++i) v[i] = -v[i];
                for (int i = 0; i <= len; ++i) if (v[i] < 0) v[i] += Base, --v[i + 1];
                DelPre0();
            }
        }
        return *this;
    }
    Bigint operator+(ll x) const {
        Bigint rst(*this);
        return rst += x;
    }
    friend Bigint operator+(ll x, const Bigint &a) {
        return a + x;
    }
    Bigint& operator+=(const Bigint &a) {
        if (a.is_pos == is_pos) {
            while (len < a.len) v[++len] = 0;
            v[len + 1] = 0;
            for (int i = 0; i <= len; ++i) {
                v[i] += i <= a.len ? a.v[i] : 0;
                if (v[i] >= Base) v[i] -= Base, ++v[i + 1];
            }
            if (v[len + 1]) ++len;
        } else {
            while (len < a.len) v[++len] = 0;
            v[len + 1] = 0;
            for (int i = 0; i <= len; ++i) {
                v[i] -= i <= a.len ? a.v[i] : 0;
                if (v[i] < 0) v[i] += Base, --v[i + 1];
            }
            if (v[len + 1]) ++len;
            DelPre0();
            if (IsZero()) is_pos = 1;
            if (v[len] < 0) {
                is_pos ^= 1;
                for (int i = 0; i <= len; ++i) v[i] = -v[i];
                for (int i = 0; i <= len; ++i) if (v[i] < 0) v[i] += Base, --v[i + 1];
                DelPre0();
            }
        }
        return *this;
    }
    Bigint operator+(const Bigint &a) const {
        Bigint rst(*this);
        return rst += a;
    }

    //Subtraction
    Bigint& operator--() { return *this += -1; }
    Bigint operator--(int) { Bigint tmp(*this); *this += -1; return tmp; }
    Bigint& operator-=(ll x) {
        return *this += -x;
    }
    Bigint operator-(ll x) const {
        Bigint rst(*this);
        return rst += -x;
    }
    friend Bigint operator-(ll x, const Bigint &a) {
        Bigint tmp(a);
        tmp.is_pos ^= 1;
        return tmp += x;
    }
    Bigint& operator-=(const Bigint &x) {
        Bigint tmp(x); tmp.is_pos ^= 1;
        return *this += tmp;
    }
    Bigint operator-(const Bigint &x) const {
        Bigint rst(*this), tmp(x); tmp.is_pos ^= 1;
        return rst += tmp;
    }

    //Multiplication
    static const int FFT_LIM = 5000;
    Bigint& operator*=(int x) {
        if (!x) return *this = Bigint();
        static ll tmp[N];
        if (x < 0 && !IsZero()) is_pos ^= 1, x = -x;
        for (int i = 0; i <= len; ++i) tmp[i] = (ll)v[i] * x;
        tmp[len + 1] = 0;
        for (int i = 0; i <= len; ++i) {
            v[i] = tmp[i] % Base;
            tmp[i + 1] += tmp[i] / Base;
        }
        if (tmp[len + 1]) ++len, v[len] = tmp[len];
        return *this;
    }
    Bigint operator*(int x) const {
        Bigint tmp(*this);
        return tmp *= x;
    }
    friend Bigint operator*(int b, const Bigint &a) {
        return a * b;
    }
    Bigint operator*=(const Bigint &x) {
        if (IsZero() || x.IsZero()) return *this = Bigint();
        if (!x.is_pos) is_pos ^= 1;
        if (len * x.len >= FFT_LIM) return FFT::Solve(v, len, x.v, x.len), *this;
        static ll tmp[N];
        memset(tmp, 0, (len + x.len + 1) * sizeof *tmp);
        for (int i = 0; i <= len; ++i)
            for (int j = 0; j <= x.len; ++j)
                tmp[i + j] += (ll)v[i] * x.v[j];
        len = len + x.len; tmp[len + 1] = 0;
        for (int i = 0; i <= len; ++i) {
            v[i] = tmp[i] % Base;
            tmp[i + 1] += tmp[i] / Base;
        }
        if (tmp[len + 1]) ++len, v[len] = tmp[len];
        return *this;
    }
    Bigint operator*(const Bigint &x) const {
        Bigint tmp(*this);
        return tmp *= x;
    }

    //Division & modulo operation
    Bigint& operator/=(int x) {
        if (IsZero()) return *this;
        if (x == 0) {
            cerr << "Devide by 0!" << endl;
            assert(0);
        }
        if (x < 0) is_pos ^= 1, x = -x;
        ll val = 0;
        for (int i = len; ~i; --i) {
            val = val * Base + v[i];
            v[i] = val / x % Base;
            val %= x;
        }
        DelPre0();
        return *this;
    }
    Bigint operator/(int x) const {
        Bigint tmp(*this);
        return tmp /= x;
    }
    Bigint& Devide(const Bigint &_a, int ty) {
        if (IsZero()) return *this;
        if (_a.IsZero()) {
            cerr << "Devide by 0!" << endl;
            assert(0);
        }
        Bigint a(_a);
        bool fif = is_pos == a.is_pos;
        a.is_pos = is_pos = 1;
        Bigint rm;
        for (int i = len; ~i; --i) {
            rm.AppendEnd(v[i]);
            for (int l = 0, r = Base - 1, mid; l <= r; a * (mid = (l + r) >> 1) <= rm ? v[i] = mid, l = mid + 1 : r = mid - 1);
            rm -= a * v[i];
        }
        if (ty) *this = rm;
        DelPre0(); is_pos = fif; return *this;
    }
    Bigint& operator/=(const Bigint &a) {
        return Devide(a, 0);
    }
    Bigint operator/(const Bigint &a) const {
        Bigint tmp(*this);
        return tmp /= a;
    }
    int operator%(const int x) const {
        if (IsZero()) return 0;
        if (!x) {
            cerr << "Mod 0!" << endl;
            assert(0);
        }
        ll rm = 0;
        for (int i = len; ~i; --i) rm = (rm * Base + v[i]) % x;
        return rm;
    }
    int operator%=(const int x) {
        int ans = *this % x;
        *this = ans;
        return ans;
    }
    Bigint operator%=(const Bigint &a) {
        return Devide(a, 1);
    }
    Bigint operator%(const Bigint &a) const {
        Bigint tmp(*this);
        return tmp %= a;
    }

    //Compare
    bool operator<(const Bigint &a) const {
        if (is_pos != a.is_pos) return !is_pos;
        if (len != a.len) return (len < a.len) ^ !is_pos;
        for (int i = len; ~i; --i) if (v[i] != a.v[i]) return (v[i] < a.v[i]) ^ !is_pos;
        return 0;
    }
    bool operator>(const Bigint &a) const {
        return a < *this;
    }
    bool operator==(const Bigint &a) const {
        return !(a < *this) && !(*this < a);
    }
    bool operator<=(const Bigint &a) const {
        return !(a < *this);
    }
    bool operator>=(const Bigint &a) const {
        return !(*this < a);
    }
    bool operator!=(const Bigint &a) const {
        return (a < *this) || (*this < a);
    }

    //Input&Output
    string to_string() const {
        string ans;
        if (!is_pos) ans.append(1, '-');
        AppendInt(ans, v[len]);
        for (int i = len - 1; ~i; --i) ans.append(Per - DigitNum(v[i]), '0'), AppendInt(ans, v[i]);
        return ans;
    }
    ll to_ll() const { //please make sure it can be stored in long long
        ll ans = v[len];
        for (int i = len - 1; ~i; --i) ans = ans * Base + v[i];
        return is_pos ? ans : -ans;
    }
    friend istream& operator>>(istream &in, Bigint &a) {
        string str;
        in >> str;
        a = Bigint(str);
        return in;
    }
    friend ostream& operator<<(ostream &out, const Bigint &a) {
        return out << a.to_string();
    }

    //Others
    friend Bigint abs(const Bigint &a) {
        Bigint tmp(a);
        tmp.is_pos = 1;
        return tmp;
    }
    friend Bigint& gcd(const Bigint &a, const Bigint &b) {
        static Bigint ta, tb;
        int tm = 0;
        ta = a; tb = b;
        ta.is_pos = tb.is_pos = 1;
        while (!ta.IsZero() && !tb.IsZero()) {
            bool odda = ta.v[0] & 1, oddb = tb.v[0] & 1;
            if (!odda && !oddb) {
                ++tm;
                ta /= 2;
                tb /= 2;
            } else if (!odda) {
                ta /= 2;
            } else if (!oddb) {
                tb /= 2;
            } else {
                ta >= tb? ta -= tb : tb -= ta;
            }
        }
        if (!tb.IsZero()) ta = tb;
        while (tm--) ta *= 2;
        return ta;
    }

    //Debug
    inline void Debug(string info = "") const {
        cout << info << endl;
        printf("Positive? %d\n", is_pos);
        printf("Len = %d\n", len);
        for (int i = len; ~i; --i) printf("%08d~", v[i]); puts("");
    }
};
using namespace FFT;
int main(){
    Bigint ans=1;
    for(int i=2;i<=2023;i++){
        ans*=i;
    }
    Bigint anss=2;
    for(Bigint i=0;i<=ans;i*=2){
        anss*=anss;
        anss%=2023;
    }
    return 0;
}

实际上就是计算 2^{319182976591646561168 \dots} = 2^{3.19182976591646561168 \dots \times 10^{5812}}。

光是指数就有5812位数!!!所以我决定用Python。

如果循环,每次自乘,则需要 10^{2906} 次,大约要 10^{2874} 天。

n=1
for i in range(1,2024):
    n*=i
print(n)
print((2**n)%2023)

总之慢慢等吧(答案是 8 \times 10^2 +\dots)