QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#733567 | #9572. Bingo | vwxyz | AC ✓ | 403ms | 30048kb | C++23 | 14.4kb | 2024-11-10 19:52:25 | 2024-11-10 19:52:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#define FOR(i, a, b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,N) for(int i=0;i<(int)(N);i++)
#define rep1(i,N) for(int i=1;i<=(int)(N);i++)
#define fs first
#define sc second
#define eb emplace_back
#define pb eb
#define all(x) x.begin(),x.end()
template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; }
template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; }
#ifdef LOCAL
#define show(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
return os << "P(" << p.first << ", " << p.second << ")";
}
template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
os << "[";
for (auto d : v) os << d << ", ";
return os << "]";
}
// cin.tie(nullptr);
// ios::sync_with_stdio(false);
// cout << fixed << setprecision(20);
template <uint MD> struct ModInt {
using M = ModInt;
const static M G;
uint v;
ModInt(ll _v = 0) { set_v(_v % MD + MD); }
M& set_v(uint _v) {
v = (_v < MD) ? _v : _v - MD;
return *this;
}
explicit operator bool() const { return v != 0; }
M operator-() const { return M() - *this; }
M operator+(const M& r) const { return M().set_v(v + r.v); }
M operator-(const M& r) const { return M().set_v(v + MD - r.v); }
M operator*(const M& r) const { return M().set_v(ull(v) * r.v % MD); }
M operator/(const M& r) const { return *this * r.inv(); }
M& operator+=(const M& r) { return *this = *this + r; }
M& operator-=(const M& r) { return *this = *this - r; }
M& operator*=(const M& r) { return *this = *this * r; }
M& operator/=(const M& r) { return *this = *this / r; }
bool operator==(const M& r) const { return v == r.v; }
M pow(ll n) const {
M x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
M inv() const { return pow(MD - 2); }
friend ostream& operator<<(ostream& os, const M& r) { return os << r.v;}
};
template<>
const ModInt<998244353> ModInt<998244353>::G = 3; // 適切な原始根を指定します
using Mint = ModInt<998244353>;
ll modpow(ll base, ll exp, ll mod) {
ll res = 1;
while (exp > 0) {
if (exp % 2 == 1) res = res * base % mod;
base = base * base % mod;
exp /= 2;
}
return res;
}
//x^2=N(mod p)となるxを返す(存在しないなら-1)
//modpow(a,b,p)はa^b(mod p)
ll Tonelli_Shanks(ll N, ll p) {
N%=p;
if(p==2){
return N;
}
if (modpow(N, p >> 1, p) == p - 1) {
return -1;
} else if (p % 4 == 3) {
return modpow(N, (p + 1) / 4, p);
} else {
ll n = 1;
for (; n < p; n++) {
if (modpow(n, p >> 1, p) == p - 1) {
break;
}
}
ll pp = p - 1;
int c = 0;
while (pp % 2 == 0) {
pp /= 2;
c++;
}
ll s = modpow(N, pp, p);
ll r = modpow(N, (pp + 1) / 2, p);
for (int i = c - 2; i >= 0; --i) {
if (modpow(s, 1LL << i, p) == p - 1) {
s = s * modpow(n, p >> (1 + i), p) % p;
r = r * modpow(n, p >> (2 + i), p) % p;
}
}
return r;
}
}
void nft(bool type, V<Mint>& a) {
int n = int(a.size()), s = 0;
while ((1 << s) < n) s++;
assert(1 << s == n);
static V<Mint> ep, iep;
while (int(ep.size()) <= s) {
ep.push_back(Mint::G.pow(Mint(-1).v / (1 << ep.size())));
iep.push_back(ep.back().inv());
}
V<Mint> b(n);
for (int i = 1; i <= s; i++) {
int w = 1 << (s - i);
Mint base = type ? iep[i] : ep[i], now = 1;
for (int y = 0; y < n / 2; y += w) {
for (int x = 0; x < w; x++) {
auto l = a[y << 1 | x];
auto r = now * a[y << 1 | x | w];
b[y | x] = l + r;
b[y | x | n >> 1] = l - r;
}
now *= base;
}
swap(a, b);
}
}
template <class Mint>
V<Mint> multiply(const V<Mint>& a, const V<Mint>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
if (min(n, m) <= 8) {
V<Mint> ans(n + m - 1);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) ans[i + j] += a[i] * b[j];
return ans;
}
int lg = 0;
while ((1 << lg) < n + m - 1) lg++;
int z = 1 << lg;
auto a2 = a, b2 = b;
a2.resize(z);
b2.resize(z);
nft(false, a2);
nft(false, b2);
for (int i = 0; i < z; i++) a2[i] *= b2[i];
nft(true, a2);
a2.resize(n + m - 1);
Mint iz = Mint(z).inv();
for (int i = 0; i < n + m - 1; i++) a2[i] *= iz;
return a2;
}
template <class D> struct Poly {
vector<D> v;
Poly(const vector<D>& _v = {}) : v(_v) { shrink(); }
void shrink() {
while (v.size() && !v.back()) v.pop_back();
}
int size() const { return int(v.size()); }
D freq(int p) const { return (p < size()) ? v[p] : D(0); }
Poly operator+(const Poly& r) const {
auto n = max(size(), r.size());
vector<D> res(n);
for (int i = 0; i < n; i++) res[i] = freq(i) + r.freq(i);
return res;
}
Poly operator-(const Poly& r) const {
int n = max(size(), r.size());
vector<D> res(n);
for (int i = 0; i < n; i++) res[i] = freq(i) - r.freq(i);
return res;
}
Poly operator*(const Poly& r) const { return {multiply(v, r.v)}; }
Poly operator*(const D& r) const {
int n = size();
vector<D> res(n);
for (int i = 0; i < n; i++) res[i] = v[i] * r;
return res;
}
Poly operator/(const D &r) const{
return *this * r.inv();
}
Poly operator/(const Poly& r) const {
if (size() < r.size()) return {{}};
int n = size() - r.size() + 1;
return (rev().pre(n) * r.rev().inv(n)).rev(n); //変更
}
Poly operator%(const Poly& r) const { return *this - *this / r * r; }
Poly operator<<(int s) const {
vector<D> res(size() + s);
for (int i = 0; i < size(); i++) res[i + s] = v[i];
return res;
}
Poly operator>>(int s) const {
if (size() <= s) return Poly();
vector<D> res(size() - s);
for (int i = 0; i < size() - s; i++) res[i] = v[i + s];
return res;
}
Poly& operator+=(const Poly& r) { return *this = *this + r; }
Poly& operator-=(const Poly& r) { return *this = *this - r; }
Poly& operator*=(const Poly& r) { return *this = *this * r; }
Poly& operator*=(const D& r) { return *this = *this * r; }
Poly& operator/=(const Poly& r) { return *this = *this / r; }
Poly& operator/=(const D &r) {return *this = *this/r;}
Poly& operator%=(const Poly& r) { return *this = *this % r; }
Poly& operator<<=(const size_t& n) { return *this = *this << n; }
Poly& operator>>=(const size_t& n) { return *this = *this >> n; }
Poly pre(int le) const {
return {{v.begin(), v.begin() + min(size(), le)}};
}
Poly rev(int n = -1) const {
vector<D> res = v;
if (n != -1) res.resize(n);
reverse(res.begin(), res.end());
return res;
}
Poly diff() const {
vector<D> res(max(0, size() - 1));
for (int i = 1; i < size(); i++) res[i - 1] = freq(i) * i;
return res;
}
Poly inte() const {
vector<D> res(size() + 1);
for (int i = 0; i < size(); i++) res[i + 1] = freq(i) / (i + 1);
return res;
}
// f * f.inv() = 1 + g(x)x^m
Poly inv(int m) const {
Poly res = Poly({D(1) / freq(0)});
for (int i = 1; i < m; i *= 2) {
res = (res * D(2) - res * res * pre(2 * i)).pre(2 * i);
}
return res.pre(m);
}
Poly exp(int n) const {
assert(freq(0) == 0);
Poly f({1}), g({1});
for (int i = 1; i < n; i *= 2) {
g = (g * 2 - f * g * g).pre(i);
Poly q = diff().pre(i - 1);
Poly w = (q + g * (f.diff() - f * q)).pre(2 * i - 1);
f = (f + f * (*this - w.inte()).pre(2 * i)).pre(2 * i);
}
return f.pre(n);
}
Poly log(int n) const {
assert(freq(0) == 1);
auto f = pre(n);
return (f.diff() * f.inv(n - 1)).pre(n - 1).inte();
}
Poly sqrt(int n) const {
assert(freq(0) == 1);
Poly f = pre(n + 1);
Poly g({1});
for (int i = 1; i < n; i *= 2) {
g = (g + f.pre(2 * i) * g.inv(2 * i)) / 2;
}
return g.pre(n + 1);
}
//定数項が1である必要はない
pair<bool,Poly> sqrt_arb(int n) const{
if(size()==0){
return {true,Poly()};
}
int c=0;
while(c*2<size() && !freq(c*2)){
if(freq(c*2+1)){
return {false,Poly()};
}
c+=1;
}
//modが変わったら修正する
Mint x=Tonelli_Shanks((ll)freq(c*2).v,998244353ll);
if(x==-1){
return {false,Poly()};
}
if(n<=c){
return {true,Poly()};
}
Poly P=(*this)>>c*2;
P/=x*x;
P=P.sqrt(n-c);
P<<=c;
P*=x;
return {true,P};
}
//
Poly power(ll k,int n){
if(!k){
return Poly({D(1)});
}
if(!size()){
return Poly();
}
int c=0;
while(c<size()&&!freq(c)){
c+=1;
}
if(c>(n-1)/k){
return Poly();
}
Mint ic=freq(c),pc=freq(c);
ic=ic.inv();
pc=pc.pow(k);
int l=n-c*k;
return (((((*this).pre(l+c)*ic>>c).log(l)*k).exp(l)*pc)<<c*k).pre(n);
}
//N項目までなら%modを.pre(N)に書き換えた方が速い(けど遅い)
Poly pow_mod(ll n, const Poly& mod) {
Poly x = *this, r = {{1}};
while (n) {
if (n & 1) r = r * x % mod;
x = x * x % mod;
n >>= 1;
}
return r;
}
friend ostream& operator<<(ostream& os, const Poly& p) {
if (p.size() == 0) return os << "0";
for (auto i = 0; i < p.size(); i++) {
if (p.v[i]) {
os << p.v[i] << "x^" << i;
if (i != p.size() - 1) os << "+";
}
}
return os;
}
};
template <class Mint> struct MultiEval {
using NP = MultiEval*;
NP l, r;
vector<Mint> que;
int sz;
Poly<Mint> mul;
MultiEval(const vector<Mint>& _que, int off, int _sz) : sz(_sz) {
if (sz <= 100) {
que = {_que.begin() + off, _que.begin() + off + sz};
mul = {{1}};
for (auto x : que) mul *= {{-x, 1}};
return;
}
l = new MultiEval(_que, off, sz / 2);
r = new MultiEval(_que, off + sz / 2, sz - sz / 2);
mul = l->mul * r->mul;
}
MultiEval(const vector<Mint>& _que) : MultiEval(_que, 0, int(_que.size())) {}
void query(const Poly<Mint>& _pol, vector<Mint>& res) const {
if (sz <= 100) {
for (auto x : que) {
Mint sm = 0, base = 1;
for (int i = 0; i < _pol.size(); i++) {
sm += base * _pol.freq(i);
base *= x;
}
res.push_back(sm);
}
return;
}
auto pol = _pol % mul;
l->query(pol, res);
r->query(pol, res);
}
vector<Mint> query(const Poly<Mint>& pol) const {
vector<Mint> res;
query(pol, res);
return res;
}
};
//rev()を取って-1倍すると、1+...の形で線形漸化式の分母になる
template <class Mint> Poly<Mint> berlekamp_massey(const vector<Mint>& s) {
int n = int(s.size());
vector<Mint> b = {Mint(-1)}, c = {Mint(-1)};
Mint y = Mint(1);
for (int ed = 1; ed <= n; ed++) {
int l = int(c.size()), m = int(b.size());
Mint x = 0;
for (int i = 0; i < l; i++) {
x += c[i] * s[ed - l + i];
}
b.push_back(0);
m++;
if (!x) continue;
Mint freq = x / y;
if (l < m) {
// use b
auto tmp = c;
c.insert(begin(c), m - l, Mint(0));
for (int i = 0; i < m; i++) {
c[m - 1 - i] -= freq * b[m - 1 - i];
}
b = tmp;
y = x;
} else {
// use c
for (int i = 0; i < m; i++) {
c[l - 1 - i] -= freq * b[m - 1 - i];
}
}
}
return c;
}
// n/dのx^Nの係数
template <class D>
D Bostan_Mori(Poly<D> n, Poly<D> d, ll N) {
while (N) {
Poly<D> dd=Poly(d.v);
for(int i=1;i<dd.size();i+=2){
dd.v[i]=-dd.v[i];
}
n*=dd;
if(N%2) n>>=1;
for(int i=0;i<n.size();i+=2){
n.v[i/2]=n.v[i];
}
n=n.pre((n.size()+1)/2);
d*=dd;
for(int i=0;i<d.size();i+=2){
d.v[i/2]=d.v[i];
}
d=d.pre((d.size()+1)/2);
N>>=1;
}
return n.size() ? n.freq(0) : D(0);
}
template<class D>
D BMBM(vector<D> A,ll N){
Poly<D> d=berlekamp_massey(A).rev()*(-1);
Poly<D> n=(d*Poly(A)).pre(d.size()-1);
return Bostan_Mori(n,d,N);
}
const ll mod=998244353;
const int K = 2 * 100000;
vector<Mint> fact(K + 1), fact_inve(K + 1);
void precompute() {
fact[0] = Mint(1);
for (int k = 1; k <= K; ++k) {
fact[k] = fact[k - 1] * k;
}
for (int k = 0; k <= K; ++k) {
fact_inve[k] = Mint(modpow(fact[k].v, mod - 2, mod));
}
}
Mint comb(int a, int b) {
if (a < b || b < 0) return 0;
return fact[a] * fact_inve[b] * fact_inve[a - b];
}
int main() {
precompute();
int T;
cin >> T;
while (T--) {
int N, M;
cin >> N >> M;
vector<int> A(N * M);
for (int i = 0; i < N * M; ++i) {
cin >> A[i];
}
map<int, int> freq;
for (int a : A) freq[a]++;
vector<int> C;
vector<Mint> D;
for (const auto& [a, c] : freq) {
D.push_back(Mint(a));
C.push_back(c);
}
int le = D.size();
for (int i = le - 1; i > 0; --i) {
D[i] -= D[i - 1];
}
for (int k = le - 2; k >= 0; --k) {
C[k] += C[k + 1];
}
for (int k = 0; k < le; ++k) {
D[k] = D[k] * fact[N * M - C[k]];
}
vector<Mint> P(N * M + 1, 0);
for (int n = 0; n <= N; ++n) {
for (int m = 0; m <= M; ++m) {
int nm = n * m;
P[nm] = P[nm] + comb(N, n) * comb(M, m) * modpow(-1, N + M - n - m, mod) * fact[nm];
}
}
vector<Mint> P0(N * M + 1), P1(N * M + 1, 0);
for (int x = 0; x <= N * M; ++x) {
P0[x] = fact_inve[x];
}
for (int k = 0; k < le; ++k) {
P1[C[k]] = P1[C[k]] + D[k];
}
Poly<Mint> poly0(P0),poly1(P1);
Poly<Mint> poly01 = poly0*poly1;
Mint ans = 0;
for (int nm = 0; nm <= N * M; ++nm) {
ans = ans + P[nm] * poly01.freq(nm);
}
cout << ans << '\n';
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 24ms
memory: 4620kb
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: 19ms
memory: 4604kb
input:
1 2 2 0 0 998244352 998244352
output:
998244345
result:
ok 1 number(s): "998244345"
Test #3:
score: 0
Accepted
time: 141ms
memory: 5172kb
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 161768904 239961447 329781933 315752584 578075668 259941994 600147169 402221164 890998500 154285994 181862417 47930994 273729619 64...
result:
ok 900 numbers
Test #4:
score: 0
Accepted
time: 210ms
memory: 4584kb
input:
400 1 995 548100968 635656907 177366910 971271357 314579375 529572241 948721678 455918644 95745688 164857981 499083775 827896554 496889261 111294651 646048809 758286431 163045934 917399362 189372614 267754648 966443706 921589740 228089960 473153545 482816423 37567957 495730380 864445591 568695110 78...
output:
954668084 677509135 636173666 415373646 477286237 209886549 398423120 24466622 672440292 390142124 498517438 305197486 239833057 500821845 475519894 347179487 974036742 810602822 75196204 48378743 393961176 290898056 957916898 434124418 663457674 225283495 704304053 338701802 670053839 137083082 165...
result:
ok 400 numbers
Test #5:
score: 0
Accepted
time: 250ms
memory: 6156kb
input:
40 92 99 14480275 12892621 932457558 584861415 926346518 101583802 498448003 884757899 463949215 661256632 872663851 651132350 565253214 18404795 810166895 145370572 123351313 298382303 777283720 775900024 613503856 817112784 713304484 541301622 595768594 550989875 960159831 571815058 777864097 3608...
output:
614712898 16225927 313765200 824491114 60971514 769510634 58341639 808667102 527187053 319496150 267177120 409701892 245708713 115397703 928197397 533118123 931076329 448328887 672878477 180728812 385639338 504093069 846218180 981546177 906805965 315620628 863877552 348963788 781585156 982673320 275...
result:
ok 40 numbers
Test #6:
score: 0
Accepted
time: 222ms
memory: 6088kb
input:
40 86 92 479103936 690362573 387313968 428679987 770097821 67859949 744428797 919332570 530162857 389639443 851979342 310332074 863845681 155743453 442066584 996725021 385646576 447381083 64960590 818019444 260564007 16381359 36238584 609449698 12466296 532193395 262308857 279184524 454814687 400578...
output:
147127348 995441625 947321329 200561175 846810174 626764591 235790337 30932003 384829067 254218916 20342301 451884441 634808121 241161955 246093492 515701050 978130791 502129313 3431507 775910032 464454612 153447682 53092548 316439192 101505498 40191013 225025922 133184210 209384134 330521977 360716...
result:
ok 40 numbers
Test #7:
score: 0
Accepted
time: 352ms
memory: 29948kb
input:
2 447 447 790583748 764745604 779691526 67598288 308196334 738524513 685610494 336125979 294155123 651917356 898366384 420012139 529304950 133567869 630219750 62853597 606184670 383809162 43962071 826608376 652871696 860138865 675639996 444122802 823442992 841633845 125418467 211407031 726738308 984...
output:
506347658 891054810
result:
ok 2 number(s): "506347658 891054810"
Test #8:
score: 0
Accepted
time: 360ms
memory: 29924kb
input:
2 100 2000 414412015 610256524 548060717 581032168 761297097 50124687 831351681 906381893 842125801 82512258 734351512 844649420 370666628 791011946 232557748 968208094 238084359 933173727 306257334 509581201 774756848 370039888 322700146 641635730 8423279 909781921 238370638 28574480 725141803 9941...
output:
380238486 147107381
result:
ok 2 number(s): "380238486 147107381"
Test #9:
score: 0
Accepted
time: 348ms
memory: 29148kb
input:
2 2000 100 432504867 225538929 546658423 260257767 682179463 892187612 142884587 872658039 89862243 117086929 104310686 342803717 47992235 148221787 695186286 875318238 264248632 320257869 568552597 54600213 364423602 412159309 666014765 235168890 795627687 977929998 351322809 9778000 723545298 1693...
output:
807761546 460321345
result:
ok 2 number(s): "807761546 460321345"
Test #10:
score: 0
Accepted
time: 377ms
memory: 29992kb
input:
2 10 20000 450597719 675029617 315027614 635737834 439025757 505777670 590615658 142679716 637832921 847916068 472514213 71186529 723562195 273447466 297524284 782428382 428366719 869622434 528857976 735817391 650344824 152288845 779100871 130691934 584587742 513859676 996493379 687235989 189730396 ...
output:
579362183 459093435
result:
ok 2 number(s): "579362183 459093435"
Test #11:
score: 0
Accepted
time: 372ms
memory: 29140kb
input:
2 20000 10 770680455 822530420 615615204 314963433 892126521 815622197 900392916 410945746 187559247 278510970 538727855 101559225 98897919 326911775 760152822 689538526 60266407 256706575 791153240 418790216 772229976 194408266 426161021 328204862 71557913 976272337 111201197 504403438 188133891 58...
output:
30164141 385139412
result:
ok 2 number(s): "30164141 385139412"
Test #12:
score: 0
Accepted
time: 391ms
memory: 30048kb
input:
2 100000 2 224212357 458006968 163025536 269449920 699657932 932776912 420937536 166351734 685658904 344666962 946460500 884461444 228370491 174980092 646368291 854381092 617669653 366836379 717071379 463349902 749408189 163205331 665200568 666647060 230069001 195048922 357469436 37819596 212080713 ...
output:
188269415 372357321
result:
ok 2 number(s): "188269415 372357321"
Test #13:
score: 0
Accepted
time: 386ms
memory: 29936kb
input:
2 2 100000 242305209 73289374 463613125 946919872 154514343 546366969 34460325 132627880 629649815 379241632 14429790 612844256 207685982 530434285 412742360 761491236 249569341 450174989 677376758 146322726 339074943 507314636 10270834 864159988 715283525 729222953 772411491 19023116 374520280 8624...
output:
178386797 319825470
result:
ok 2 number(s): "178386797 319825470"
Test #14:
score: 0
Accepted
time: 403ms
memory: 29920kb
input:
2 1 200000 562387945 522780061 928236786 626145471 377386592 856211496 180201513 702883794 179376140 808080887 382633317 110998553 883255942 655659964 711334827 668601380 413687428 303285085 939672021 525550020 460960094 549434056 957565221 759683032 202253696 797371030 885363662 532445034 674913659...
output:
499141558 710898596
result:
ok 2 number(s): "499141558 710898596"
Extra Test:
score: 0
Extra Test Passed