QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#474247#8721. 括号序列Andyqian7AC ✓556ms114920kbC++205.4kb2024-07-12 16:54:532024-07-12 16:54:53

Judging History

你现在查看的是最新测评结果

  • [2024-07-12 16:54:53]
  • 评测
  • 测评结果:AC
  • 用时:556ms
  • 内存:114920kb
  • [2024-07-12 16:54:53]
  • 提交

answer

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

// end fast read template by CYJian

#define iter(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) iter(i, 0, a)
#define rep1(i, a) iter(i, 1, (a) + 1)
#define fi first
#define se second
#define pb push_back

#define ll long long
#define pii pair<int, int>

// #define int ll
const int MOD = 998244353;

int fac[524300], ifac[524300], inv[524300];
int ini[524300], bern[524300];

int d[524300];
int S[524300], f[524300], g[524300], h[524300];
int answer[524300];
namespace poly
{
    const int MOD = 998244353;
    const int IMAG = 86583718;
    const int NTTG = 3;

    int rev[524300];
    int minv[524300];
    int w[20][2][524300];

    int qpow(int b, int e)
    {
        int re = 1;
        while (e)
        {
            if (e & 1)
                re = 1ll * re * b % MOD;
            b = 1ll * b * b % MOD;
            e >>= 1;
        }
        return re;
    }

    void constructrev(int n)
    {
        for (int i = 1, j = 0; i < n; i++)
        {
            int bit = n >> 1;
            for (; j & bit; bit >>= 1)
                j ^= bit;
            j ^= bit;
            rev[i] = j;
        }
    }

    void constructroot(int n)
    {
        minv[1] = 1;
        iter(i, 2, n + 1)
            minv[i] = 1ll * (MOD - MOD / i) * minv[MOD % i] % MOD;
        for (int l = 1; (1 << l) <= n; l++)
            rep(inv, 2)
            {
                int re = inv ? qpow(minv[NTTG], (MOD - 1) >> l) : qpow(NTTG, (MOD - 1) >> l);
                w[l][inv][0] = 1;
                rep1(i, (1 << (l - 1)) - 1) w[l][inv][i] = 1ll * w[l][inv][i - 1] * re % MOD;
            }
    }

    void ntt(int *v, int n, bool inv)
    {
        rep(i, n) if (i < rev[i]) swap(v[i], v[rev[i]]);
        for (int l = 1; (1 << l) <= n; l++)
            for (int i = 0; i < n; i += (1 << l))
            {
                int p = i + (1 << (l - 1));
                iter(j, i, p)
                {
                    int a = v[j], b = 1ll * v[j + (1 << (l - 1))] * w[l][inv][j - i] % MOD;
                    v[j] = (a + b >= MOD ? a + b - MOD : a + b);
                    v[j + (1 << (l - 1))] = (a < b ? a + MOD - b : a - b);
                }
            }
        if (inv)
            rep(i, n) v[i] = 1ll * v[i] * minv[n] % MOD;
    }

    void mult(int *a, int as, int *b, int bs, int *o, bool construct, bool clean = 0, int th = 100000000)
    {
        iter(i, as, as << 1) a[i] = 0;
        iter(i, bs, bs << 1) b[i] = 0;
        int n = as + bs - 1;
        while (n - (n & (-n)))
            n += (n & (-n));
        if (construct)
            constructroot(n);
        constructrev(n);
        ntt(a, n, 0);
        ntt(b, n, 0);
        rep(i, n) o[i] = 1ll * a[i] * b[i] % MOD;
        ntt(o, n, 1);
        iter(i, th, n) o[i] = 0;
        rep(i, n) b[i] = 0;
        if (clean)
            rep(i, n) a[i] = 0;
    }

    void cfn(int *a, int as, int *o)
    {
        static int tmp[524300];
        if (as == 1)
        {
            tmp[0] = a[0];
            o[0] = qpow(a[0], MOD - 2);
            return;
        }
        cfn(a, (as + 1) / 2, o);
        int le = 0;
        while ((1 << le) < (as << 1))
            le++;
        constructrev(1 << le);
        rep(i, as) tmp[i] = a[i];
        iter(i, as, 1 << le) tmp[i] = o[i] = 0;
        ntt(tmp, 1 << le, 0);
        ntt(o, 1 << le, 0);
        rep(i, 1 << le) o[i] = 1ll * (MOD + (2 - 1ll * tmp[i] * o[i]) % MOD) * o[i] % MOD;
        ntt(o, 1 << le, 1);
        iter(i, as, 1 << le) o[i] = 0;
    }

