QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#354746 | #7858. Basic Equation Solving | ucup-team3215 | AC ✓ | 28ms | 3864kb | C++20 | 3.8kb | 2024-03-15 22:31:33 | 2024-03-20 11:13:10 |
Judging History
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';
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3556kb
input:
1 P=NP
output:
766136394
result:
ok single line: '766136394'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3540kb
input:
1 2000CNY>3000USD
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
4 AB>CD E<A BC>FF EF>F1
output:
23645065
result:
ok single line: '23645065'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
2 BC>DD BD<EA
output:
27271695
result:
ok single line: '27271695'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3788kb
input:
3 CE>ED CC>BA BB<AC
output:
426829091
result:
ok single line: '426829091'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3628kb
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: 3580kb
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: 3600kb
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: 28ms
memory: 3616kb
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: 20ms
memory: 3820kb
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: 0
Accepted
time: 1ms
memory: 3844kb
input:
6 EDDC>AB5A B<C E9A9B>CACAA DE2>A0D DBCDAC>AED3D5 AAA>BB5
output:
169889581
result:
ok single line: '169889581'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
9 C<B A>B FDF2<FBDB DB>B4 CF>DA EF4<D1A B8<A5 B3>BF FFA<D5B
output:
0
result:
ok single line: '0'
Test #13:
score: 0
Accepted
time: 3ms
memory: 3616kb
input:
5 SP6<GCT J0RFZ<ZZLUX UDY7<UEVX C1CQ>FXTG SOCT07<MEABU8
output:
603602671
result:
ok single line: '603602671'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
7 F>M G8F<KC5 F06<E8G H5J<BJE M8CDE<DIGMC AE08>EFI7 DM>CI
output:
821791712
result:
ok single line: '821791712'
Test #15:
score: 0
Accepted
time: 1ms
memory: 3848kb
input:
10 PS1>O9O G76>F8S J<S SB>Y4 WS<VM E<N ZR<CV G8T>XPJ J<A KT<LS
output:
97272892
result:
ok single line: '97272892'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
4 K1TVV0>TOB4QTH E5U5C9>QGDEGU Q9LW3SK>LWFRP DXUQM=V4N4
output:
467745652
result:
ok single line: '467745652'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
5 BC5F<AC3F FA4<D48306EDD EFDD>FDABE CF5C<AFDDB FAF<C387
output:
808992671
result:
ok single line: '808992671'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
1 ABCDEFGHIJKLMNOPQRSTUVWX>BCDEFGHIJKLMNOPQRSTUVWXY
output:
835948861
result:
ok single line: '835948861'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
3 A=A 00109=109 XX=Z
output:
276262510
result:
ok single line: '276262510'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
2 ABCDEFGHIJKL=CDEFGHIJKLMN OPQRSTUVWXYZ=RSTUVWXYZOPQ
output:
100000
result:
ok single line: '100000'
Test #21:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
9 N=A8 TT<QO3G LS>JV TSG>U5F D<A934 FK<HKG O>S1 GT<BBCX SG>S
output:
929594610
result:
ok single line: '929594610'
Test #22:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
0
output:
673653469
result:
ok single line: '673653469'
Test #23:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
3 AB<CD AC<BD AD<BC
output:
219041723
result:
ok single line: '219041723'
Extra Test:
score: 0
Extra Test Passed