QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#335017 | #7401. Ternary String Revolution | Kevin5307 | TL | 951ms | 98724kb | C++20 | 3.1kb | 2024-02-22 16:19:52 | 2024-02-22 16:19:53 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
vector<string> vec;
map<string,int> Mp;
vector<int> fa;
vector<int> state;
vector<vector<int>> trans;
vector<int> inverse;
vector<string> id;
void init()
{
const int len=12,lim=4;
vec.pb("");
for(int i=1;i<=len;i++)
{
int n=sz(vec);
for(int j=0;j<n;j++)
if(sz(vec[j])==i-1)
for(int k=0;k<3;k++)
vec.pb(vec[j]+(char)(k^48));
}
for(int i=0;i<sz(vec);i++)
Mp[vec[i]]=i;
fa.resize(sz(vec));
for(int i=0;i<sz(fa);i++)
fa[i]=i;
auto connect=[&](int u,int v)
{
while(fa[u]!=u)
u=fa[u]=fa[fa[u]];
while(fa[v]!=v)
v=fa[v]=fa[fa[v]];
if(u>v) swap(u,v);
fa[v]=u;
};
for(int i=0;i<sz(vec);i++)
{
string cur=vec[i];
for(int j=0;j<sz(cur)-2;j++)
if(cur.substr(j,3)=="111")
{
string ns=cur.substr(0,j)+"20"+cur.substr(j+3);
connect(i,Mp[ns]);
}
for(int j=0;j<sz(cur)-2;j++)
if(cur.substr(j,3)=="012")
{
string ns=cur.substr(0,j)+cur.substr(j+3);
connect(i,Mp[ns]);
}
for(int j=0;j<sz(cur)-1;j++)
if(cur.substr(j,2)=="00")
{
string ns=cur.substr(0,j)+"12"+cur.substr(j+2);
connect(i,Mp[ns]);
}
for(int j=0;j<sz(cur)-1;j++)
if(cur.substr(j,2)=="22")
{
string ns=cur.substr(0,j)+cur.substr(j+2);
connect(i,Mp[ns]);
}
}
state.resize(sz(vec));
int tot=0;
for(int i=0;i<sz(vec);i++)
if(fa[i]==i&&sz(vec[i])<=lim)
state[i]=tot++;
id.resize(tot);
for(int i=0;i<sz(vec);i++)
if(fa[i]==i&&sz(vec[i])<=lim)
id[state[i]]=vec[i];
for(int i=0;i<sz(vec);i++)
{
int tmp=i;
while(fa[tmp]!=tmp) tmp=fa[tmp]=fa[fa[tmp]];
state[i]=state[tmp];
}
inverse.resize(tot);
trans.resize(tot);
for(int i=0;i<tot;i++)
{
trans[i].resize(tot);
for(int j=0;j<tot;j++)
trans[i][j]=state[Mp[id[i]+id[j]]];
}
for(int i=0;i<tot;i++)
for(int j=0;j<tot;j++)
if(!trans[i][j])
inverse[i]=j;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init();
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
vector<ll> cnt(sz(id));
vector<ll> ans(sz(id));
string s;
cin>>s;
int cur=0;
cnt[0]++;
for(int i=sz(s)-1;i>=0;i--)
{
cur=trans[state[Mp[s.substr(i,1)]]][cur];
for(int j=0;j<sz(cnt);j++)
ans[trans[cur][j]]+=cnt[j];
cnt[inverse[cur]]++;
}
while(m--)
{
string t;
cin>>t;
int st=0;
for(int i=0;i<sz(t);i++)
st=trans[st][state[Mp[t.substr(i,1)]]];
cout<<ans[st]<<'\n';
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 719ms
memory: 96888kb
input:
2 11 4 01021001020 0 1 2 012 6 3 012210 0 1 2
output:
6 3 4 0 2 4 4
result:
ok 7 tokens
Test #2:
score: 0
Accepted
time: 951ms
memory: 98360kb
input:
181890 4 2 0121 0 2 1 2 2 2 0 7 3 2102011 122 21 2 9 1 222022212 01 7 2 0122000 0 0 1 2 2 2 212 10 2 1111011000 1210 02 2 3 01 222 22 22 10 3 1121121121 21 2 1 10 4 2201210022 202 122 20 00 7 2 0102010 221222 2 1 3 2 20 212 21 5 5 21112 00101221 202 0 20 0 9 3 022200012 10 2021 0 8 2 12212200 122 1 ...
output:
1 2 1 0 4 2 4 9 5 5 1 0 1 2 1 0 0 3 3 7 0 5 1 3 1 3 0 0 0 2 1 1 1 1 0 0 7 6 6 2 2 2 2 2 1 4 4 3 2 0 1 1 0 0 0 2 0 2 1 1 1 1 3 0 0 1 3 0 3 3 7 3 3 0 0 1 1 0 1 3 1 1 5 2 1 2 1 0 7 2 2 5 5 5 5 0 3 3 0 3 3 2 0 2 3 1 0 0 0 2 3 3 3 3 3 6 1 0 0 2 1 0 0 2 1 3 2 1 2 4 4 4 4 3 3 4 4 2 5 5 0 0 1 0 1 0 0 3 3 2 ...
result:
ok 403256 tokens
Test #3:
score: 0
Accepted
time: 912ms
memory: 97556kb
input:
100000 10 1 1102212120 10 10 8 2002121021 110 1202011212 0220 220110212 0020110 1 2 2 10 1 1212222011 1100 10 2 0002020102 2 2 10 6 0012122020 2222021120 1111200 220112 2100 11 1 10 1 0111222201 1 10 2 1200020000 22 2 10 1 1012101011 0 10 3 2101122120 11000100 02200 1 10 3 2020011122 002 1 101002 10...
output:
2 1 3 2 1 2 4 4 4 1 5 5 2 1 1 1 2 5 6 6 6 4 6 3 7 4 4 2 4 2 0 10 1 6 5 0 1 6 0 10 5 0 3 6 0 5 5 0 3 3 1 9 6 2 11 11 8 8 8 0 3 6 4 7 2 1 5 4 7 3 5 4 5 5 10 3 3 2 7 7 2 7 5 8 8 3 5 3 5 5 1 9 2 4 7 4 3 3 6 1 3 4 0 8 5 1 5 0 7 5 0 10 0 6 9 6 0 3 2 6 4 2 4 8 4 3 3 3 5 3 1 3 2 3 3 0 0 0 3 1 3 0 4 11 5 3 0...
result:
ok 305998 tokens
Test #4:
score: 0
Accepted
time: 903ms
memory: 97260kb
input:
66689 15 6 102202000022210 1110102010 01100011 121201122 22 1220 12 13 3 2201202110022 00102020 1110 0 20 2 20221102122121222001 110 0 18 4 202112020100121110 011 01 022 1222 14 2 12220211012001 01021 2 20 2 22110122211101210010 20120000020 0 13 3 0200010111220 1121201 20 101 13 6 0222101202112 01 2...
output:
5 12 3 6 7 5 10 2 10 6 11 8 13 13 11 3 10 3 15 0 5 5 9 2 5 5 2 5 11 8 5 6 6 5 0 14 7 5 7 4 7 1 9 7 2 6 8 3 6 4 8 12 2 10 10 7 13 0 3 4 10 15 15 2 3 6 3 8 13 18 6 14 6 14 5 3 13 5 4 5 2 4 12 12 2 17 2 12 22 0 15 20 5 0 7 3 3 7 6 2 8 8 0 11 17 9 7 10 10 8 8 6 14 4 7 11 7 6 9 8 11 11 6 8 8 13 2 6 13 18...
result:
ok 213529 tokens
Test #5:
score: 0
Accepted
time: 886ms
memory: 97264kb
input:
13314 86 3 12121212121222011202221022122100212201100110001112210001122201211021010200022022220122 1111022102211100212110221222100011020012202020002 00112212 22021 76 8 0110010210100011200001021210200222121111000101101211101221022100222020110012 1121110112001110001011 0 010010012220212210000212 02000...
output:
146 160 162 108 159 149 123 126 126 160 160 113 139 141 158 141 112 94 68 81 32 160 62 239 264 106 77 142 161 168 142 127 142 151 184 161 166 142 168 147 126 110 168 162 92 131 130 92 109 122 60 76 67 87 76 83 106 106 110 110 110 68 62 80 68 68 63 162 152 162 175 40 103 139 172 137 138 138 153 180 1...
result:
ok 74354 tokens
Test #6:
score: 0
Accepted
time: 902ms
memory: 97668kb
input:
6644 189 5 221022222202210120010002011102220210220201121201012211221011111201001112012100210121210002212101200011012101010101022200102102120110021110100022202012222202211122011110221000200111010112000 2011212102001 02022120020101102222200022211010122000220122 10101011012 200 1 116 9 2020120200211012...
output:
768 736 760 742 780 260 283 301 314 301 287 260 314 296 373 327 389 422 510 636 600 632 669 297 277 304 289 357 347 297 357 310 356 304 614 699 667 685 594 544 689 671 689 544 405 489 489 489 417 421 409 438 445 417 430 423 410 423 414 409 430 414 430 457 466 245 271 297 284 270 296 272 250 297 255 ...
result:
ok 41602 tokens
Test #7:
score: 0
Accepted
time: 880ms
memory: 97240kb
input:
2865 436 4 2101120122110010110212212212000001002122022112002220012100121221102102221111101120111222220111222022201101101000120011221000101012001102121121101020112100212222112002100111022020211020112012210122222111212111210220111012202120202121210012112021122100110221121020110002001001002220110121200...
output:
4058 4091 4058 3684 3476 3052 3154 2568 2603 2592 2620 2522 2530 2530 2509 2646 2546 2576 2546 2543 2768 2789 1564 1564 1374 1495 1550 1475 1448 1550 1458 1550 1384 1543 1465 1550 1435 1417 1502 1406 2943 2815 3214 2871 2884 2700 3147 3951 3862 3961 3671 3827 3862 3951 3927 3828 3537 1807 1902 1912 ...
result:
ok 23119 tokens
Test #8:
score: 0
Accepted
time: 881ms
memory: 97476kb
input:
1344 921 8 1221000122102210122111011002201221200001020020022101111001110100120112100100220210210112202202201212012222001221022002120020110122010222100122200012201222202112110002102210211211220011121122102220111000122201102100201000200020121212120111110111012200021201100201020212002201110211122100100...
output:
17973 17810 17401 17649 17682 17401 17401 17992 5082 5237 5808 5585 5186 5952 4641 5808 4712 5005 5237 4641 5776 4712 4726 4973 4984 5005 4668 4641 5872 5186 4984 4712 4992 5237 5186 5872 5237 5542 5532 6259 5669 6299 5788 5532 5309 6250 5669 6259 5641 6259 5404 5542 6376 5304 5404 5729 5729 6226 56...
result:
ok 20585 tokens
Test #9:
score: 0
Accepted
time: 872ms
memory: 98384kb
input:
68 18457 396 10010220202210212110122100222210001012201020012010102002202011011100120100112012021110021002102220200001121122020011011100112222011010111222111001002112111100220020122000220211112022000100012212102102121110112121002221101010120020200200200202202021200110101220110201011011212112002111201...
output:
7090114 7098346 7101400 7090743 7096028 7090743 7092798 7103529 7098474 7098474 7103529 7098474 7098346 7101400 7106486 7098474 7090743 7090743 7095040 7090114 7101400 7101400 7088335 7098346 7102754 7090743 7103520 7090114 7091901 7095040 7092798 7110606 7098474 7110606 7102350 7103520 7098346 7093...
result:
ok 33474 tokens
Test #10:
score: 0
Accepted
time: 865ms
memory: 98060kb
input:
67 12013 40 201212120011021221202102001110010120211000120212120212220220200201121211020210002020002022210022221022200022012201201201211022101011112021022012121001112010002011022100101201020220102201222211202021011010121002111020002220100222220222112010022200201211120012020222121220220102020200022211...
output:
3009017 3007182 3006127 3007182 3009017 3006193 3008818 3006267 3008249 3006285 3005186 3010230 3006792 3006398 3003496 3008249 3006193 3004129 3007864 3009054 3006127 3005186 3009017 3005816 3009054 3006398 3006398 3004129 3009017 3006267 3006127 3006267 3005816 3006193 3008249 3006127 3009054 3008...
result:
ok 3440 tokens
Test #11:
score: 0
Accepted
time: 874ms
memory: 97544kb
input:
68 17096 2 1102222110220122201011021122001102022112022212012111012122011121212201211012112022020200022011022120101110210010001211210200011010201100101002012202021121010021202222112202102112021021201000121120020221012222022011212122101210212100011210112121111211211201201202122100110101221021101020111...
output:
6089552 6091883 6170510 6167792 6171738 6170511 7506399 7515643 7505655 7511785 8158324 8152227 8159460 8153474 8158463 8155387 8151333 8154140 8151333 8153177 8153177 8159460 8153474 8155387 8151770 8154289 8152681 8148191 8153177 8152227 8153697 8155387 8151770 5681228 5690275 5685718 5681934 5679...
result:
ok 722 tokens
Test #12:
score: 0
Accepted
time: 884ms
memory: 97956kb
input:
68 16703 5 0020111022022210221200110200202112200111012210100212020110222021202220100100012120112200002200022000111002011022120211101221102012110112212122002002111122100102101202022012120112022121010210122121201020002221010210021112210210122211112101200122221111021002212012100012201010112001222002120...
output:
5808766 5818899 5815584 5806611 5819835 7381872 7377870 7379135 7558300 7557285 7555834 7554632 7553485 7557285 7554508 7557267 7559063 7822417 3044314 3039271 3047195 6561512 6527454 6535070 6560666 6542088 6527454 6530476 6561512 6561512 6560178 6560666 6548187 6542029 6526397 6558049 4953022 4228...
result:
ok 356 tokens
Test #13:
score: 0
Accepted
time: 879ms
memory: 97964kb
input:
67 16023 2 0100201022101100022111012011211110010202021011101221100121121212221000111202022002202111002100002202120021221120211110200120110220020122100021200210012200201000110012121111001120221100000200022122200012121222001022212112012022002102210112112022000101222110012011011210200220020022120201021...
output:
5349365 5349380 2141117 2146070 2242532 2243421 4324427 6122812 4600411 4599139 4605251 4600009 3888113 3895508 2885010 3314333 5217224 3840797 2908371 2884398 7334623 7318704 5448124 5452585 3616175 3620674 7407958 7400792 7412208 6765560 2516496 6942529 6942263 4238069 4366709 2385162 2377187 7495...
result:
ok 112 tokens
Test #14:
score: 0
Accepted
time: 896ms
memory: 98724kb
input:
7 171317 1749 1201021010012111220002110211021202121101110120122022201001212100002011022010010100100111112211220022201222210111020120111211101000101221020022120020022202122101221202211022002220221110111102100220212220221122000010211002220020101112220001222021202121212222020221102201220010011110201022...
output:
611391378 611563202 611340454 611563202 611461307 611448073 611402565 611563202 611402565 611452011 611399004 611448073 611479646 611356182 611402565 611402565 611372807 611486216 611417027 611465177 611417027 611465177 611448073 611402565 611391378 611372807 611464635 611568464 611372807 611399004 ...
result:
ok 33386 tokens
Test #15:
score: 0
Accepted
time: 886ms
memory: 98176kb
input:
9 100669 486 20021002202211111121210100021100010000121121212010200011102121002021112001221020112012011012002211020000021111221022201121110212200100201210220111222011010200100011112120010210112010111022220102022211202212012020200101012210200111021110211001001100020220201001202112120202010122211122010...
output:
211126793 211121850 211126890 211118029 211104824 211113963 211134710 211190099 211126793 211173572 211104429 211126890 211104824 211173572 211102272 211122602 211104429 211192439 211170203 211126890 211106675 211102272 211118029 211118029 211116032 211116032 211116032 211118273 211190099 211113963 ...
result:
ok 3383 tokens
Test #16:
score: 0
Accepted
time: 922ms
memory: 97960kb
input:
8 101617 217 00202212112111100110211222122212021010212101111221022221212111021210001110222222021110101201102012220212212100210221120010121101210220022110221221120220212021000112012202212210020200220000010022012011122020101101002002220202102121220010221201012101112122220010001122221001010011112202220...
output:
215149130 215102858 215122328 215112932 215112367 215123339 215135127 215111721 215112367 215111688 215132464 215130608 215130608 215122279 215125756 215133427 215149130 215111721 215116626 215132464 215122328 215122328 215132464 215122279 215139171 215112367 215122867 215125756 215122867 215121865 ...
result:
ok 667 tokens
Test #17:
score: -100
Time Limit Exceeded
input:
8 198271 49 212011211121121000000020112012100111102200102010022001201021021210020002022102111101020012112202012011110122110210020212222021021110022121010010120220121022101011100112012010002022011011012212111220012211002102011012201001001000002110102110010221212020001100210112221202100122212001122020...