QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#880439#9695. Trash Problemhos_lyricWA 0ms3968kbC++147.6kb2025-02-03 12:38:282025-02-03 12:38:40

Judging History

This is the latest submission verdict.

  • [2025-02-03 12:38:40]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3968kb
  • [2025-02-03 12:38:28]
  • 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")

////////////////////////////////////////////////////////////////////////////////
// 2^61 - 1 = 2'305'843'009'213'693'951
struct ModLong61 {
  static constexpr unsigned long long M = (1ULL << 61) - 1;
  unsigned long long x;
  constexpr ModLong61() : x(0ULL) {}
  constexpr ModLong61(unsigned x_) : x(x_) {}
  constexpr ModLong61(unsigned long long x_) : x(x_ % M) {}
  constexpr ModLong61(int x_) : x((x_ < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  constexpr ModLong61(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModLong61 &operator+=(const ModLong61 &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModLong61 &operator-=(const ModLong61 &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModLong61 &operator*=(const ModLong61 &a) {
    const unsigned __int128 y = static_cast<unsigned __int128>(x) * a.x;
    x = (y >> 61) + (y & M);
    x = (x >= M) ? (x - M) : x;
    return *this;
  }
  ModLong61 &operator/=(const ModLong61 &a) { return (*this *= a.inv()); }
  ModLong61 pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModLong61 a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModLong61 inv() const {
    unsigned long long a = M, b = x; long long y = 0, z = 1;
    for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast<long long>(q) * z; y = z; z = w; }
    assert(a == 1ULL); return ModLong61(y);
  }
  ModLong61 operator+() const { return *this; }
  ModLong61 operator-() const { ModLong61 a; a.x = x ? (M - x) : 0ULL; return a; }
  ModLong61 operator+(const ModLong61 &a) const { return (ModLong61(*this) += a); }
  ModLong61 operator-(const ModLong61 &a) const { return (ModLong61(*this) -= a); }
  ModLong61 operator*(const ModLong61 &a) const { return (ModLong61(*this) *= a); }
  ModLong61 operator/(const ModLong61 &a) const { return (ModLong61(*this) /= a); }
  template <class T> friend ModLong61 operator+(T a, const ModLong61 &b) { return (ModLong61(a) += b); }
  template <class T> friend ModLong61 operator-(T a, const ModLong61 &b) { return (ModLong61(a) -= b); }
  template <class T> friend ModLong61 operator*(T a, const ModLong61 &b) { return (ModLong61(a) *= b); }
  template <class T> friend ModLong61 operator/(T a, const ModLong61 &b) { return (ModLong61(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModLong61 &a) const { return (x == a.x); }
  bool operator!=(const ModLong61 &a) const { return (x != a.x); }
  bool operator<(const ModLong61 &a) const { return (x < a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModLong61 &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

#include <chrono>
#ifdef LOCAL
mt19937_64 rng(58);
#else
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif

const ModLong61 BASE = static_cast<unsigned long long>(rng());

////////////////////////////////////////////////////////////////////////////////


// cannot use count
// no move constructor (==> use pointer for merge tech)
// unordered_set by value: __gnu_pbds::null_type
// no erase(iterator)
#include <ext/pb_ds/assoc_container.hpp>
using __gnu_pbds::gp_hash_table;

// https://codeforces.com/blog/entry/62393
#include <chrono>
struct Hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(const pair<int, int> &a) const {
    return operator()((uint64_t)a.first << 32 | a.second);
  }
};
template <class K> using Set = gp_hash_table<K, __gnu_pbds::null_type, Hash>;
template <class K, class V> using Map = gp_hash_table<K, V, Hash>;


using Mint = ModLong61;

int N;
char A[310][310];

char B[310][310];
Mint ha[310][2];

int main() {
  for (; ~scanf("%d", &N); ) {
    for (int x = 0; x < N; ++x) {
      scanf("%s", A[x]);
    }
    
    // rolling in x, linear in y
    vector<Mint> pw(N);
    for (int x = 0; x < N; ++x) pw[x] = x ? (pw[x - 1] * BASE) : 1;
    vector<Mint> rndL(N), rndR(N);
    for (int y = 0; y < N; ++y) {
      rndL[y] = (unsigned long long)rng();
      rndR[y] = (unsigned long long)rng();
    }
    
    memcpy(B, A, sizeof(B));
    Int ans = 0;
    for (int yL = N; yL >= 0; --yL) {
      for (int x = 0; x < N; ++x) {
        int cnt = 0;
        for (int y = yL; y < N; ++y) {
          if (A[x][y] == '1') {
            if (cnt & 1) {
              if (y > yL && B[x][y - 1] == 'L') {
                B[x][y] = 'R';
              } else {
                B[x][y] = 'x';
                break;
              }
            } else {
              B[x][y] = 'L';
            }
            ++cnt;
          }
        }
      }
// cerr<<"yL = "<<yL<<endl;for(int x=0;x<N;++x)cerr<<B[x]<<endl;
      memset(ha, 0, sizeof(ha));
      for (int yR = yL + 1; yR <= N; ++yR) {
        Map<unsigned long long, int> freq;
        ++freq[0];
        for (int x = N; --x >= 0; ) {
          bool ok = true;
          int cnt = 0;
          Mint sum = 0;
          for (int y = yL; y < yR; ++y) {
            if (B[x][y] == 'L') {
              ++cnt;
              swap(ha[y][0], ha[y][1]);
              ha[y][0] += pw[x] * rndL[y];
            } else if (B[x][y] == 'R') {
              ++cnt;
              swap(ha[y][0], ha[y][1]);
              ha[y][0] += pw[x] * rndR[y];
            } else if (B[x][y] == 'x') {
              ok = false;
            }
            sum += (ha[y][0] * BASE - ha[y][1]);
          }
          ok = ok && (!(cnt & 1));
          if (ok) {
            auto it = freq.find(sum.x);
            if (it != freq.end()) {
// cerr<<"yL = "<<yL<<", yR = "<<yR<<", x = "<<x<<": "<<it->second<<endl;
              ans += it->second;
            }
          } else {
            freq.clear();
          }
          ++freq[sum.x];
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
0110
0110
1111
1111

output:

17

result:

ok 1 number(s): "17"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3968kb

input:

20
00110101100010111111
01000101010001111000
11101001001011011010
01000001001001101110
11100011001111111100
01101110111100111100
10000101011110110101
10001001101110000110
11110011110001110010
10001000101101011111
01000010001100110101
00111100100010011010
01000011000111011011
00111010111111010101
000...

output:

539

result:

wrong answer 1st numbers differ - expected: '549', found: '539'