QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#454551#8821. Nightmarehos_lyricWA 381ms8492kbC++149.1kb2024-06-25 03:16:152024-06-25 03:16:15

Judging History

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

  • [2024-06-25 03:16:15]
  • 评测
  • 测评结果:WA
  • 用时:381ms
  • 内存:8492kb
  • [2024-06-25 03:16:15]
  • 提交

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")

////////////////////////////////////////////////////////////////////////////////
// Barrett
struct ModInt {
  static unsigned M;
  static unsigned long long NEG_INV_M;
  static void setM(unsigned m) { M = m; NEG_INV_M = -1ULL / M; }
  unsigned x;
  ModInt() : x(0U) {}
  ModInt(unsigned x_) : x(x_ % M) {}
  ModInt(unsigned long long x_) : x(x_ % M) {}
  ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  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) {
    const unsigned long long y = static_cast<unsigned long long>(x) * a.x;
    const unsigned long long q = static_cast<unsigned long long>((static_cast<unsigned __int128>(NEG_INV_M) * y) >> 64);
    const unsigned long long r = y - M * q;
    x = r - M * (r >= 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; }
};
unsigned ModInt::M;
unsigned long long ModInt::NEG_INV_M;
// !!!Use ModInt::setM!!!
////////////////////////////////////////////////////////////////////////////////

using Mint = ModInt;


int N, P;
Mint G[510][510];

Mint g[510][510];
Mint ans[510][510];

struct Op {
  int typ;
  int i, j;
  Mint c;
};
vector<Op> ops;

// swap(v[i], v[j]);
void sw(int i, int j) {
// cerr<<COLOR("94")<<"sw "<<i<<" "<<j<<COLOR()<<endl;
  ops.push_back(Op{0, i, j, 0});
  swap(g[i][i], g[j][j]);
  for (int k = 0; k < N; ++k) if (i != k && j != k) {
    swap(g[i][k], g[j][k]);
    swap(g[k][i], g[k][j]);
  }
}

// v[i] += c v[j]
void add(int i, int j, Mint c) {
// cerr<<COLOR("91")<<"add "<<i<<" "<<j<<" "<<c<<COLOR()<<endl;
  ops.push_back(Op{1, i, j, c});
  g[i][i] += c * g[i][j];
  g[i][i] += c * g[j][i];
  g[i][i] += c * c * g[j][j];
  for (int k = 0; k < N; ++k) if (i != k) {
    g[i][k] += c * g[j][k];
    g[k][i] += c * g[k][j];
  }
}

