QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#101407#5829. 马戏团里你最忙Scintilla100 ✓753ms90336kbC++145.5kb2023-04-29 16:01:062023-04-29 16:01:10

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-29 16:01:10]
  • 评测
  • 测评结果:100
  • 用时:753ms
  • 内存:90336kb
  • [2023-04-29 16:01:06]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define drep(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define pv(a) cout << #a << " = " << a << endl
#define pa(a, l, r) cout << #a " : "; rep(_, l, r) cout << a[_] << ' '; cout << endl

using vi = vector <int>;

const int P = 998244353;

const int N = 18;
const int M = 1 << N;
const int L = 80;

int read() {
  int x = 0, f = 1; char c = getchar();
  for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
  return x * f;
}

int inc(int a, int b) { return (a += b) >= P ? a - P : a; }
int dec(int a, int b) { return (a -= b) < 0 ? a + P : a; }
int mul(int a, int b) { return 1ll * a * b % P; }
void add(int &a, int b) { (a += b) >= P ? a -= P : 1; }
void sub(int &a, int b) { (a -= b) < 0 ? a += P : 1; }
int sgn(int x) { return x & 1 ? P - 1 : 1; }
int qpow(int a, int b) { int res = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a); return res; }

void fwt_or(int *f, int n, int o) {
  for (int k = 1; k < n; k <<= 1) {
    for (int i = 0; i < n; i += k << 1) {
      for (int j = 0; j < k; ++ j) {
        if (~o) add(f[i + j + k], f[i + j]);
        else sub(f[i + j + k], f[i + j]);
      }
    }
  }
}

void fwt_and(int *f, int n, int o) {
  for (int k = 1; k < n; k <<= 1) {
    for (int i = 0; i < n; i += k << 1) {
      for (int j = 0; j < k; ++ j) {
        if (~o) add(f[i + j], f[i + j + k]);
        else sub(f[i + j], f[i + j + k]);
      }
    }
  }
}

vi berlekamp_massey(vi a) {
  int n = a.size(), cnt = 0;
  // pa(a, 0, n - 1);
  vi delta(n), fail(n + 1);
  vector <vi> R(n + 1);
  for (int i = 0, t, p = 0; i < n; ++ i) {
    delta[i] = a[i];
    for (int j = 0; j < R[cnt].size(); ++ j) sub(delta[i], mul(R[cnt][j], a[i - j - 1]));
    if (!delta[i]) continue;
    fail[cnt] = i, ++ cnt;
    if (cnt == 1) { R[cnt].resize(i + 1); continue; }
    t = mul(delta[i], qpow(delta[fail[p]], P - 2));
    vi r0(i - fail[p] - 1, 0);
    r0.push_back(t);
    for (auto it : R[p]) r0.push_back(mul(it, P - t));
    R[cnt].resize(max(R[cnt - 1].size(), r0.size()));
    for (int j = 0; j < R[cnt - 1].size(); ++ j) add(R[cnt][j], R[cnt - 1][j]);
    for (int j = 0; j < r0.size(); ++ j) add(R[cnt][j], r0[j]);
    if ((int) R[cnt - 1].size() - fail[cnt - 1] < (int) R[p].size() - fail[p]) p = cnt - 1;
  }
  return R[cnt];
}

vi reduce(vi a) {
  while (a.size() && !a.back()) a.pop_back();
  return a;
}

vi operator + (vi a, vi b) {
  int n = a.size(), m = b.size();
  vi res(max(n, m));
  rep(i, 0, n - 1) add(res[i], a[i]);
  rep(i, 0, m - 1) add(res[i], b[i]);
  return res;
}

vi operator - (vi a, vi b) {
  int n = a.size(), m = b.size();
  vi res(max(n, m));
  rep(i, 0, n - 1) add(res[i], a[i]);
  rep(i, 0, m - 1) sub(res[i], b[i]);
  return res;
}

vi operator * (vi a, vi b) {
  int n = a.size(), m = b.size();
  vi res(n + m - 1);
  rep(i, 0, n - 1) rep(j, 0, m - 1) {
    add(res[i + j], mul(a[i], b[j]));
  }
  return res;
}

vi operator / (vi a, vi b) {
  int n = a.size(), m = b.size();
  if (n < m) return {};
  vi res(n - m + 1);
  drep(i, n - m, 0) {
    res[i] = mul(a[i + m - 1], qpow(b[m - 1], P - 2));
    rep(j, 0, m - 1) sub(a[i + j], mul(res[i], b[j]));
  }
  return res;
}

vi operator % (vi a, vi b) {
  return reduce(a - a / b * b);
}

vi work(vi coef, int b) {
  int n = coef.size();
  vi g(n + 1), res{1}, a{0, 1};
  g[n] = 1;
  rep(i, 0, n - 1) g[i] = dec(0, coef[n - 1 - i]);
  for (; b; b >>= 1, a = a * a % g) {
    if (b & 1) res = res * a % g;
  }
  return res;
}

