QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449309#7838. 往日之影Made_in_Code10 75ms42756kbC++143.6kb2024-06-20 22:02:152024-06-20 22:02:15

Judging History

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

  • [2024-06-20 22:02:15]
  • 评测
  • 测评结果:10
  • 用时:75ms
  • 内存:42756kb
  • [2024-06-20 22:02:15]
  • 提交

answer

#include <iostream>
#define LL long long

using namespace std;

const int kMaxN = 1e6 + 1, kMaxL = 20;
struct H {
  int pw;
  LL fact, _fact;
} h[kMaxN];
int t, p, n, a[4], d[4][4];

void Add(LL &x, LL y) { x = (x + y) % p; }

LL Pow(LL x, LL y) {
  LL ans = 1;
  for (LL i = 1; i <= y; i <<= 1) {
    if (i & y) {
      ans = ans * x % p;
    }
    x = x * x % p;
  }
  return ans;
}

class P {
 public:
  LL r, i;

  P() { r = i = 0; }
  P(LL _r, LL _i) { r = _r, i = _i; }

  P operator+(P x) {
    return {(r + x.r) % p, (i + x.i) % p};
  }

  P operator<<(LL x) {
    return x & 1 ? P(i, r) : P(r, i);
  }

  P operator*(LL x) {
    return {r * x % p, i * x % p};
  }

  P operator*(P x) {
    return {(r * x.r - i * x.i) % p, (r * x.i + i * x.r) % p};
  }

  P operator^(LL y) {
    P x = *this, ans(1, 0);
    for (LL i = 1; i <= y; i <<= 1) {
      if (i & y) {
        ans = ans * x;
      }
      x = x * x;
    }
    return ans;
  }
} ans;

void CalcFact() {
  int _r;
  LL pw[kMaxL], s[kMaxN], t[kMaxN];
  pw[0] = 1;
  for (int &i = _r = 1; i < kMaxL; i++) {
    pw[i] = pw[i - 1] * p;
    if (pw[i] >= kMaxN) {
      break;
    }
  }
  h[0] = {0, 1, 1};
  for (int i = 1; i < kMaxN; i++) {
    int l = 0, r = _r;
    while (l <= r) {
      int mid = l + r >> 1;
      if (mid <= _r && !(i % pw[mid])) {
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    h[i] = h[i - 1], h[i].pw += r;
    h[i].fact = h[i].fact * (i / pw[r]) % p;
  }
  s[0] = 1;
  for (int i = 1; i < kMaxN; i++) {
    s[i] = s[i - 1] * h[i].fact % p;
  }
  t[kMaxN - 1] = Pow(s[kMaxN - 1], p - 2);
  for (int i = kMaxN - 1; i > 0; i--) {
    t[i - 1] = t[i] * h[i].fact % p;
  }
  for (int i = 1; i < kMaxN; i++) {
    h[i]._fact = s[i - 1] * t[i] % p;
  }
}

void CalcAns(int o) {
  LL b[4] = {};
  P w(o, 0);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      b[j] += d[i][j];
    }
  }
  for (int i = 0; i < 4; i++) {
    w.r = w.r * h[a[i]].fact % p;
    for (int j = 0; j < 4; j++) {
      w.r = w.r * h[d[i][j]]._fact % p;
    }
  }
  int q = (d[1][3] + d[3][1]) + 3 * (d[1][1] + d[3][3]) & 3;
  q = q + 2 * (d[1][2] + d[2][1] + d[2][3] + d[3][2]) & 3;
  w = w << q;
  for (int i = 0; i < 4; i++) {
    for (int j = i + 1; j < 4; j++) {
      P p(1, 0);
      if (i + j & 1) {
        p.i += (i + j & 3) == 1 ? 1 : -1;
      } else {
        p.r += (i + j & 3) == 0 ? 1 : -1;
      }
      w = w * (p ^ b[i] * b[j]);
    }
  }
  for (int i = 0; i < 4; i++) {
    P p(1, 0);
    p.r += (i + i & 3) == 0 ? 1 : -1;
    w = w * (p ^ b[i] * (b[i] - 1) / 2);
  }
  ans = ans + w;
}

