QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#296771 | #7858. Basic Equation Solving | Dualqwq | TL | 7083ms | 4652kb | C++20 | 4.0kb | 2024-01-03 16:08:03 | 2024-03-20 11:12:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 15,M = 40,Sz = 1 << 11,P = 998244353;
inline void Plus(int &x,const int &y) { x += y;if(x >= P) x -= P;}
int n;
string s1[N],s2[N];
char opp[N];
int nq[N];
inline int Id(char x) { return isdigit(x) ? x - '0' : x - 'A' + 10;}
int ufs[M];
int find(int x) { return ufs[x] == x ? x : ufs[x] = find(ufs[x]);}
inline void Merge(int x,int y) { ufs[find(x)] = find(y);}
int tot,X[N],Y[N]; // X[i] < Y[i]
vector<int> can[Sz]; // 预处理把 T 放在 S 中的最后是否可行,3^V * n
vector<int> con[M];
bool dig[Sz][10];
inline bool buildlim() {
for(int i = 0;i < M;i++) ufs[i] = i;
tot = 0;
for(int i = 1;i <= n;i++) if(opp[i] != '=') {
for(int j = 0;j < nq[i];j++)
Merge(Id(s1[i][j]),Id(s2[i][j]));
++tot,X[tot] = Id(s1[i][nq[i]]),Y[tot] = Id(s2[i][nq[i]]);
if(opp[i] == '>') swap(X[tot],Y[tot]);
} else for(int j = 0;j < s1[i].size();j++) Merge(Id(s1[i][j]),Id(s2[i][j]));
for(int i = 0;i < M;i++) con[i].clear();
for(int i = 1;i <= tot;i++) if(find(X[i]) == find(Y[i])) return false;
for(int i = 0;i < 10;i++)
for(int j = i + 1;j < 10;j++) if(find(i) == find(j)) return false;
for(int i = 1;i <= tot;i++) {
X[i] = find(X[i]);Y[i] = find(Y[i]);
con[X[i]].push_back(Y[i]),con[Y[i]].push_back(X[i]);
}
return true;
}
int bel[M],idcnt;
int dp[10][Sz];
inline int Calc0(vector<int> V) {
int siz = V.size();idcnt = 0;
// printf("siz:%d\n",siz);
for(int i = 0;i < M;i++) bel[i] = -1;
for(auto i : V) bel[i] = idcnt++;
for(int i = 0;i < (1 << siz);i++) {
int cnt = __builtin_popcount(i);
can[i].clear();can[i].resize(1 << cnt);
for(int j = i,d = 0;j;j = (j - 1) & i,++d) {
can[i][d] = true;
for(int k = 1;k <= tot;k++)
if(bel[X[k]] != -1 && bel[Y[k]] != -1 && (j >> bel[X[k]] & 1) && ((i - j >> bel[Y[k]]) & 1))
can[i][d] = false;
}
}
for(int i = 0;i < (1 << siz);++i) {
for(int j = 0;j < 10;j++) dig[i][j] = true;
int c1 = -1,c2 = -1;
for(int j = 0;j < 10;j++) if(bel[find(j)] != -1 && (i >> bel[find(j)] & 1)) {
if(c1 == -1) c1 = j;
else c2 = j;
}
for(int k = 1;k <= tot;k++)
if(bel[X[k]] != -1 && bel[Y[k]] != -1 && (i >> bel[X[k]] & 1) && (i >> bel[Y[k]] & 1)) {
for(int j = 0;j < 10;j++) dig[i][j] = false;
break;
}
if(c2 != -1) for(int j = 0;j < 10;j++) dig[i][j] = false;
else if(c1 != -1) for(int j = 0;j < 10;j++) if(j != c1) dig[i][j] = false;
}
for(int i = 0;i <= 9;i++) for(int j = 0;j < (1 << siz);++j) dp[i][j] = 0;
for(int i = 0;i < (1 << siz);i++) if(dig[i][0]) dp[0][i] = 1;
for(int i = 1;i <= 9;i++)
for(int j = 0;j < (1 << siz);++j) {
Plus(dp[i][j],dp[i - 1][j]);
for(int k = j,d = 0;k;k = (k - 1) & j,++d)
if(can[j][d] && dig[k][i]) Plus(dp[i][j],dp[i - 1][j - k]);
}
return dp[9][(1 << siz) - 1];
}
vector<int> cv;
bool usd[M];
void dfs0(int x) {
usd[x] = true;cv.push_back(x);
for(auto y : con[x])
if(!usd[y]) dfs0(y);
}
inline int Calcnq() {
int mul = 1;
if(!buildlim()) return 0;
// printf("nq1:%d,%d,%d,%d\n",nq[1]);
for(int i = 0;i < M;i++) usd[i] = 0;
for(int i = 0;i < 36;i++) if(find(i) == i && !usd[i]) {
cv.clear();dfs0(i);//printf("i:%d\n",i);
// printf("Calc0:%d\n",Calc0(cv));
mul = 1ll * mul * Calc0(cv) % P;
}
return mul;
}
int ans = 0;
void dfsnq(int x) {
if(x == n + 1) { Plus(ans,Calcnq());return;}
if(opp[x] != '=') for(int i = 0;i < s1[x].size();i++) { nq[x] = i;dfsnq(x + 1);nq[x] = -1;}
else dfsnq(x + 1);
}
int main() {
cin >> n;
for(int i = 1;i <= n;i++) {
string limit;
cin >> limit;
int len = limit.size(),pos = 0;
for(int j = 0;j < len;j++) if(!isdigit(limit[j]) && !isalpha(limit[j])) { opp[i] = limit[j];pos = j;break;}
s1[i] = limit.substr(0,pos);
s2[i] = limit.substr(pos + 1,(int)limit.size() - pos - 1);
while(s1[i].size() < s2[i].size()) s1[i] = '0' + s1[i];
while(s1[i].size() > s2[i].size()) s2[i] = '0' + s2[i];
// printf("s1:%s,s2:%s\n",s1[i].c_str(),s2[i].c_str());
}
dfsnq(1);
cout << ans << endl;
return 0;
}
//start: 2024.1.3 15:09
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3512kb
input:
1 P=NP
output:
766136394
result:
ok single line: '766136394'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
1 2000CNY>3000USD
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3536kb
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: 3508kb
input:
2 BC>DD BD<EA
output:
27271695
result:
ok single line: '27271695'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
3 CE>ED CC>BA BB<AC
output:
426829091
result:
ok single line: '426829091'
Test #6:
score: 0
Accepted
time: 80ms
memory: 4456kb
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: 196ms
memory: 4368kb
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: 543ms
memory: 4460kb
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: 6946ms
memory: 4652kb
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: 7083ms
memory: 4620kb
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: 3520kb
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: 3740kb
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: 8ms
memory: 3604kb
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: 18ms
memory: 3556kb
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: 7ms
memory: 3648kb
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: 3568kb
input:
4 K1TVV0>TOB4QTH E5U5C9>QGDEGU Q9LW3SK>LWFRP DXUQM=V4N4
output:
467745652
result:
ok single line: '467745652'
Test #17:
score: 0
Accepted
time: 2ms
memory: 3624kb
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: 3708kb
input:
1 ABCDEFGHIJKLMNOPQRSTUVWX>BCDEFGHIJKLMNOPQRSTUVWXY
output:
835948861
result:
ok single line: '835948861'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
3 A=A 00109=109 XX=Z
output:
276262510
result:
ok single line: '276262510'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3892kb
input:
2 ABCDEFGHIJKL=CDEFGHIJKLMN OPQRSTUVWXYZ=RSTUVWXYZOPQ
output:
100000
result:
ok single line: '100000'
Test #21:
score: 0
Accepted
time: 29ms
memory: 3692kb
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: 3636kb
input:
0
output:
673653469
result:
ok single line: '673653469'
Test #23:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
3 AB<CD AC<BD AD<BC
output:
219041723
result:
ok single line: '219041723'
Extra Test:
score: -3
Extra Test Failed : Time Limit Exceeded on 4
input:
10 A<BCD E<FGH I<JKL M<NOP Q<RST U<VWX Y<ZAB C<DEF G<HIJ K<LMN