int n, p, k, x, c[M], ipw[N], f[L][M], g[L][M], tf[M], tg[M], ans[M];

int main() {
  n = read(), p = read(), k = read(), x = read();
  for (int s = 0; s < 1 << n; ++ s) c[s] = read();
  rep(i, 0, n) ipw[i] = qpow(1 << i, P - 2);
  f[0][x] = 1;
  rep(i, 1, L - 1) {
    fwt_or(f[i - 1], 1 << n, 1);
    fwt_or(g[i - 1], 1 << n, 1);
    for (int s = 0; s < 1 << n; ++ s) {
      int t = ipw[n - __builtin_popcount(s)];
      tf[s] = mul(f[i - 1][s], t);
      tg[s] = mul(g[i - 1][s], t);
    }
    fwt_or(f[i - 1], 1 << n, -1);
    fwt_or(g[i - 1], 1 << n, -1);
    fwt_or(tf, 1 << n, -1);
    fwt_or(tg, 1 << n, -1);
    for (int s = 0; s < 1 << n; ++ s) {
      add(f[i][s], mul(tf[s], p));
      add(g[i][s], mul(tg[s], p));
    }
    fwt_and(f[i - 1], 1 << n, 1);
    fwt_and(g[i - 1], 1 << n, 1);
    for (int s = 0; s < 1 << n; ++ s) {
      int t = ipw[__builtin_popcount(s)];
      tf[s] = mul(f[i - 1][s], t);
      tg[s] = mul(g[i - 1][s], t);
    }
    fwt_and(f[i - 1], 1 << n, -1);
    fwt_and(g[i - 1], 1 << n, -1);
    fwt_and(tf, 1 << n, -1);
    fwt_and(tg, 1 << n, -1);
    for (int s = 0; s < 1 << n; ++ s) {
      add(f[i][s], mul(tf[s], dec(1, p)));
      add(g[i][s], mul(tg[s], dec(1, p)));
    }
    for (int s = 0; s < 1 << n; ++ s) {
      add(g[i][s], mul(f[i][s], c[s]));
    }
  }
  if (k < L) {
    for (int s = 0; s < 1 << n; ++ s) {
      printf("%d%c", g[k][s], " \n"[s + 1 == (1 << n)]);
    }
    return 0;
  }
  vi val(L);
  rep(i, 0, L - 1) val[i] = g[i][0];
  vi coef = berlekamp_massey(val);
  vi res = work(coef, k);
  for (int s = 0; s < 1 << n; ++ s) {
    for (int i = 0; i < res.size(); ++ i) {
      add(ans[s], mul(g[i][s], res[i]));
    }
    printf("%d%c", ans[s], " \n"[s + 1 == (1 << n)]);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 708ms
memory: 90088kb

input:

17 203635058 20 45197
630927925 367691872 854861811 935381440 972493717 952218952 40627765 901726383 333182690 814058886 114559515 857808755 180823985 448291128 904629758 587362495 553139819 808271094 537555635 876810522 460868754 965863351 816253267 677756972 587551773 48482825 12382358 781942390 2...

output:

196898572 306765528 537213886 127286930 159851023 63717087 706071741 435797240 327471764 154687895 775133161 446884277 355140499 730759144 828313812 358804201 563368151 879077306 777389211 488119012 23523875 414748489 31031626 712027375 508390555 892195537 272674444 65777736 725465806 703671740 8558...

result:

ok 131072 numbers

Test #2:

score: 10
Accepted
time: 687ms
memory: 89804kb

input:

17 55303977 20 32265
494363203 155538370 630883878 512423276 148363202 832624635 801640865 903488980 811127763 249184672 605607782 287932872 764562597 387167720 508436523 263243837 25444512 71537379 602424523 577977303 207298251 640159465 917833936 352635217 315477193 937365223 740930608 774925120 2...

output:

289704320 958498566 842657284 940168022 997881125 862052632 724273322 231206605 305893297 43278691 837297755 633518092 730177995 204277846 437293037 502075979 382748699 327548917 26671944 687820882 479587172 348577818 6544646 912745783 388632361 225651778 270406177 724852774 753829405 290426213 3659...

result:

ok 131072 numbers

Test #3:

score: 10
Accepted
time: 753ms
memory: 90248kb

input:

17 126833065 1000 123430
911917407 121906013 929903588 323429234 139121451 3203442 65035849 409625525 525882288 532577435 971826327 904691827 481756384 47231489 346006246 274441242 584793873 841520735 543444701 644939661 885202827 142653644 180313583 548253835 847010044 216554243 124989074 619408231...

output:

134631343 480699513 849862453 247146759 699748130 715338161 355151478 292751752 86181381 407771588 827284655 803659756 69403105 527593812 231299485 275012392 926452282 230048926 248932928 562168554 952865833 726491930 824516102 652955122 618228742 103751577 43309223 223455183 332063966 84250610 4567...

result:

ok 131072 numbers

Test #4:

score: 10
Accepted
time: 741ms
memory: 90232kb

input:

17 621627241 1000 37306
874709248 739473737 912060934 158833941 971188630 744039312 327912594 702720054 185196283 745584702 903434215 484148979 176610819 222341600 961711994 877263458 352785155 12121832 920590333 736755672 428869475 220835314 277186463 787049344 504919397 134827356 59724693 78120026...

output:

287767112 405924924 863936907 813048216 488495341 760777896 218672231 613011508 100939729 350001545 294832115 941360641 416789535 876909340 200852660 16856124 923319874 175774574 460372770 720910090 806672046 723152383 1499791 143768065 495566120 25220523 157220954 572061879 911190946 581269547 9547...

result:

ok 131072 numbers

Test #5:

score: 10
Accepted
time: 0ms
memory: 8260kb

input:

1 836026557 1000000000 1
89073685 44729188

output:

936822371 494640537

result:

ok 2 number(s): "936822371 494640537"

Test #6:

score: 10
Accepted
time: 2ms
memory: 8300kb

input:

8 729700268 1000000000 44
415083631 759858310 528846926 698375322 811429471 668487195 443858087 600445292 190489246 123673503 530450450 76731586 197623863 866555166 743879064 141858132 642913199 103864508 528442021 931490458 969629325 724240247 392146627 32067149 882745389 980834475 140464044 516308...

output:

830023741 616121060 660911628 638220986 252695004 652606403 123917422 418209485 845655441 750569346 32907787 264930076 953962978 689996424 323420989 513057450 197453167 221143412 79717545 924653624 119845062 512678835 395988191 629025773 355693367 391006251 472355315 383277265 392809151 920109119 59...

result:

ok 256 numbers

Test #7:

score: 10
Accepted
time: 751ms
memory: 90336kb

input:

17 499122177 1000000000 101368
879449681 64374006 2136324 179144921 432405367 443552363 403139437 39083535 353339709 78321779 538844553 158621708 576996457 284607049 353163162 754891442 765538773 579712088 200974213 4059721 644110071 271278034 94792222 726122290 905812908 137101325 873585214 5466611...

output:

76712311 736727498 966914483 448805348 750114858 28897485 753461342 683730774 284755505 348793098 822598660 28004678 14410200 429526723 364387489 948405933 384557716 821776284 767121187 341389443 512949890 25590295 915209327 878995476 76329751 297767711 690108093 378878117 383202047 800122506 251561...

result:

ok 131072 numbers

Test #8:

score: 10
Accepted
time: 713ms
memory: 90284kb

input:

17 73054278 1000000000 103328
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

307343988 473326156 473326156 763878935 473326156 763878935 763878935 46738335 473326156 763878935 763878935 46738335 763878935 46738335 46738335 438303753 473326156 763878935 763878935 46738335 763878935 46738335 46738335 438303753 763878935 46738335 46738335 438303753 46738335 438303753 438303753 ...

result:

ok 131072 numbers

Test #9:

score: 10
Accepted
time: 377ms
memory: 49328kb

input:

16 544826057 1000000000 58181
127333103 438073137 374751329 498443816 423416409 925764567 46036736 110186961 233535448 830000756 676728537 294866428 430284448 424338238 337810871 397978 916371232 246055490 114266106 174763813 816533383 346479346 358334243 10507119 464920585 275232506 870081558 38720...

output:

770450440 38771269 201293058 433444649 474897116 299183217 522132059 603152811 116039172 451175434 911427230 814731843 644404641 621015244 908568610 412934671 829476216 473110428 37797136 221722199 711492820 360181295 381795593 401481063 639255822 4170678 924263451 31028063 83884670 635084912 898232...

result:

ok 65536 numbers

Test #10:

score: 10
Accepted
time: 750ms
memory: 90272kb

input:

17 596529770 1000000000 121733
359584182 80170529 721719197 271156012 552773637 882685657 619483754 39827609 20656366 82444398 478178854 625533821 736267120 37211948 503896746 770716690 554769769 575127167 638367404 541442475 219866908 428643659 649491821 417360007 540021485 514124438 754101213 3068...

output:

421614534 116554773 723738982 42159276 719258896 170212004 364550169 234235698 305553646 637748331 28143263 403791908 155115061 556160305 147943692 21727045 115873314 367011359 831485941 264863840 554693188 873995114 441755686 503586113 897215621 929133656 495767228 476016113 783433620 966017915 835...

result:

ok 131072 numbers

Extra Test:

score: 0
Extra Test Passed