void S(int x, int b0, int b1, int b2, int b3, int o) {
  if (x == 4) {
    CalcAns(o);
    return;
  }
  for (int &i = d[x][0] = 0; i <= b0 && i <= a[x]; i++) {
    for (int &j = d[x][1] = 0; j <= b1 && i + j <= a[x]; j++) {
      for (int &k = d[x][2] = 0; k <= b2 && i + j + k <= a[x]; k++) {
        int &l = d[x][3] = a[x] - i - j - k;
        if (l <= b3 && h[i].pw + h[j].pw + h[k].pw + h[l].pw == h[a[x]].pw) {
          S(x + 1, b0 - i, b1 - j, b2 - k, b3 - l, o);
        }
      }
    }
  }
}

int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(0);
  cin >> t >> p;
  CalcFact();
  while (t--) {
    cin >> n >> a[0] >> a[1] >> a[2] >> a[3];
    ans = P();
    S(0, 0, 1, n, 1, 1);
    S(0, n, 1, 0, 1, 1);
    S(0, 0, 1, 0, 1, p - 1);
    int q = p - 1 - 1LL * n * (n + 3) / 2 % (p - 1);
    cout << (ans.r + p) * Pow(2, q) % p << '\n';
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 38ms
memory: 42692kb

input:

1 998244353
3
1 1 0 1

output:

935854081

result:

wrong answer 1st lines differ - expected: '0', found: '935854081'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 10
Accepted

Test #23:

score: 10
Accepted
time: 31ms
memory: 42616kb

input:

999 999999001
2
2 0 0 0
3
3 0 0 0
4
4 0 0 0
5
5 0 0 0
6
6 0 0 0
7
7 0 0 0
8
8 0 0 0
9
9 0 0 0
10
10 0 0 0
11
11 0 0 0
12
12 0 0 0
13
13 0 0 0
14
14 0 0 0
15
15 0 0 0
16
16 0 0 0
17
17 0 0 0
18
18 0 0 0
19
19 0 0 0
20
20 0 0 0
21
21 0 0 0
22
22 0 0 0
23
23 0 0 0
24
24 0 0 0
25
25 0 0 0
26
26 0 0 0
27...

output:

499999501
874999126
359374641
919920956
691222454
586081873
33512082
496961574
790501684
206445579
708073277
492142887
486007979
21786019
802052117
198521403
854660059
658779344
904643630
538486221
357736277
949763680
94144464
342842045
695164947
276856011
552666277
813428208
572457238
910726512
177...

result:

ok 999 lines

Test #24:

score: 10
Accepted
time: 32ms
memory: 42700kb

input:

1 1000000933
154579
154579 0 0 0

output:

657848365

result:

ok single line: '657848365'

Test #25:

score: 10
Accepted
time: 37ms
memory: 42632kb

input:

1 999999001
475343
475343 0 0 0

output:

619834045

result:

ok single line: '619834045'

Test #26:

score: 10
Accepted
time: 39ms
memory: 42632kb

input:

1 999999001
165629
165629 0 0 0

output:

753727723

result:

ok single line: '753727723'

Test #27:

score: 10
Accepted
time: 33ms
memory: 42756kb

input:

1 1000000933
348634
348634 0 0 0

output:

360252198

result:

ok single line: '360252198'

Test #28:

score: 10
Accepted
time: 63ms
memory: 42664kb

input:

10000 999999229
88
88 0 0 0
96
96 0 0 0
24
24 0 0 0
95
95 0 0 0
38
38 0 0 0
27
27 0 0 0
21
21 0 0 0
76
76 0 0 0
45
45 0 0 0
72
72 0 0 0
83
83 0 0 0
78
78 0 0 0
93
93 0 0 0
73
73 0 0 0
64
64 0 0 0
71
71 0 0 0
37
37 0 0 0
42
42 0 0 0
13
13 0 0 0
19
19 0 0 0
79
79 0 0 0
65
65 0 0 0
91
91 0 0 0
28
28 0 ...

output:

228919133
233057601
14414502
12124194
609308119
236040032
384632724
342725930
359253864
890419758
438100183
233688628
152725418
662705946
267680214
513058807
175872233
462495339
413457613
571184369
442694163
537658282
297765746
578699117
239095545
57659973
903105608
745160181
652441005
521182492
347...

result:

ok 10000 lines

Test #29:

score: 10
Accepted
time: 55ms
memory: 42660kb

input:

10000 999999937
53
53 0 0 0
81
81 0 0 0
12
12 0 0 0
19
19 0 0 0
32
32 0 0 0
43
43 0 0 0
54
54 0 0 0
70
70 0 0 0
50
50 0 0 0
86
86 0 0 0
31
31 0 0 0
33
33 0 0 0
85
85 0 0 0
54
54 0 0 0
39
39 0 0 0
90
90 0 0 0
75
75 0 0 0
37
37 0 0 0
84
84 0 0 0
68
68 0 0 0
98
98 0 0 0
73
73 0 0 0
45
45 0 0 0
71
71 0 ...

output:

315851314
404961058
478608220
872834111
278393486
518892420
770069569
110378716
660680499
726285336
810531197
451629260
668208879
770069569
948485799
58650663
417912486
382928062
769177073
530030150
665011673
960730579
766994694
452899566
414064316
5286677
960730579
155111507
110378716
582367338
872...

result:

ok 10000 lines

Test #30:

score: 10
Accepted
time: 66ms
memory: 42660kb

input:

10000 1000000933
94
94 0 0 0
33
33 0 0 0
79
79 0 0 0
11
11 0 0 0
25
25 0 0 0
99
99 0 0 0
12
12 0 0 0
13
13 0 0 0
97
97 0 0 0
70
70 0 0 0
23
23 0 0 0
62
62 0 0 0
29
29 0 0 0
50
50 0 0 0
58
58 0 0 0
96
96 0 0 0
49
49 0 0 0
23
23 0 0 0
94
94 0 0 0
81
81 0 0 0
52
52 0 0 0
27
27 0 0 0
48
48 0 0 0
35
35 0...

output:

302038132
324852455
712788007
51168171
665310026
719061662
642183435
960667161
525345109
829432052
887075946
622617369
92198574
962363993
672506569
207567890
342207626
887075946
302038132
792241276
216981867
23895508
903615878
834858306
749794882
224043056
834367334
962363993
244541137
140944214
712...

result:

ok 10000 lines

Test #31:

score: 10
Accepted
time: 75ms
memory: 42600kb

input:

10000 999999229
48
48 0 0 0
99
99 0 0 0
56
56 0 0 0
82
82 0 0 0
13
13 0 0 0
78
78 0 0 0
44
44 0 0 0
23
23 0 0 0
91
91 0 0 0
39
39 0 0 0
31
31 0 0 0
39
39 0 0 0
22
22 0 0 0
69
69 0 0 0
63
63 0 0 0
49
49 0 0 0
54
54 0 0 0
91
91 0 0 0
86
86 0 0 0
14
14 0 0 0
80
80 0 0 0
47
47 0 0 0
52
52 0 0 0
56
56 0 ...

output:

969254311
41258828
420816844
53150269
413457613
233688628
16329388
962253276
297765746
579341371
491021836
579341371
694633659
238365606
737049114
282048136
467787038
297765746
568729633
239095545
37731193
985694051
20197429
420816844
467787038
337283936
131320231
267680214
890419758
12124194
757560...

result:

ok 10000 lines

Test #32:

score: 10
Accepted
time: 74ms
memory: 42668kb

input:

10000 1000000097
47
47 0 0 0
32
32 0 0 0
67
67 0 0 0
29
29 0 0 0
47
47 0 0 0
24
24 0 0 0
62
62 0 0 0
34
34 0 0 0
69
69 0 0 0
90
90 0 0 0
48
48 0 0 0
33
33 0 0 0
70
70 0 0 0
27
27 0 0 0
93
93 0 0 0
37
37 0 0 0
59
59 0 0 0
95
95 0 0 0
83
83 0 0 0
58
58 0 0 0
91
91 0 0 0
93
93 0 0 0
54
54 0 0 0
87
87 0...

output:

722092697
288259246
443086249
346535444
722092697
96701722
589679036
496735762
287843437
134626935
367894801
88663770
85448149
843667949
156786538
440592027
933702679
516941035
418538470
518055305
804803263
156786538
107212544
806586057
107212544
256264258
107212544
810269380
343546929
426066064
144...

result:

ok 10000 lines

Subtask #5:

score: 0
Skipped

Dependency #1:

0%