QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#354752#7858. Basic Equation Solvingucup-team3215WA 26ms3864kbC++203.3kb2024-03-15 23:01:042024-03-15 23:01:05

Judging History

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

  • [2024-03-20 11:12:17]
  • hack成功,自动添加数据
  • (/hack/581)
  • [2024-03-15 23:01:05]
  • 评测
  • 测评结果:WA
  • 用时:26ms
  • 内存:3864kb
  • [2024-03-15 23:01:04]
  • 提交

answer

#include <bits/stdc++.h>

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 g(int v) { return p[v] < 0? v: p[v] = g(p[v]); }
  bool u(int u, int v) { return (u = g(u)) != (v = g(v))? p[v] = u, 1: 0; }
};

int dp[1 << 11];

int solve(auto& gr, auto 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];
}

int solve(auto& gr, DSU eq) {
  DSU weak;
  vector<array<int, 2>> compe[36];
  vector<int> compv[36], compval[36];
  for (auto [a, b]: gr) if (min(a, b) > 9) weak.u(a, b); else if (max(a, b) > 9) compv[weak.g(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.g(i) == i) compv[weak.g(i)].push_back(i);
  for (int i = 10; i < 36; ++i) for (int j = 0; j < compv[i].size(); ++j) if (compval[i].resize(compv[i].size(), -1), 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.g(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.g(inp[i][0][j]), b = eq.g(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.u(min(a, b), max(a, b));
    for (auto& [a, b]: gr) a = eq.g(a), b = eq.g(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[2], o; cin >> o;
    for (auto c: "=><"s) if (auto i = o.find(c); ~i) s[0] = o.substr(0, i), s[1] = o.substr(i + 1), o = ""s + c;
    for (int i: {0, 1}) {
      if (s[i].size() < s[!i].size()) s[i].insert(s[i].begin(), s[!i].size() - s[i].size(), '0');
      for (auto& c: s[i]) c = c <= '9'? c - '0': c - 'A' + 10;
    }
    if (o == "<") swap(s[0], s[1]);
    if (o == "=") for (int i = 0; i < s->size(); ++i) eq.u(min(s[0][i], s[1][i]), max(s[0][i], s[1][i]));
    else inp.push_back({s[0], s[1]});
  }
  solve(gr, eq, 0);
  cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
P=NP

output:

766136394

result:

ok single line: '766136394'

Test #2:

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

input:

1
2000CNY>3000USD

output:

0

result:

ok single line: '0'

Test #3:

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

input:

4
AB>CD
E<A
BC>FF
EF>F1

output:

23645065

result:

ok single line: '23645065'

Test #4:

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

input:

2
BC>DD
BD<EA

output:

27271695

result:

ok single line: '27271695'

Test #5:

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

input:

3
CE>ED
CC>BA
BB<AC

output:

426829091

result:

ok single line: '426829091'

Test #6:

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

input:

10
KG<EI
EJ>DA
EB<IH
EB>JG
KF<CF
JC>FC
IC<BJ
FI>HH
KD>AH
AE>GJ

output:

87744507

result:

ok single line: '87744507'

Test #7:

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

input:

10
EK<GM
EL<DC
DH>IH
EF>BL
IM<LL
EH<JA
DJ<AL
GL>MB
DB>FM
AI<HA

output:

665533468

result:

ok single line: '665533468'

Test #8:

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

input:

10
OD<FK
FJ>NL
NH>KB
KM>CA
CI>JH
CI<AH
CE>GI
CO<EG
FA>HA
FA<IJ

output:

878923575

result:

ok single line: '878923575'

Test #9:

score: 0
Accepted
time: 26ms
memory: 3612kb

input:

10
YH>UQ
UQ>FD
YZ>MK
FY<GO
YV<QW
UV<VJ
UZ>EB
EQ>LX
VP>ZF
LZ>TS

output:

867624189

result:

ok single line: '867624189'

Test #10:

score: 0
Accepted
time: 17ms
memory: 3680kb

input:

10
YH<UL
UD<FY
FK<MU
MM<GO
GG<QW
QJ<VQ
VZ<EB
EG<LX
LZ<ZP
ZV<TS

output:

57935948

result:

ok single line: '57935948'

Test #11:

score: -100
Wrong Answer
time: 1ms
memory: 3596kb

input:

6
EDDC>AB5A
B<C
E9A9B>CACAA
DE2>A0D
DBCDAC>AED3D5
AAA>BB5

output:

196524134

result:

wrong answer 1st lines differ - expected: '169889581', found: '196524134'