QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#783233#9785. Shrookshos_lyricWA 33ms3916kbC++146.6kb2024-11-26 02:27:552024-11-26 02:27:57

Judging History

This is the latest submission verdict.

  • [2024-11-26 02:27:57]
  • Judged
  • Verdict: WA
  • Time: 33ms
  • Memory: 3916kb
  • [2024-11-26 02:27:55]
  • Submitted

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


/*
  \exists naname square containing all points:
    a <=  x+y <= a + N
    b <= -x+y <= b + N
  
  center (x, y) must satisfy:
    2x, 2y \in \Z
    x - N/2 <= 1 <= N <= x + N/2
    y - N/2 <= 1 <= N <= y + N/2
  <=>
    2x, 2y \in \Z
    N/2 <= x <= N/2 + 1
    N/2 <= y <= N/2 + 1
  <=>
    (x, y) = (N/2 + s/2, N/2 + t/2)  for s, t \in {0,1,2}
  <=>
    (x+y, -x+y) = (N + s/2 + t/2, -s/2 + t/2)  for s, t \in {0,1,2}
    a =  N/2 + s/2 + t/2
    b = -N/2 - s/2 + t/2
  
  N = 6
    (s, t) = (0, 0)  (similar for (s, t) = (0, 2), (2, 0), (2, 2))
      .###..
      #...#.
      #.o..#
      #...#.
      .#.#..
      ..#...
    (s, t) = (1, 1)
      ..##..
      .#..#.
      #....#
      #....#
      .#..#.
      ..##..
  N = 7
    (s, t) = (0, 1)  (similar for (s, t) = (1, 0), (1, 2), (2, 1))
      ..###..
      .#...#.
      #.....#
      #.....#
      .#...#.
      ..#.#..
      ...#...
  others are impossible
*/

int N;
vector<int> A;

vector<int> avail;

Mint solve(int a0, int a1, int b0, int b1) {
  if (!(a0 <= a1 && b0 <= b1)) return 0;
  vector<int> freq(N + 1, 0);
  for (int x = 1; x <= N; ++x) {
    int y0 = 1, y1 = N;
    chmax(y0, a0 - x); chmin(y1, a1 - x);
    chmax(y0, b0 + x); chmin(y1, b1 + x);
// cerr<<"  "<<x<<" "<<y0<<" "<<y1<<endl;
    if (~A[x]) {
      if (!(y0 <= A[x] && A[x] <= y1)) return 0;
    } else {
      if (!(y0 <= y1)) return 0;
      ++freq[avail[y1] - avail[y0 - 1]];
    }
  }
  Mint ret = 1;
  int k = 0;
  for (int l = 1; l <= N; ++l) for (; freq[l]--; ) ret *= (l - k++);
  return ret;
}

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d", &N);
    A.assign(N + 2, -1);
    for (int x = 1; x <= N; ++x) {
      scanf("%d", &A[x]);
    }
    
    avail.assign(N + 1, 0);
    for (int y = 1; y <= N; ++y) avail[y] = 1;
    for (int x = 1; x <= N; ++x) if (~A[x]) avail[A[x]] = 0;
    for (int y = 1; y <= N; ++y) avail[y] += avail[y - 1];
    
    int len = 0;
    int ss[5], ts[5];
    for (int s = 0; s <= 2; ++s) for (int t = 0; t <= 2; ++t) if (!((N ^ s ^ t) & 1)) {
      ss[len] = s;
      ts[len] = t;
      ++len;
    }
    Mint ans = 0;
    for (int p = 1; p < 1 << len; ++p) {
      int a0 =  1 + 1, a1 =  N + N;
      int b0 = -N + 1, b1 = -1 + N;
      for (int i = 0; i < len; ++i) if (p >> i & 1) {
        const int s = ss[i], t = ts[i];
        const int a = ( N + s + t) / 2;
        const int b = (-N - s + t) / 2;
        chmax(a0, a); chmin(a1, a + N);
        chmax(b0, b); chmin(b1, b + N);
      }
      const Mint res = solve(a0, a1, b0, b1);
// cerr<<"p = "<<p<<"; "<<a0<<" "<<a1<<" "<<b0<<" "<<b1<<"; "<<(__builtin_parity(p)?-1:+1)<<" "<<res<<endl;
      ans -= (__builtin_parity(p)?-1:+1) * res;
    }
    printf("%u\n", ans.x);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3916kb

input:

6
2
1 2
3
-1 -1 -1
4
1 -1 -1 -1
5
1 -1 -1 -1 5
6
3 -1 -1 -1 -1 4
10
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

output:

1
4
1
0
6
92

result:

ok 6 numbers

Test #2:

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

input:

6
2
1 2
3
-1 -1 -1
4
1 -1 -1 -1
5
1 -1 -1 -1 5
6
3 -1 -1 -1 -1 4
10
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

output:

1
4
1
0
6
92

result:

ok 6 numbers

Test #3:

score: -100
Wrong Answer
time: 33ms
memory: 3820kb

input:

26874
7
-1 -1 7 1 5 3 -1
7
-1 -1 5 3 6 -1 7
7
-1 7 -1 2 3 6 -1
7
-1 2 7 1 5 3 -1
7
3 -1 2 6 1 4 -1
7
4 -1 5 6 1 -1 3
7
-1 -1 4 -1 7 2 -1
7
-1 6 -1 5 4 3 -1
7
6 7 1 2 5 4 -1
7
-1 5 -1 4 2 3 6
7
-1 4 3 5 7 6 -1
7
6 -1 -1 -1 7 5 -1
7
-1 -1 2 -1 4 -1 3
7
-1 -1 2 -1 6 -1 4
7
-1 5 6 2 7 4 -1
7
-1 -1 6 -1 ...

output:

0
0
0
0
1
0
4
3
0
0
1
0
2
0
1
18
0
0
1
0
2
0
2
0
0
1
1
0
1
0
0
0
0
2
0
0
0
0
2
0
0
0
0
1
3
2
0
0
0
0
0
0
0
3
0
2
0
0
0
2
0
2
0
0
2
0
2
2
0
0
0
0
0
2
1
26
3
1
0
0
0
0
5
0
2
1
4
1
2
1
0
0
0
0
1
0
0
3
2
1
0
0
1
0
0
6
0
1
0
0
0
6
1
0
0
1
0
0
2
0
0
0
0
1
2
0
2
1
2
0
0
1
0
1
1
0
0
0
0
0
6
2
1
0
0
0
10
0
0...

result:

wrong answer 5th numbers differ - expected: '0', found: '1'