QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#415849#6562. First Lastskittles1412#AC ✓1ms3760kbC++173.0kb2024-05-21 11:30:142024-05-21 11:30:14

Judging History

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

  • [2024-05-21 11:30:14]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:3760kb
  • [2024-05-21 11:30:14]
  • 提交

answer

#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

using A = array<array<int, 3>, 3>;

map<pair<int, A>, bool> memo;

bool dp(int ind, A arr) {
    auto [it, inserted] = memo.insert({{ind, arr}, false});
    bool& ans = it->second;
    if (!inserted) {
        return ans;
    }
    for (int i = 0; i < 3; i++) {
        if (arr[ind][i]) {
            arr[ind][i]--;
            ans |= !dp(i, arr);
            arr[ind][i]++;
        }
    }
    return ans;
}

A norm(A arr) {
    for (int i = 0; i < 3; i++) {
        arr[i][i] %= 2;
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < i; j++) {
            int sub = min(arr[i][j], arr[j][i]);
            arr[i][j] -= sub;
            arr[j][i] -= sub;
        }
    }
    {
        int sub = min({arr[0][1], arr[1][2], arr[2][0]});
        sub = max(0, sub - 2);
        sub &= ~1;
        arr[0][1] -= sub;
        arr[1][2] -= sub;
        arr[2][0] -= sub;
    }
    {
        int sub = min({arr[1][0], arr[2][1], arr[0][2]});
        sub = max(0, sub - 2);
        sub &= ~1;
        arr[1][0] -= sub;
        arr[2][1] -= sub;
        arr[0][2] -= sub;
    }
    return arr;
}

bool solve(int u, A arr) {
    return dp(u, norm(arr));
}

