QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#728340#9572. Bingo中国入民小学附属信息I队 (Zicheng Wang, Shubang Liu, Minkai Shao)#WA 3ms11928kbC++144.7kb2024-11-09 15:00:092024-11-09 15:00:17

Judging History

This is the latest submission verdict.

  • [2024-11-09 15:00:17]
  • Judged
  • Verdict: WA
  • Time: 3ms
  • Memory: 11928kb
  • [2024-11-09 15:00:09]
  • Submitted

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <random>
#include <ctime>
#include <chrono>
#include <numeric>
#include <iomanip>
#include <cassert>

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 = 2e5 + 10, mod = 998244353;

ll fac[MAXN], inv[MAXN], inv3;
inline ll C(int n, int m) { return m < 0 || n < m ? 0 : fac[n] * inv[m] % mod * inv[n - m] % mod; }
inline ll Qpow(ll x, ll y) { ll res = 1; while (y) (y & 1) && (res = res * x % mod), x = x * x % mod, y >>= 1; return res; }
inline ll add(ll x, ll y) { return (x += y) >= mod ? x - mod : x; }
inline ll sub(ll x, ll y) { return (x -= y) < 0 ? x + mod : x; }

int H, W;
int n, a[MAXN];

ll coef[MAXN];

const int MAXM = 6e5 + 10;
int len, rev[MAXM];
inline void preNTT(int N)
{
    len = 1;
    while (len <= N) len <<= 1;
    for (int i = 1; i < len; i++) rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? (len >> 1) : 0);
}
inline void NTT(int *f, bool tp)
{
    for (int i = 0; i < len; i++)
        if (rev[i] < i) swap(f[i], f[rev[i]]);
    for (int l = 1; l < len; l <<= 1)
    {
        ll w = Qpow((tp ? inv3 : 3), (mod - 1) / l / 2), p, x, y;
        for (int i = 0; i < len; i += (l << 1))
        {
            p = 1;
            for (int j = 0; j < l; j++)
            {
                x = f[i | j], y = f[i | j | l] * p % mod;
                f[i | j] = add(x, y), f[i | j | l] = sub(x, y);
                p = p * w % mod;
            }
        }
    }
    if (tp)
    {
        ll mul = Qpow(len, mod - 2);
        for (int i = 0; i < len; i++) f[i] = f[i] * mul % mod;
    }
}

int f[MAXM], g[MAXM];

int main()
{
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    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;
    inv3 = inv[3];
    for (int i = 2; i < MAXN; i++) inv[i] = inv[i - 1] * inv[i] % mod;
    int T;
    Read(T);
    while (T--)
    {
        Read(H), Read(W);
        n = H * W;
        for (int i = 1; i <= n; i++) Read(a[i]);
        sort(a + 1, a + n + 1);
        for (int i = 0; i <= n; i++) coef[i] = 0;
        for (int i = 0; i <= H; i++)
            for (int j = 0; j <= W; j++)
                if (i || j) (coef[i * W + j * H - i * j] += (i + j & 1 ? 1 : -1) * fac[(H - i) * (W - j)] * C(H, i) % mod * C(W, j)) %= mod;
        preNTT(n << 1);
        for (int i = 0; i < len; i++) f[i] = g[i] = 0;
        for (int i = 0; i <= n; i++) f[i] = coef[i], g[i] = inv[i];
        NTT(f, 0), NTT(g, 0);
        for (int i = 0; i < len; i++) f[i] = (ll)f[i] * g[i] % mod;
        NTT(f, 1);
        ll ans = fac[n] * a[n] % mod;
        for (int i = 1; i < n; i++) ans = (ans - f[i] * (a[i + 1] - a[i]) % mod * fac[i]) % mod;
        cout << (ans + mod) % mod << "\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 11928kb

input:

4
2 2
1 3 2 4
3 1
10 10 10
1 3
20 10 30
3 4
1 1 4 5 1 4 1 9 1 9 8 10

output:

56
60
60
855346687

result:

ok 4 number(s): "56 60 60 855346687"

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 11788kb

input:

1
2 2
0 0 998244352 998244352

output:

209715175

result:

wrong answer 1st numbers differ - expected: '998244345', found: '209715175'