QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#728399 | #9572. Bingo | 中国入民小学附属信息I队 (Zicheng Wang, Shubang Liu, Minkai Shao)# | WA | 60ms | 13928kb | C++14 | 4.7kb | 2024-11-09 15:06:07 | 2024-11-09 15:06:09 |
Judging History
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 - (ll)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: 4ms
memory: 13832kb
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: 0
Accepted
time: 0ms
memory: 13928kb
input:
1 2 2 0 0 998244352 998244352
output:
998244345
result:
ok 1 number(s): "998244345"
Test #3:
score: -100
Wrong Answer
time: 60ms
memory: 13916kb
input:
900 1 1 810487041 1 2 569006976 247513378 1 3 424212910 256484544 661426830 1 4 701056586 563296095 702740883 723333858 1 5 725786515 738465053 821758167 170452477 34260723 1 6 204184507 619884535 208921865 898995024 768400582 369477346 1 7 225635227 321139203 724076812 439129905 405005469 369864252...
output:
810487041 495026756 540662911 541929691 118309348 270925149 575366228 709974238 761347712 304011276 14811741 366145628 638305530 240546928 484276475 603344008 926633861 428047653 556227142 85086171 315752584 578075668 259941994 276250911 402221164 890998500 387256806 795479144 466710273 342809767 64...
result:
wrong answer 18th numbers differ - expected: '161768904', found: '428047653'