QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#580#354746#7858. Basic Equation Solvingliswtucup-team3215Failed.2024-03-20 11:07:002024-03-20 11:07:01

Details

Extra Test:

Accepted
time: 67ms
memory: 3576kb

input:

10
A<BCD
E<FGH
I<JKL
M<NOP
Q<RST
U<VWX
Y<ZAB
C<DEF
G<HIJ
K<LMN

output:

809276369

result:

ok single line: '809276369'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#354746#7858. Basic Equation Solvingucup-team3215AC ✓28ms3864kbC++203.8kb2024-03-15 22:31:332024-03-20 11:13:10

answer

#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

constexpr int mod = 998244353;

auto& add(auto&& a, auto b) { return a -= (a += b) >= mod? mod: 0; }
auto& mul(auto&& a, auto b) { return a = a * (uint64_t)b % mod; }

struct DSU {
  int p[36];

  DSU() { fill(p, p + 36, -1); }

  int operator()(int v) { return getp(v); }
  bool operator()(int u, int v) { return uni(u, v); }
  bool operator[](array<int, 2> uv) { return getp(uv[0]) == getp(uv[1]); }
  int getp(int v) { return p[v] < 0? v: p[v] = getp(p[v]); }
  bool uni(int u, int v) { return (u = getp(u)) != (v = getp(v))? p[v] = u, 1: 0; }
};

int dp[1 << 11];

int solve(auto& gr, vector<int> val) {
  int n = val.size();
  fill_n(dp, 1 << n, 0);
  vector<int> mgr(n);
  for (auto [a, b]: gr) mgr[b] |= 1 << a;
  dp[0] = 1;
  for (int v = 10; v--; )
  for (int m = 1 << n; m--; ) if (dp[m]) {
    int ava = 0;
    for (int i = 0; i < n; ++i) if (!(m >> i & 1) && !(mgr[i] & ~m) && (!~val[i] || val[i] == v)) ava |= 1 << i;
    for (int t = 0; --t &= ava; ) add(dp[m + t], dp[m]);
  }
  return dp[(1 << n) - 1];
}

vector<vector<array<int, 2>>> compe(36);
vector<vector<int>> compv(36);
vector<vector<int>> compval(36);

int solve(auto& gr, DSU eq) {
  DSU weak;
  for (auto& x: compe) x = {};
  for (auto& x: compv) x = {};
  for (auto& x: compval) x = {};
  for (auto [a, b]: gr) if (min(a, b) > 9) weak(a, b);
  for (auto [a, b]: gr) if (max(a, b) > 9 && min(a, b) <= 9) compv[weak(max(a, b))].push_back(min(a, b));
  for (int i = 10; i < 36; ++i) sort(compv[i].begin(), compv[i].end()), compv[i].erase(unique(compv[i].begin(), compv[i].end()), compv[i].end());
  for (int i = 10; i < 36; ++i) if (eq(i) == i) compv[weak(i)].push_back(i);
  for (int i = 10; i < 36; ++i) if (weak(i) == i) compval[i].assign(compv[i].size(), -1);
  for (int i = 10; i < 36; ++i) for (int j = 0; j < compv[i].size(); ++j) if (compv[i][j] <= 9) compval[i][j] = compv[i][j];
  auto id = [&](int c, int v) {
    auto& loc = compv[c];
    return find(loc.begin(), loc.end(), v) - loc.begin();
  };
  for (auto [a, b]: gr) if (max(a, b) > 9) { int c = weak(max(a, b)); compe[c].push_back({id(c, a), id(c, b)}); }
  int ans = 1;
  for (int i = 10; i < 36; ++i) if (compv[i].size()) {
    mul(ans, solve(compe[i], compval[i]));
  }
  return ans;
}

vector<array<string, 2>> inp;
int ans;

bool isgr(auto& gr, int a, int b) {
  uint64_t vis = 1ull << a;
  for (int ch = 1; ch--; )
  for (auto [x, y]: gr) if ((vis >> x & 1) && !(vis >> y & 1)) ch = 1, vis |= 1ull << y;
  return vis >> b & 1;
}

void solve(auto gr, DSU eq, int i) {
  if (i == inp.size()) return void(add(ans, solve(gr, eq)));
  for (int j = 0; j < inp[i][0].size(); ++j) if (int a = eq(inp[i][0][j]), b = eq(inp[i][1][j]); a != b) {
    if (isgr(gr, b, a)) return;
    if (isgr(gr, a, b)) return solve(gr, eq, i + 1);
    gr.push_back({a, b}), solve(gr, eq, i + 1), gr.pop_back();
    eq(min(a, b), max(a, b));
    for (auto& [a, b]: gr) a = eq(a), b = eq(b);
  }
}

int main() {
  DSU eq;
  vector<array<int, 2>> gr;
  for (int i = 0; i < 9; ++i) gr.push_back({i + 1, i});
  int n; cin >> n;
  for (int i = 0; i < n; ++i) {
    string s, t, o; cin >> s;
    for (auto c: "=><"s) if (auto i = s.find(c); ~i) o = ""s + c, t = s.substr(0, i), s = s.substr(i + 1);
    if (s.size() < t.size()) s.insert(s.begin(), t.size() - s.size(), '0');
    if (s.size() > t.size()) t.insert(t.begin(), s.size() - t.size(), '0');
    for (auto& c: s) c = c <= '9'? c - '0': c - 'A' + 10;
    for (auto& c: t) c = c <= '9'? c - '0': c - 'A' + 10;
    if (o == ">") swap(s, t);
    if (o == "=") for (int i = 0; i < s.size(); ++i) eq(min(s[i], t[i]), max(s[i], t[i]));
    else inp.push_back({s, t});
  }
  solve(gr, eq, 0);
  cout << ans << '\n';
}