QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#63225#1262. Justice For EveryoneElegiaAC ✓133ms18068kbC++173.7kb2022-11-20 23:33:462022-11-20 23:33:48

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-20 23:33:48]
  • 评测
  • 测评结果:AC
  • 用时:133ms
  • 内存:18068kb
  • [2022-11-20 23:33:46]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

const int N = 35, L = 12, P = 998244353;

int mpow(int x, int k) {
  if (k == 0) return 1;
  int ret = mpow(x * (ull)x % P, k >> 1);
  if (k & 1) ret = ret * (ull)x % P;
  return ret;
}

int norm(int x) { return x >= P ? x - P : x; }
int reduce(int x) { return x < 0 ? x + P : x; }
void add(int& x, int y) {
  if ((x += y - P) < 0) x += P;
}
void sub(int& x, int y) {
  if ((x -= y) < 0) x += P;
}

int root[(1 << L) + 1];
void prep(int l) {
  int n = 1 << L;
  int* w2 = root;
  *w2 = 1;
  w2[1 << l] = mpow(31, 1 << (21 - l));
  for (int i = l; i; --i) w2[1 << (i - 1)] = (ull)w2[1 << i] * w2[1 << i] % P;
  for (int i = 1; i < n; ++i) w2[i] = (ull)w2[i & (i - 1)] * w2[i & -i] % P;
}

void DIF(int *a, int l) {
  int *j, *k, n = 1 << l, len = n >> 1, r, *o;
  for (; len; len >>= 1)
    for (j = a, o = root; j != a + n; j += len << 1, ++o)
      for (k = j; k != j + len; ++k) {
        r = (ull)*o * k[len] % P;
        k[len] = reduce(*k - r);
        add(*k, r);
      }
}

void DIT(int* a, int l) {
  int *j, *k, n = 1 << l, len = 1, r, *o;
  for (; len != n; len <<= 1)
    for (j = a, o = root; j != a + n; j += len << 1, ++o)
      for (k = j; k != j + len; ++k) {
        r = reduce(*k + k[len] - P);
        k[len] = ull(*k - k[len] + P) * *o % P;
        *k = r;
      }
}

void fft(int* a, int lgn, int d = 1) {
  int n = 1 << lgn;
  if (d == 1) DIF(a, lgn);
  else {
    DIT(a, lgn);
    reverse(a + 1, a + n);
    ull nv = P - (P - 1) / n;
    for (int i = 0; i < n; ++i) a[i] = a[i] * nv % P;
  }
}

int n;
int f[N][N][1 << L], mat[N][N];
int fac[2 << L], ifac[2 << L], res[1 << L];
pair<int, int> a[N];

void GG() {
  cout << "0\n";
  exit(0);
}

void init(int M) {
  fac[0] = 1;
  for (int i = 1; i <= M; ++i) fac[i] = fac[i - 1] * (ull)i % P;
  ifac[M] = mpow(fac[M], P - 2);
  for (int i = M; i; --i) ifac[i - 1] = ifac[i] * (ull)i % P;
}

int det() {
  int ret = 1;
  for (int i = 1; i <= n; ++i) {
    int o = -1;
    for (int j = i; j <= n; ++j) if (mat[j][i]) {
      o = j;
      break;
    }
    if (o == -1) return 0;
    if (o != i) {
      ret = P - ret;
      for (int j = i; j <= n; ++j) swap(mat[i][j], mat[o][j]);
    }
    ret = ret * (ull)mat[i][i] % P;
    int nv = mpow(mat[i][i], P - 2);
    for (int j = i; j <= n; ++j) mat[i][j] = mat[i][j] * (ull)nv % P;
    for (int j = i + 1; j <= n; ++j) for (int k = n; k >= i; --k)
      mat[j][k] = (mat[j][k] + (ull)(P - mat[j][i]) * mat[i][k]) % P;
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  prep(L);
  init((2 << L) - 1);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i].first;
  for (int i = 1; i <= n; ++i) cin >> a[i].second;
  sort(a + 1, a + n + 1);
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += a[i].second - a[i].first;
    if (a[i].first > a[i].second) GG();
    if (a[i].second < a[i - 1].second) GG();
  }
  if (sum % 2) GG();
  for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (a[i].first <= a[j].second) {
    int t = a[j].second - a[i].first;
    for (int k = 0; k <= t / 2; ++k) f[i][j][k] = ifac[k] * (ull)ifac[t - k * 2] % P;
    fft(f[i][j], L, 1);
  }
  for (int i = 0; i != 1 << L; ++i) {
    for (int j = 1; j <= n; ++j)
      for (int k = 1; k <= n; ++k)
        mat[j][k] = f[j][k][i];
    res[i] = det();
  }
  fft(res, L, -1);
  int ans = 0;
  for (int i = 0; i <= sum / 2; ++i) {
    int v = res[i] * (ull)fac[sum - i * 2] % P * fac[sum / 2] % P * ifac[sum / 2 - i] % P;
    if (i & 1) sub(ans, v);
    else add(ans, v);
  }
  ans = ans * (ull)mpow(2, P - 1 - sum / 2) % P;
  cout << ans << '\n';

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3608kb

input:

3
1 2 3
3 4 5

output:

1

result:

ok answer is '1'

Test #2:

score: 0
Accepted
time: 3ms
memory: 3556kb

input:

3
1 2 3
7 8 9

output:

42

result:

ok answer is '42'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3436kb

input:

3
1 4 7
3 6 9

output:

6

result:

ok answer is '6'

Test #4:

score: 0
Accepted
time: 3ms
memory: 3472kb

input:

1
1
3

output:

0

result:

ok answer is '0'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3404kb

input:

1
1
1

output:

1

result:

ok answer is '1'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3472kb

input:

2
1 4
4 7

output:

1

result:

ok answer is '1'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3524kb

input:

10
10 9 8 7 6 1 2 3 4 5
11 12 13 114 115 120 129 128 127 126

output:

0

result:

ok answer is '0'

Test #8:

score: 0
Accepted
time: 2ms
memory: 3476kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 131

output:

0

result:

ok answer is '0'

Test #9:

score: 0
Accepted
time: 129ms
memory: 18068kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

output:

936606510

result:

ok answer is '936606510'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3524kb

input:

30
16 21 33 44 51 60 71 81 91 100 110 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199
110 111 112 113 114 115 116 117 118 119 120 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199

output:

0

result:

ok answer is '0'

Test #11:

score: 0
Accepted
time: 108ms
memory: 11984kb

input:

30
16 21 33 44 51 60 71 81 91 100 110 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199
110 111 112 113 114 115 116 117 118 119 120 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 200

output:

836228983

result:

ok answer is '836228983'

Test #12:

score: 0
Accepted
time: 20ms
memory: 5180kb

input:

10
10 9 8 7 6 5 4 3 2 1
110 109 108 107 106 105 104 103 102 101

output:

422463757

result:

ok answer is '422463757'

Test #13:

score: 0
Accepted
time: 133ms
memory: 17932kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

output:

575061951

result:

ok answer is '575061951'