CSES - Datatähti 2023 loppu - Results
Submission details
Task:Merkkijonot
Sender:Mahtimursu
Submission time:2023-01-21 13:50:00 +0200
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED15
#2ACCEPTED55
#3ACCEPTED30
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3details
#2ACCEPTED0.00 s1, 2, 3details
#3ACCEPTED0.00 s1, 2, 3details
#4ACCEPTED0.01 s1, 2, 3details
#5ACCEPTED0.00 s2, 3details
#6ACCEPTED0.00 s2, 3details
#7ACCEPTED0.00 s2, 3details
#8ACCEPTED0.01 s2, 3details
#9ACCEPTED0.00 s3details
#10ACCEPTED0.11 s3details
#11ACCEPTED0.28 s3details
#12ACCEPTED0.04 s3details
#13ACCEPTED0.00 s3details
#14ACCEPTED0.00 s3details
#15ACCEPTED0.01 s2, 3details
#16ACCEPTED0.08 s3details

Code

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll M = 1e9 + 7;
const int of = 250;
vector<pair<int, int>> v;

map<pair<int, pair<pair<int, int>, pair<int, int>>>, bool> vis;
map<pair<int, pair<pair<int, int>, pair<int, int>>>, int> dp;


int n;
int atot = 0;
int btot = 0;

ll cnt(int i, pair<int, int> x, pair<int, int> y) {
    if (i == n) {
        //cout << x.first << " " << x.second << " " << y.first << " " << y.second << endl;
        return x == y;
    }

    if (x.first > atot / 2 || y.first > atot / 2) return 0;
    if (x.second > btot / 2 || y.second > btot / 2) return 0;

    if (vis[{i, {x, y}}]) {
        return dp[{i, {x, y}}];
    }

    ll a = 0;

    a += cnt(i + 1, {x.first + v[i].first, x.second + v[i].second}, y);
    a += cnt(i + 1, x, {y.first + v[i].first, y.second + v[i].second});

    a = a % M;

    vis[{i, {x, y}}] = 1;
    dp[{i, {x, y}}] = a;

    return a;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> n;

    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        int a = 0;
        int b = 0;
        for (int j = 0; j < (int)s.size(); ++j) {
            if (s[j] == 'a') a++;
            else b++;
        }

        v.push_back({a, b});
        atot += a;
        btot += b;
        //cout << a << " " << b << endl;
    }

    sort(v.rbegin(), v.rend());

    cout << cnt(0, {0, 0}, {0, 0}) << "\n";

    return 0;
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
4
b
bbb
baabaabaa
aab

correct output
0

user output
0

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
8
b
bb
baa
a
...

correct output
12

user output
12

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
16
a
a
a
b
...

correct output
5040

user output
5040

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

input
16
b
b
a
a
...

correct output
0

user output
0

Test 5

Group: 2, 3

Verdict: ACCEPTED

input
5
bab
bbaaabbabbbaababbbabbabaaabaaa...

correct output
0

user output
0

Test 6

Group: 2, 3

Verdict: ACCEPTED

input
10
baabbbababbbabbaaaabab
aabaaabbbab
aaaabbabab
aab
...

correct output
2

user output
2

Test 7

Group: 2, 3

Verdict: ACCEPTED

input
20
aaaab
baaab
babb
b
...

correct output
4332

user output
4332

Test 8

Group: 2, 3

Verdict: ACCEPTED

input
100
a
b
a
b
...

correct output
433105324

user output
433105324

Test 9

Group: 3

Verdict: ACCEPTED

input
10
aaaabbabbaabbaaaabbbbabaaaabab...

correct output
0

user output
0

Test 10

Group: 3

Verdict: ACCEPTED

input
50
aaba
aaa
abbbbaaba
ababbabbabab
...

correct output
636733956

user output
636733956

Test 11

Group: 3

Verdict: ACCEPTED

input
100
ba
bbbaba
bbba
bb
...

correct output
264657218

user output
264657218

Test 12

Group: 3

Verdict: ACCEPTED

input
500
a
b
b
b
...

correct output
394045503

user output
394045503

Test 13

Group: 3

Verdict: ACCEPTED

input
2
bbbababaaaabbbaaaaaaabbabbbaab...

correct output
2

user output
2

Test 14

Group: 3

Verdict: ACCEPTED

input
1
bbbaaaabaabbbababbbbbbbbabbbaa...

correct output
0

user output
0

Test 15

Group: 2, 3

Verdict: ACCEPTED

input
100
a
a
a
a
...

correct output
538992043

user output
538992043

Test 16

Group: 3

Verdict: ACCEPTED

input
500
a
a
a
a
...

correct output
515561345

user output
515561345