    void init(int n) { constructroot(n); }
    void solve(int as, int *o)
    {
        static int H1i[524300], H[524300], dg[524300], H1[524300], tmp[524300];
        if (as == 1)
        {
            o[0] = 1;
            return;
        }
        solve((as + 1) / 2, o);
        rep(i, as) H1i[i] = o[i], tmp[i] = o[i];
        mult(H1i, as, tmp, as, H1i, false);
        rep(i, as) tmp[i] = o[i];
        tmp[0]++;
        if (tmp[0] >= MOD)
            tmp[0] -= MOD;
        mult(H1i, as, tmp, as, H1i, false);
        rep(i, as) dg[i] = 1ll * o[i + 1] * (i + 1) % MOD;
        cfn(H1i, as, H1);
        rep(i, as) tmp[i] = H1[i];
        mult(dg, as, tmp, as, H, false, true);
        for (int i = as - 2; ~i; i--)
            H[i + 1] = 1ll * H[i] * inv[i + 1] % MOD;
        H[0] = 0, H[1]--;
        if (H[1] < 0)
            H[1] += MOD;
        mult(H, as, H1i, as, tmp, false);
        rep(i, as)
        {
            o[i] -= tmp[i];
            if (o[i] < 0)
                o[i] += MOD;
        }
    }
}

int ncr(int n, int k)
{
    return 1ll * fac[n] * ifac[k] % MOD * ifac[n - k] % MOD;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    fac[0] = ifac[0] = fac[1] = ifac[1] = inv[0] = inv[1] = 1;
    int n = 2.5e5 + 10;
    iter(i, 2, n + 2)
    {
        inv[i] = 1ll * (MOD - MOD / i) * inv[MOD % i] % MOD;
        fac[i] = 1ll * fac[i - 1] * i % MOD;
        ifac[i] = 1ll * ifac[i - 1] * inv[i] % MOD;
    }
    int th = 1;
    while (th < (n << 1))
        th <<= 1;
    poly::init(th);
    cin >> n;
    poly::solve(n + 1, g);
    for (int i = n; ~i; i--)
        g[i + 1] = 1ll * g[i] * inv[i + 1] % MOD;
    rep(i, n + 1) h[i] = MOD - g[i];
    h[0] = 1;
    poly::cfn(h, n + 1, f);
    cout << 1ll * f[n] * fac[n] % MOD;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 8ms
memory: 110116kb

input:

3

output:

28

result:

ok 1 number(s): "28"

Test #2:

score: 0
Accepted
time: 7ms
memory: 110128kb

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 8ms
memory: 110132kb

input:

2

output:

4

result:

ok 1 number(s): "4"

Test #4:

score: 0
Accepted
time: 8ms
memory: 110144kb

input:

4

output:

282

result:

ok 1 number(s): "282"

Test #5:

score: 0
Accepted
time: 7ms
memory: 110060kb

input:

5

output:

3718

result:

ok 1 number(s): "3718"

Test #6:

score: 0
Accepted
time: 6ms
memory: 110120kb

input:

6

output:

60694

result:

ok 1 number(s): "60694"

Test #7:

score: 0
Accepted
time: 8ms
memory: 110212kb

input:

7

output:

1182522

result:

ok 1 number(s): "1182522"

Test #8:

score: 0
Accepted
time: 8ms
memory: 110060kb

input:

8

output:

26791738

result:

ok 1 number(s): "26791738"

Test #9:

score: 0
Accepted
time: 8ms
memory: 110060kb

input:

9

output:

692310518

result:

ok 1 number(s): "692310518"

Test #10:

score: 0
Accepted
time: 4ms
memory: 110080kb

input:

10

output:

135061370

result:

ok 1 number(s): "135061370"

Test #11:

score: 0
Accepted
time: 8ms
memory: 110148kb

input:

100

output:

423669705

result:

ok 1 number(s): "423669705"

Test #12:

score: 0
Accepted
time: 15ms
memory: 110192kb

input:

1234

output:

878522960

result:

ok 1 number(s): "878522960"

Test #13:

score: 0
Accepted
time: 113ms
memory: 114420kb

input:

54321

output:

827950477

result:

ok 1 number(s): "827950477"

Test #14:

score: 0
Accepted
time: 256ms
memory: 110056kb

input:

65536

output:

380835743

result:

ok 1 number(s): "380835743"

Test #15:

score: 0
Accepted
time: 545ms
memory: 114556kb

input:

131072

output:

842796122

result:

ok 1 number(s): "842796122"

Test #16:

score: 0
Accepted
time: 254ms
memory: 113360kb

input:

131071

output:

981021531

result:

ok 1 number(s): "981021531"

Test #17:

score: 0
Accepted
time: 253ms
memory: 110144kb

input:

131070

output:

480197639

result:

ok 1 number(s): "480197639"

Test #18:

score: 0
Accepted
time: 554ms
memory: 114496kb

input:

131074

output:

383000585

result:

ok 1 number(s): "383000585"

Test #19:

score: 0
Accepted
time: 543ms
memory: 114572kb

input:

131073

output:

316664839

result:

ok 1 number(s): "316664839"

Test #20:

score: 0
Accepted
time: 556ms
memory: 114176kb

input:

250000

output:

119658643

result:

ok 1 number(s): "119658643"

Test #21:

score: 0
Accepted
time: 549ms
memory: 114892kb

input:

249999

output:

78110138

result:

ok 1 number(s): "78110138"

Test #22:

score: 0
Accepted
time: 542ms
memory: 114920kb

input:

249998

output:

297253469

result:

ok 1 number(s): "297253469"

Extra Test:

score: 0
Extra Test Passed