int main() {
  for (; ~scanf("%d%d", &N, &P); ) {
    Mint::setM(P);
    memset(G, 0, sizeof(G));
    for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) {
      scanf("%u", &G[i][j].x);
    }
// cerr<<string(80,'=')<<" N = "<<N<<", P = "<<P<<endl;
    
    memcpy(g, G, sizeof(G));
    ops.clear();
    int h = 0;
    for (; ; ) {
// cerr<<"h = "<<h<<endl;for(int i=0;i<N;++i)pv(g[i],g[i]+N);
      for (int i = h; i < N; ++i) {
        if (g[i][i]) {
          sw(h, i);
          break;
        }
      }
      if (h < N && g[h][h]) {
        const Mint s = g[h][h].inv();
        for (int i = h + 1; i < N; ++i) {
          add(i, h, -s * g[h][i]);
        }
        h += 1;
        continue;
      }
      for (int i = h; i < N; ++i) {
        for (int j = i + 1; j < N; ++j) {
          if (g[i][j]) {
            sw(h, i);
            sw(h + 1, j);
            goto found2;
          }
        }
      }
     found2:{}
      if (h + 1 < N && g[h][h + 1]) {
        if (P == 2) {
          for (int i = h + 2; i < N; ++i) {
            add(i, h, g[h + 1][i]);
            add(i + 1, h, g[h][i]);
          }
          h += 2;
        } else {
          add(h, h + 1, 1);
        }
        continue;
      }
      break;
    }
    assert(h);
    
    int dim = h;
    memset(ans, 0, sizeof(ans));
    if (P == 2) {
      int od = -1;
      for (int i = 0; i < h; ++i) if (g[i][i]) {
        od = i;
        break;
      }
      if (!~od) {
        g[h][h] = 1;
        od = h;
        ++dim;
      }
      ans[od][od] = 1;
      for (int i = dim - 1; i >= 0; ) {
        if (od == i) {
          i -= 1;
        } else if (g[i][i]) {
          ans[i][i] = 1;
          i -= 1;
        } else {
          const int j = i - 1;
          assert(j >= 0);
          assert(!g[j][j]);
          assert(g[j][i]);
          assert(g[i][j]);
          /*
            v0, v1, v2
            100
            001
            010
            
            v0 + v1 + v2, v0 + v1, v0 + v2
            100
            010
            001
          */
          add(j, od, 1);
          add(i, od, 1);
          add(od, j, 1);
          add(od, i, 1);
          ans[j][j] = ans[i][i] = 1;
          i -= 2;
        }
      }
    } else {
      vector<int> sqr(P, -1);
      for (int x = 0; x < P; ++x) {
        const int y = (Mint(x) * Mint(x)).x;
        if (!~sqr[y]) sqr[y] = x;
      }
      // z^2 + 1: non-residue
      Mint z = 0;
      for (int x = 0; x < P; ++x) {
        const int y = (Mint(x) * Mint(x) + 1).x;
        if (!~sqr[y]) {
          z = x;
          break;
        }
      }
      assert(z);
      Mint det = 1;
      for (int i = 0; i < h; ++i) det *= g[i][i];
      if (!~sqr[det.x]) {
        g[h][h] = det;
        ++dim;
      }
      for (int i = dim - 1; i >= 0; ) {
        if (~sqr[g[i][i].x]) {
          ans[i][i] = sqr[g[i][i].x];
          i -= 1;
        } else {
          for (int j = i; --j >= 0; ) {
            if (~sqr[(g[j][j] * g[i][i]).x]) {
              ans[j][j] = z;
              ans[j][i] = -1;
              ans[i][j] = 1;
              ans[i][i] = z;
              for (const int k : {j, i}) {
                const Mint c = sqr[(g[k][k] / (z*z+1)).x];
                ans[k][j] *= c;
                ans[k][i] *= c;
              }
              i -= 2;
              goto foundPair;
            }
          }
          assert(false);
         foundPair:{}
        }
      }
    }
    
    reverse(ops.begin(), ops.end());
    for (const Op &op : ops) {
      if (op.typ == 0) {
        for (int k = 0; k < dim; ++k) swap(ans[op.i][k], ans[op.j][k]);
      } else if (op.typ == 1) {
        for (int k = 0; k < dim; ++k) ans[op.i][k] -= op.c * ans[op.j][k];
      } else {
        assert(false);
      }
    }
    
    printf("%d\n", dim);
    for (int i = 0; i < N; ++i) {
      for (int k = 0; k < dim; ++k) {
        if (k) printf(" ");
        printf("%u", ans[i][k].x);
      }
      puts("");
    }
#ifdef LOCAL
for(int i=0;i<N;++i)for(int j=0;j<N;++j){
 Mint dot=0;
 for(int k=0;k<dim;++k)dot+=ans[i][k]*ans[j][k];
 if(G[i][j]!=dot){
  cerr<<i<<" "<<j<<": "<<G[i][j]<<" "<<dot<<endl;
  assert(false);
 }
}
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6808kb

input:

2 2
1 1
1 1

output:

1
1
1

result:

ok accepted

Test #2:

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

input:

3 5
4 4 3
4 4 3
3 3 2

output:

2
2 0
2 0
4 1

result:

ok accepted

Test #3:

score: 0
Accepted
time: 340ms
memory: 8328kb

input:

500 2
1 0 1 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1 1 1 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 1 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 0 1 0 0 ...

output:

423
1 0 0 0 0 0 0 0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