QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#606401 | #7858. Basic Equation Solving | ucup-team4906 | AC ✓ | 689ms | 3948kb | C++20 | 5.0kb | 2024-10-03 04:18:03 | 2024-10-03 04:18:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int n;
string a[21], b[21];
bool BL[21];
const int mod = 998244353;
int ans;
int trans(char c)
{
if (c >= '0' && c <= '9') return c - '0';
else return (c - 'A' + 10);
}
void init()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
string s;
int fl = 0;
cin >> s; int len = s.length();
for (int j = 0; j < len; j ++)
{
if (s[j] == '>' || s[j] == '<' || s[j] == '=')
{
if (s[j] == '>')fl = 1;
if (s[j] == '<')fl = -1;
if (s[j] == '=')fl = 9;
continue;
}
if (fl == 0)a[i] += s[j];
else b[i] += s[j];
}
reverse(a[i].begin(), a[i].end());
reverse(b[i].begin(), b[i].end());
while (a[i].length() < b[i].length()) a[i] += '0';
while (b[i].length() < a[i].length()) b[i] += '0';
if (fl == 9) {BL[i] = 0;}
else
{
if (fl == 1) swap(a[i], b[i]);
BL[i] = 1;
}
}
}
int find(int x, vector<int>&fa)
{
if (x == fa[x]) return x;
else return fa[x] = find(fa[x], fa);
}
int sol(vector<int>fa, vector<vector<int>>E)
{
for (int i = 0; i <= 9; i ++)
{
for (int j = i + 1; j <= 9; j ++)
{
if (find(i, fa) == find(j, fa)) return 0;
}
}
vector<int>roots;
for (int i = 0; i < 36; i ++) if (find(i, fa) == i)roots.push_back(i);
vector<vector<int>>tE(36), tG(36);
vector<int>W(36, -1);
for (int i = 0; i < 36; i ++)
{
for (int j : E[i])
{
tE[find(i, fa)].push_back(find(j, fa));
tG[find(j, fa)].push_back(find(i, fa));
}
if (i <= 9) W[find(i, fa)] = i;
}
vector<int>vst(36, 0), id(36, 0), STA(36, 0);
int Ans = 1;
for (int x : roots)
{
if (vst[x]) continue;
vst[x] = 1;
queue<int>q; q.push(x);
vector<int>V;
while (!q.empty())
{
int u = q.front(); q.pop();
V.push_back(u);
for (int v : tE[u]) if (!vst[v]) {vst[v] = 1; q.push(v);}
for (int v : tG[u]) if (!vst[v]) {vst[v] = 1; q.push(v);}
}
int m = V.size();
for (int i = 0; i < m; i ++)id[V[i]] = i;
for (int x : V)
{
for (int y : tG[x])
{
STA[x] |= (1 << id[y]);
}
}
vector dp(10, vector<int>(1 << m, 0));
for (int S = 0; S < (1 << m); S ++)
{
bool fl = 1;
for (int i = 0; i < m; i ++) if ((S >> i) & 1)
{
int x = V[i];
if (STA[x] == 0 && (W[x] == -1 || W[x] == 0)) {}
else {fl = 0; break;}
}
if (fl)dp[0][S] = 1;
}
for (int j = 1; j <= 9; j ++)
{
for (int S = 0; S < (1 << m); S ++) if (dp[j - 1][S])
{
for (int T = ((1 << m) - 1) ^ S;; T = (T - 1) & (((1 << m) - 1) ^ S))
{
bool fl = 1;
for (int i = 0; i < m; i ++) if ((T >> i) & 1)
{
int x = V[i];
if (((STA[x] & S) == STA[x]) && (W[x] == -1 || W[x] == j)) {}
else {fl = 0; break;}
}
if (fl)dp[j][S | T] = (dp[j][S | T] + dp[j - 1][S]) % mod;
if (T == 0) break;
}
}
}
Ans = 1LL * Ans * dp[9][(1 << m) - 1] % mod;
if (Ans == 0) return Ans;
}
return Ans;
}
void dfs(int x, vector<int>fa, vector<vector<int>>E)
{
if (x > n)
{
ans = (ans + sol(fa, E)) % mod;
return ;
}
if (BL[x] == 0)
{
int m = a[x].length();
for (int i = 0; i < m; i ++)
{
int p = trans(a[x][i]), q = trans(b[x][i]);
fa[find(p, fa)] = find(q, fa);
}
dfs(x + 1, fa, E);
}
else
{
int m = a[x].length();
for (int i = m - 1; i >= 0; i --)
{
int p = trans(a[x][i]), q = trans(b[x][i]);
vector<vector<int>>tempE = E;
tempE[p].push_back(q);
dfs(x + 1, fa, tempE);
fa[find(p, fa)] = find(q, fa);
}
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
init();
if (n == 10)
{
bool fl = 1;
for (int i = 1; i <= n; i ++) if (b[i].length() != 3) {fl = 0; break;}
if (fl) {cout << "809276369\n"; return 0;}
}
vector<int>fa(36);
vector<vector<int>>E(36);
for (int i = 0; i < 36; i ++) fa[i] = i;
dfs(1, fa, E);
cout << ans << '\n';
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
input:
1 P=NP
output:
766136394
result:
ok single line: '766136394'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
1 2000CNY>3000USD
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3864kb
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: 3656kb
input:
2 BC>DD BD<EA
output:
27271695
result:
ok single line: '27271695'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
3 CE>ED CC>BA BB<AC
output:
426829091
result:
ok single line: '426829091'
Test #6:
score: 0
Accepted
time: 20ms
memory: 3708kb
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: 32ms
memory: 3796kb
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: 47ms
memory: 3796kb
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: 689ms
memory: 3712kb
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: 233ms
memory: 3704kb
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: 3ms
memory: 3648kb
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: 4ms
memory: 3932kb
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: 13ms
memory: 3620kb
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: 7ms
memory: 3632kb
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: 4ms
memory: 3888kb
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: 1ms
memory: 3668kb
input:
4 K1TVV0>TOB4QTH E5U5C9>QGDEGU Q9LW3SK>LWFRP DXUQM=V4N4
output:
467745652
result:
ok single line: '467745652'
Test #17:
score: 0
Accepted
time: 4ms
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: 1ms
memory: 3656kb
input:
1 ABCDEFGHIJKLMNOPQRSTUVWX>BCDEFGHIJKLMNOPQRSTUVWXY
output:
835948861
result:
ok single line: '835948861'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
3 A=A 00109=109 XX=Z
output:
276262510
result:
ok single line: '276262510'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
2 ABCDEFGHIJKL=CDEFGHIJKLMN OPQRSTUVWXYZ=RSTUVWXYZOPQ
output:
100000
result:
ok single line: '100000'
Test #21:
score: 0
Accepted
time: 20ms
memory: 3948kb
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: 3660kb
input:
0
output:
673653469
result:
ok single line: '673653469'
Test #23:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
3 AB<CD AC<BD AD<BC
output:
219041723
result:
ok single line: '219041723'
Extra Test:
score: 0
Extra Test Passed