QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#318528#8180. Bridge EliminationMax_s_xaMWA 141ms5964kbC++144.2kb2024-01-31 13:18:522024-01-31 13:18:52

Judging History

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

  • [2024-01-31 13:18:52]
  • 评测
  • 测评结果:WA
  • 用时:141ms
  • 内存:5964kb
  • [2024-01-31 13:18:52]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <cstring>

typedef long long ll;
typedef double lf;

// #define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

const int MAXN = 410, mod = 998244353;

int n, a[MAXN];

ll fac[MAXN], inv[MAXN];
inline ll C(int n, int m) {return fac[n] * inv[m] % mod * inv[n - m] % mod;}
inline ll Qpow(ll x, int y) {ll res = 1; while (y) (y & 1 && (res = res * x % mod)), x = x * x % mod, y >>= 1; return res;} 

ll f[MAXN][MAXN], g1[MAXN], g2[MAXN][MAXN], h[MAXN], ans;

int main()
{
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    Read(n);
    for (int i = 1; i <= n; i++) Read(a[i]);

    fac[0] = fac[1] = inv[0] = inv[1] = 1;
    for (int i = 2; i < MAXN; i++) fac[i] = fac[i - 1] * i % mod, inv[i] = (mod - mod / i) * inv[mod % i] % mod;
    for (int i = 2; i < MAXN; i++) inv[i] = inv[i] * inv[i - 1] % mod;

    h[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        h[i] = Qpow(2, i * (i - 1) / 2);
        for (int j = 1; j < i; j++)
            h[i] = (h[i] - h[j] * C(i - 1, j - 1) % mod * Qpow(2, (i - j) * (i - j - 1) / 2)) % mod;
    }
    for (int i = 1; i <= n; i++) g2[i][1] = h[i] * i;
    for (int i = 2; i <= n; i++)
        for (int j = 2; j <= i; j++)
            for (int k = 1; k < i; k++)
                g2[i][j] = (g2[i][j] + h[k] * g2[i - k][j - 1] % mod * C(i - 1, k - 1) % mod * k) % mod;
    g1[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        g1[i] = h[i];
        for (int j = 1; j < i; j++)
        {
            ll mul = j;
            for (int k = 1; k <= i - j; k++)
            g1[i] = (g1[i] - g1[j] * C(i - 1, j - 1) % mod * g2[i - j][k] % mod * mul) % mod, mul = mul * j % mod;
        }
    }
    memset(g2, 0, sizeof(g2));
    for (int i = 1; i <= n; i++) f[i][1] = g1[i] * i * i % mod, g2[i][1] = (g2[i - 1][1] + a[i]) % mod;
    for (int i = 2; i <= n; i++)
        for (int j = 2; j <= i; j++)
        {
            g2[i][j] = (g2[i - 1][j] + g2[i - 1][j - 1] * a[i]) % mod;
            for (int k = 1; k < i; k++)
                f[i][j] = (f[i][j] + f[i - k][j - 1] * C(i - 1, k - 1) % mod * g1[k] % mod * k * k) % mod;
        }
    f[n][1] = f[n][1] * inv[n] % mod * fac[n - 1] % mod;
    for (int i = 2; i <= n; i++) f[n][i] = f[n][i] * Qpow(n, i - 2) % mod;
    for (int i = 1; i <= n; i++) ans = (ans + f[n][i] * g2[n][i] % mod * inv[n] % mod * fac[i] % mod * fac[n - i]) % mod;
    cout << ans << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5048kb

input:

3
8 5 9

output:

1102

result:

ok "1102"

Test #2:

score: 0
Accepted
time: 0ms
memory: 4988kb

input:

5
4 2 1 3 10

output:

63860

result:

ok "63860"

Test #3:

score: 0
Accepted
time: 1ms
memory: 5064kb

input:

7
229520041 118275986 281963154 784360383 478705114 655222915 970715006

output:

35376232

result:

ok "35376232"

Test #4:

score: -100
Wrong Answer
time: 141ms
memory: 5964kb

input:

300
7 8 2 8 6 5 5 3 2 3 8 0 6 0 1 0 10 7 10 0 1 0 6 7 2 6 4 7 9 4 6 5 5 9 8 5 4 5 3 5 4 4 10 2 4 9 7 5 2 2 5 6 3 6 8 2 8 3 6 2 5 1 10 3 0 7 1 9 6 5 10 0 3 0 2 4 2 7 6 10 1 0 0 9 4 3 5 5 2 6 1 8 5 4 0 0 5 8 8 1 3 9 9 9 8 1 4 10 7 4 8 5 0 4 3 4 4 8 1 6 1 10 9 3 2 5 0 0 5 2 7 5 4 10 3 5 10 10 7 6 10 3 ...

output:

-353604349

result:

wrong answer 1st words differ - expected: '409590176', found: '-353604349'