void solve() {
    int n;
    cin >> n;
    string iarr[n];
    for (auto& a : iarr) {
        cin >> a;
    }
    vector<pair<int, int>> arr;
    {
        vector<char> v;
        for (auto& a : iarr) {
            v.push_back(a[0]);
            v.push_back(a.back());
        }
        sort(begin(v), end(v));
        v.erase(unique(begin(v), end(v)), end(v));

        auto comp = [&](char c) -> int {
            return int(lower_bound(begin(v), end(v), c) - begin(v));
        };

        for (auto& a : iarr) {
            arr.emplace_back(comp(a[0]), comp(a.back()));
        }
    }

    A carr {};
    for (auto& [x, y] : arr) {
        carr[x][y]++;
    }

    int cache[3][3];
    memset(cache, -1, sizeof(cache));

    auto go = [&](int x, int y) -> bool {
        int& ans = cache[x][y];
        if (ans != -1) {
            return ans;
        }

        carr[x][y]--;
        ans = !solve(y, carr);
        carr[x][y]++;
        return ans;
    };

    int ans = 0;
    for (auto& [x, y] : arr) {
        ans += go(x, y);
    }
    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
attic
climb
alpha

output:

2

result:

ok single line: '2'

Test #2:

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

input:

22
agora
alpha
antic
aorta
apnea
arena
aroma
attic
basic
blurb
china
circa
civic
climb
cobra
cocoa
comic
comma
conic
crumb
cubic
cynic

output:

6

result:

ok single line: '6'

Test #3:

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

input:

3
ccabaabbba
acbbbacccb
ccccccacba

output:

1

result:

ok single line: '1'

Test #4:

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

input:

11
mraa
myga
vtwm
mala
vvgm
atvv
vusm
mznv
avea
atfv
amgv

output:

7

result:

ok single line: '7'

Test #5:

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

input:

1000
vznwivhoprv
pvzcjyruxv
phykv
vozinczup
vbp
vgjxfotuhzhobfp
pbv
vygphslv
vpnqrfep
vzrphpfggoqcgv
vgdjmzuckyedpnp
vatmaxfp
ppnmmwtqmv
paawwp
pspoycv
vcjthv
ppcvagp
pteiqdonkklp
vav
vcsxv
priqwbalidav
vinp
phqeijnqkgbxv
pwvmbivmgvouv
vwzhndzmqpcwkmp
pyzlv
vtvblfov
vrdv
vzrfmjdnaruwp
pfup
pwzv
vwyv...

output:

478

result:

ok single line: '478'

Test #6:

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

input:

1000
rlp
pgscawiivpjndlp
pxr
ruzutr
pqdhfniemqvvr
rvar
rp
prarwp
pkuinxempbmear
rupxuanwlvawmr
pp
rzfvr
rcsxoyirp
pgpilljr
pogeizvqlr
rajolbgzswur
rcap
ryzqymwr
rrpp
rbxtr
rfldblbesyhqr
rjidcekyrir
pqrycftr
rftwgmmakykkmep
pehkxkayuip
pguxsgtuikvp
rtxmynvoolrp
pzmsljlpcr
phcfdr
plaop
rydojaccp
pzfr
...

output:

246

result:

ok single line: '246'

Test #7:

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

input:

1000
ffn
fgikjn
fwtakdbjn
fsjnfzzn
feqmuasubirsf
faypin
fcsdhfjvncfzsn
nbcmvxemkan
ngoqtaaon
nlovkxxrbn
fhdqkltoxvif
fxpnqbguftsrhff
fyniukvsmlf
fibwbqckjpojglf
nwcvf
fxgeivnakggcn
nn
fkghlusecf
neuwdafrkzptwf
fcxn
ff
nlkxtn
fdlsijtpbcjjcbn
frlcjjnpsn
ntyabhf
nvf
fdlmlhebidazhn
fjn
fstxgnmfof
fqhusf...

output:

496

result:

ok single line: '496'

Test #8:

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

input:

15
adb
bdc
cda
aeb
bec
cea
afb
bfc
cfa
agb
bgc
cga
ahb
bhc
cha

output:

15

result:

ok single line: '15'

Test #9:

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

input:

1
alpha

output:

1

result:

ok single line: '1'

Test #10:

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

input:

3
alpha
beta
gamma

output:

1

result:

ok single line: '1'

Test #11:

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

input:

2
alpha
beta

output:

1

result:

ok single line: '1'

Test #12:

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

input:

20
ccbccaabaa
aaccbbbbab
bcbcbccabc
bbbabcbbcc
ccaaccabca
acabbbccab
baacbcbccc
bcbbabbbbc
bccbbbbabc
bacaccbcac
cbbabbbaca
ccaaacbaaa
acbbbaccab
bcaacaaacc
aabbaabcbb
bbbabbbbcc
aabcaaabcb
aabbbaaacb
acacaababb
abacccccbb

output:

8

result:

ok single line: '8'

Test #13:

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

input:

20
acccccbcbb
bccbccaaac
aabbcbbccb
ababcabccb
abcaabbcab
ccabbabcca
bbccaaacbc
bbabacbccc
babacbaacc
bcabaacbac
acbabaabbb
caaccbacaa
bccccbcbcc
abbbbbbcbb
bbcbaabaac
abccaabccb
cacacaccca
bcbbcbcacc
bcccaabcbc
cabaaaccca

output:

9

result:

ok single line: '9'

Test #14:

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

input:

20
bcbbbababc
baccababbc
bcccbaacbc
ccccacbaca
aaaaccabbb
cbbbacbaaa
aaabbaabcb
aacccbbacb
bcbccbcacc
caabacbcba
bcbcbbcbbc
cacccabcaa
abbababacb
aacaabbccb
abbcacaccb
caaccaacca
bcbacacaac
baccbcccbc
abbcaccabb
acbacccccb

output:

13

result:

ok single line: '13'

Test #15:

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

input:

20
babccbbbbc
accacaabcb
acacabaabb
bacbccacac
cacacaacaa
cacaaaaaca
bccbccacac
acbaccabbb
ccbacaccba
acbaaababb
ccacabcaaa
bbabbcabcc
bacbccccac
bcacaacacc
ccabcbbbaa
aaababbbab
bbbbbabbac
abbcaabbbb
bababbabac
aaabcbbacb

output:

12

result:

ok single line: '12'

Test #16:

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

input:

20
aacbaacccb
cbcbcacaaa
cacbacbaca
aabacccacb
bbbcbacbcc
cababcacaa
bbcaaacbbc
ccccacaaca
cabcbcccaa
bbcbccabbc
ccababccca
aaacbaabbb
baacabacbc
accbbcbbab
cccbccabba
cbccaaaaca
cbcabaccca
caaacbccca
acaaabccab
bbbaaabbac

output:

10

result:

ok single line: '10'

Test #17:

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

input:

20
aaacaacbcb
cccccccaca
abbabbcacb
accaacbabb
bacbaccbac
cbcbbcccba
bacbcabcac
abaabcabab
acaaacaaab
bacbbaccac
baababbbbc
caaaaabaca
abacaccacb
abbbcbccbb
cbcbcbbcaa
cabbcaabba
cbccbaccaa
ccbbcabbaa
ababababcb
abcbcbcabb

output:

9

result:

ok single line: '9'

Test #18:

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

input:

20
bcaccbabbc
caaaaccaca
acbaacabab
aaabbabbcb
ccccaccaca
bcacbaacbc
cbccacabca
ccbabbaaaa
ababcbaaab
caabacabca
bbbcaabcac
cccccababa
aabbabcccb
abaaaccbbb
bcabaabbcc
bcbbbaacac
acaacbbccb
bacabbabcc
bbcbccbbac
aacbabccbb

output:

7

result:

ok single line: '7'

Test #19:

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

input:

20
cbacccbaba
caccbccaba
abcaccacab
cbcccabaca
acccbccacb
bccacbbabc
bbacabacac
abbcccbbab
acaaaabbcb
cbbbaabaca
babbbccacc
bbabccbcbc
cabaababaa
caacbabcba
aaaaaacccb
ccaaacacaa
baccaabcac
aaaacbcbab
baacaaaabc
ccabcacaba

output:

8

result:

ok single line: '8'

Test #20:

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

input:

20
bbaccaccbc
cabcccabba
bcabbbbaac
caaacccaca
cabccbabaa
abccaaacab
ababbccabb
acccaaabab
bababbcacc
acabbacacb
accccbcaab
cabaaccbca
cccacbbcca
acabbaccab
baacccacbc
bacacabbcc
aabaaaacab
cbbabcccba
aaaacbaccb
cccbaaaaca

output:

12

result:

ok single line: '12'

Test #21:

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

input:

20
aabacacacb
abaccaccbb
acaccccbcb
cbacbcccca
cccccabbaa
bbbcbabccc
cabbccccba
babcbcabac
baccbaacbc
ccbbccbbba
aacaabbbab
bccaccacac
abbcacaccb
cbcaabacba
babbcbaacc
ccabbbbbaa
cbcbbbaaba
bcaabcbaac
bcbbbcbbbc
babccbbbac

output:

13

result:

ok single line: '13'

Test #22:

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

input:

17
hydrogen
nitrogen
helium
neon
magnesium
niobium
molybdenum
newdymnium
holmium
hafnium
neptunium
mendelevium
nobelium
hassium
meitnerium
nihonium
moscovium

output:

6

result:

ok single line: '6'

Test #23:

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

input:

10
cbabbbcabb
bacbacbcba
bcacacacba
bbabbbbbac
acaccccbac
bbbbababca
bacabccaac
aacbcaabcb
bacbabaaaa
acabcabccb

output:

3

result:

ok single line: '3'

Test #24:

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

input:

100
ccbcaccbab
bbcbcbbbca
acbbbbbcbb
bacabcbcba
aaaabcbaac
babaaabbba
bbbabbbabc
bccaaacbac
acacaccbaa
acbacbcaac
baccabcccb
bbabcccbca
acbabaaaac
bcbbcabacc
ccbbacbcbc
aaaabcbcaa
aacbbbaaaa
bbabbcacac
baabacbbcc
abaccbabab
abcbbabacb
cbbbaccbcb
bbabbcabca
bcbaacacaa
aabccaacca
bcabaaacca
acbcbacbbc...

output:

32

result:

ok single line: '32'

Test #25:

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

input:

1000
pfalakkgegzhogw
wkkuxghywruwxfe
wyejymxprfudmww
pzfhykyprexdzjp
euglzawfswzsicw
wukfsntcitmqqsp
ptedktsjhdybegp
woiewjinnixhftw
pwysrvatyqcgxcp
pyslupqwmpqluzw
ezwtyuflrtgjdle
pyecuqzysscnqvp
pualunhhvihcohe
eemgrxaolvkxmup
egvaujnrfthojvw
ekfhcvufcqcrhcw
ecqzrgboomfjcve
whxlumclfpgcanw
pfoxyrw...

output:

225

result:

ok single line: '225'

Test #26:

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

input:

30
rfiuianksyzpkqz
zkntbrwccnjcnzh
hvyhatniqoeptvh
zzwxgtvocagvdlz
hkvtotqsilzpexr
rmqjlxhiyuvtwrh
ziynrswzgzpnfnz
rntoducfmkqpaoz
rgzweicxeqajpzr
zjznfxgpfgshkbr
rmtmhwrzkenopsz
hyodaehasivoabz
hkgxlvuxonzlvgr
hcjelcmekbohgxr
zkdlkppyvchvimh
rehlcscmhbnjexr
rhhhexryikreroz
htuwcazhktbrvqr
hvnicynkr...

output:

11

result:

ok single line: '11'

Test #27:

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

input:

300
bzpjihkgvkrwzav
bssiintigbaccyv
asuzprjzymuwkfv
blajmgohbmihtwb
bwocwcqekqwuwab
azkbjbrjdnwgzhb
aqdgacdlcofzjzb
vvqgvpdtsphhqbb
vafommzgswakrub
bizlmhzwvokxezb
btwrjkdkxiaciaa
bgyfvakbcgcaima
vtkthaktjltsvtv
vmyxovrkpphmnna
bgwhfiinpdjigza
vaghbpbabwfsnub
atztqndgqetgbzv
agtepkzcnnrlfzb
ayktyzbv...

output:

196

result:

ok single line: '196'

Test #28:

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

input:

1000
drucrd
dwbdnwzffwd
dkcoffhuvxgzd
dtd
dbud
dhdjamctkfkufpd
dpolagked
dqpyleydrcbjd
diunguld
dbpxmzd
dd
dllmgjsd
dnncoucapnojd
dbydsqd
did
dgjd
dmd
dkzkjmdkd
derbbcjvtgxbgd
dbpznlrtud
dxrdhad
dqhyxdd
dgqd
dawzjid
domzfuycd
dlyed
dkunoduhamd
dlpwoaqgjuwazd
dcd
dxnvgrvxnd
drxptrhxd
duqwvybjgahd
djh...

output:

0

result:

ok single line: '0'

Test #29:

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

input:

18
aab
bac
caa
abb
bbc
cba
acb
bcc
cca
adb
bdc
cda
aeb
bec
cea
afb
bfc
cfa

output:

0

result:

ok single line: '0'

Test #30:

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

input:

1000
qqaligwjiggjcq
iagxuiizhglmi
xuikcxsrirevfi
qgcyddrpx
iriauckx
xaq
iczi
xqhfnkhgx
ihjmwpzmaplimcq
qxhadplhx
qylxcygq
xi
xansoq
injixpfhfasvbq
qiwgbci
xjgmxhtxvdq
iihmigcihi
iedrdwzgq
iyi
xhqlttzyni
qcx
irbnlx
qmcxdihujwlai
iqaq
qyhquti
qcqzoifzglwxzdq
ikjajeafi
ifgydi
xembxgcluzqq
qyesjwiix
qkx...

output:

242

result:

ok single line: '242'

Test #31:

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

input:

100
yhothsuntodvjbp
yovbflgbufdystp
plxhiphtvtvccxn
yteszfwnocvquep
yimksvuvxrmquyn
yzxolkjfnwdwcop
yvnzximnjfteqap
padrxvamipryowy
pxgkjkpddfbrarn
ykqtkdxxwzknrvp
pxewmovejzktyzn
pnnjanibbinhwzn
ylzdwvrozhyyyen
yfyfqvvuycfaxpp
nechefkhfvumkry
yvtzpokomrtusyn
pwpbgmtkpcnhmtn
niyymfgohdgdhuy
yedcxgnr...

output:

67

result:

ok single line: '67'

Test #32:

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

input:

1000
yaswiedrkfymnzk
kzcaleacmpuazhv
kfefxdppxusoqxv
vifosydgwnqpssk
ymtrrddmufmncfv
vrupdvcsmepqtwk
vjuvfllbidvbcqy
veldskdioyyzock
kdmjepctwkiixrv
ypicbokwooycocv
ybbsmccvqynbvwk
yzgsbzbhteyajfk
yyynqadclcjkcvv
kmwrrremazrcdyv
yfuauavqsoxnknv
yizrfjukexccyvk
yuodugxjnuymgwv
vvdbmcmdcejdjhk
vsesegg...

output:

360

result:

ok single line: '360'

Test #33:

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

input:

30
nduqkltyrjchrir
npqnyjhvtczrxhk
rvkhqwigvnjasln
nqxnnyxpmhbzhrr
kcrjplhrdkyyvpn
nuxuodpvfvyovsk
nzcbwoxvmuajnuk
rcrqzqtykuzgiek
rvzjffzzwapzvck
rgntauijiodjcwn
nqntaykdmrpiojr
kmbynsfeerniqpr
rszawhpanrairqk
nhjsfdswnnaqumr
kijysjjlrbwigin
njebshbbcadcbwk
ncwluvybyxhbuvr
rlxuzfjuxgziehk
rcbtttxlq...

output:

11

result:

ok single line: '11'

Test #34:

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

input:

300
ldqparrlixcrfhn
lslzwfkysvvvaqt
lklydecymfdsymn
nxxolcshfdiofxt
lkwqujbckqmigin
nfqvomhsapqexsl
njnqzhjetlnecwt
twgomlfjreikycl
lujnrxtaognjjbn
lciyasqkkkcgrln
ncwhdddgamnbgft
naprkvnqkxqjdjl
ttltucizicrybgl
trdnzylpxeujmql
nmyusbfdjyaoaft
lsdwoewrtnrxhkn
nfapnyvwftbnwyl
lvahwwkijbffrxn
tzmuasjd...

output:

107

result:

ok single line: '107'