QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449296#7838. 往日之影Made_in_Code0 63ms31040kbC++143.8kb2024-06-20 21:40:082024-06-20 21:40:09

Judging History

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

  • [2024-06-20 21:40:09]
  • 评测
  • 测评结果:0
  • 用时:63ms
  • 内存:31040kb
  • [2024-06-20 21:40:08]
  • 提交

answer

#include <iostream>
#define LL long long

using namespace std;

const int kMaxN = 1e6 + 1, kMaxL = 20;
struct H {
  int pw, 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 f[4];

  P() {
    f[0] = f[1] = f[2] = f[3] = 0;
  }

  P(LL f0, LL f1, LL f2, LL f3) {
    f[0] = f0, f[1] = f1, f[2] = f2, f[3] = f3;
  }

  P operator+(P x) {
    P ans;
    for (int i = 0; i < 4; i++) {
      ans.f[i] = (f[i] + x.f[i]) % p;
    }
    return ans;
  }

  P operator<<(LL x) {
    P ans;
    for (int i = 0; i < 4; i++) {
      ans.f[i + x & 3] = f[i];
    }
    return ans;
  }

  P operator*(LL x) {
    P ans;
    for (int i = 0; i < 4; i++) {
      ans.f[i] = f[i] * x % p;
    }
    return ans;
  }

  P operator*(P x) {
    P ans;
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        Add(ans.f[i + j & 3], f[i] * x.f[j]);
      }
    }
    return ans;
  }

  P operator^(LL y) {
    P x = *this, ans(1, 0, 0, 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 b[4] = {};
  P w(1, 0, 0, 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.f[0] = w.f[0] * h[a[i]].fact % p;
    for (int j = 0; j < 4; j++) {
      w.f[0] = w.f[0] * 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, 0, 0);
      p.f[i + j & 3] += 1;
      w = w * (p ^ b[i] * b[j]);
    }
  }
  for (int i = 0; i < 4; i++) {
    P p(1, 0, 0, 0);
    p.f[i + i & 3] += 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) {
  if (x == 4) {
    CalcAns();
    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);
        }
      }
    }
  }
}

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);
    S(0, n, 1, 0, 1);
    int q = p - 1 - 1LL * n * (n + 3) / 2 % (p - 1);
    cout << (ans.f[0] - ans.f[2] + 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: 10
Accepted
time: 38ms
memory: 30984kb

input:

1 998244353
3
1 1 0 1

output:

0

result:

ok single line: '0'

Test #2:

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

input:

1 998244353
7
0 2 1 4

output:

998069185

result:

ok single line: '998069185'

Test #3:

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

input:

1 998244353
4
0 1 0 3

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Wrong Answer
time: 30ms
memory: 30920kb

input:

1 998244353
2
1 0 1 0

output:

124780544

result:

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

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #23:

score: 0
Wrong Answer
time: 63ms
memory: 30896kb

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:

374999626
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:

wrong answer 1st lines differ - expected: '499999501', found: '374999626'

Subtask #5:

score: 0
Skipped

Dependency #1:

0%