Submission details
Task:Merkkijonot
Sender:jlaire
Submission time:2025-11-08 01:56:56 +0200
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3, 4, 5details
#2ACCEPTED0.01 s2, 3, 4, 5details
#3ACCEPTED0.00 s1, 2, 3, 4, 5details
#4ACCEPTED0.00 s1, 2, 3, 4, 5details
#5ACCEPTED0.00 s1, 2, 3, 4, 5details
#6ACCEPTED0.00 s1, 2, 3, 4, 5details
#70.00 s1, 2, 3, 4, 5details
#8ACCEPTED0.00 s1, 2, 3, 4, 5details
#9ACCEPTED0.00 s1, 2, 3, 4, 5details
#10ACCEPTED0.00 s1, 2, 3, 4, 5details
#11ACCEPTED0.01 s2, 3, 4, 5details
#12ACCEPTED0.02 s2, 3, 4, 5details
#13ACCEPTED0.02 s2, 3, 4, 5details
#14ACCEPTED0.02 s2, 3, 4, 5details
#150.00 s2, 3, 4, 5details
#16ACCEPTED0.02 s2, 3, 4, 5details
#170.01 s2, 3, 4, 5details
#18ACCEPTED0.02 s2, 3, 4, 5details
#190.01 s3, 4, 5details
#200.03 s3, 4, 5details
#210.03 s3, 4, 5details
#220.03 s3, 4, 5details
#230.00 s3, 4, 5details
#240.04 s3, 4, 5details
#250.01 s3, 4, 5details
#260.03 s3, 4, 5details
#270.01 s4, 5details
#280.04 s4, 5details
#290.05 s4, 5details
#300.04 s4, 5details
#310.00 s4, 5details
#320.05 s4, 5details
#330.01 s4, 5details
#340.04 s4, 5details
#350.01 s5details
#360.06 s5details
#370.06 s5details
#380.05 s5details
#390.00 s5details
#400.06 s5details
#410.01 s5details
#420.05 s5details

Code

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using vll = vector<ll>;

const ll MOD = 1e9 + 7;

namespace {
// library
ll modpow(ll b, ll e) {
    ll ans = 1;
    for (; e; b = b * b % MOD, e /= 2)
        if (e & 1) ans = ans * b % MOD;
    return ans;
}

vll berlekampMassey(vll s) {
    int n = s.size(), L = 0, m = 0;
    if (!n) return {};
    vll C(n), B(n), T;
    C[0] = B[0] = 1;
    ll b = 1;
    for (int i=0; i<n; i++) {
        ++m;
        ll d = s[i] % MOD;
        for (int j=1; j<L+1; j++) d = (d + C[j] * s[i - j]) % MOD;
        if (!d) continue;
        T = C;
        ll coef = d * modpow(b, MOD - 2) % MOD;
        for (int j=m; j<n; j++) C[j] = (C[j] - coef * B[j - m]) % MOD;
        if (2 * L > i) continue;
        L = i + 1 - L;
        B = T;
        b = d;
        m = 0;
    }
    C.resize(L + 1);
    C.erase(C.begin());
    for (ll &x : C) x = (MOD - x) % MOD;
    return C;
}

template <typename T>
vector<vector<T>> prod(vector<vector<T>> &a, vector<vector<T>> &b, const ll mod) {
    assert(a.back().size() == b.size());
    int n = a.size();
    int m = a.back().size();
    vector<vector<T>> c(n, vector<T>(m));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            for (int k = 0; k < m; k++)
                c[i][j] = (c[i][j] + ((a[i][k] * b[k][j]) % mod)) % mod;
    return c;
}

template <typename T>
vector<vector<T>> fpow(vector<vector<T>> &xs, ll p, ll mod) {
    vector<vector<T>> ans(xs.size(), vector<T>(xs.size()));
    for (int i = 0; i < (int)xs.size(); i++) ans[i][i] = 1;
    for (auto b = xs; p; p >>= 1, b = prod(b, b, mod))
        if (p & 1) ans = prod(ans, b, mod);
    return ans;
}

ll linear_req(vector<vector<ll>> rec, vector<ll> first_k, ll n, const ll mod) {
    int k = first_k.size();
    if (n <= k) return first_k[n - 1];
    ll n2 = n - k;
    rec = fpow(rec, n2, mod);
    ll ret = 0;
    for (int i = 0; i < k; i++)
        ret = (ret + (rec.back()[i] * first_k[i]) % mod) % mod;
    return ret;
}
}

ll go(vector<map<ll,ll>>& dp, ll mask, ll todo, const string& X) {
    if (todo == 0) return mask&1;
    if (dp[todo].count(mask)) return dp[todo][mask];
    ll ans = 0;
    int m = X.size();
    for (char c='a'; c<='z'; c++) {
        ll m2 = 0;
        for (int i=1; i<m; i++) if (X[i]==c) {
            if (mask & (1|(1ll<<(m-i)))) {
                m2 |= 1ll << (m-i-1);
            }
        }
        if (X[0]==c) {
            m2 |= 1ll<<(m-1);
        }
        if (!m2) continue;
        ans += go(dp, m2, todo-1, X);
        ans %= MOD;
    }
    return dp[todo][mask] = ans;
}

ll solve(ll n, string X) {
    vll seed;
    ll SS=10+2*X.size();
    vector<map<ll,ll>> dp(SS+X.size());
    for (int i=0; i<SS; i++) {
        seed.push_back(go(dp, 1ll<<X.size(), X.size()+i, X));
    }
    vll ff = berlekampMassey(seed);
    int K = ff.size();
    vector<vll> mat(K, vll(K));
    for (int i=0; i<K-1; i++) mat[i][i+1] = 1;
    mat[K-1] = ff;
    reverse(mat[K-1].begin(), mat[K-1].end());
    return linear_req(mat, seed, n+1-X.size(), MOD);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll n,m; cin>>n>>m;
    assert(n>=m);
    string s; cin>>s;
    cout << solve(n, s) << '\n';
}

Test details

Test 1

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
1 1
a

correct output
1

user output
1

Test 2

Group: 2, 3, 4, 5

Verdict: ACCEPTED

input
20 20
ssxfykmuzljmwgyvldnu

correct output
1

user output
1

Test 3

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
20 10
aaaaaaaaaa

correct output
1

user output
1

Test 4

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
20 10
aabbbaaaab

correct output
1532

user output
1532

Test 5

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
20 10
aabbacbdca

correct output
1542

user output
1542

Test 6

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
20 10
ztknszhrby

correct output
3261

user output
3261

Test 7

Group: 1, 2, 3, 4, 5

Verdict:

input
20 1
a

correct output
1

user output
758911393

Feedback: Incorrect character on line 1 col 1: expected "1", got "758911393"

Test 8

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
20 10
aabbbbbbba

correct output
1689

user output
1689

Test 9

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
20 8
abxabyab

correct output
8619

user output
8619

Test 10

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
20 10
ababababab

correct output
509

user output
509

Test 11

Group: 2, 3, 4, 5

Verdict: ACCEPTED

input
100 50
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
1

user output
1

Test 12

Group: 2, 3, 4, 5

Verdict: ACCEPTED

input
100 50
bbaaabbbbbabbbbabababababbbaab...

correct output
511493117

user output
511493117

Test 13

Group: 2, 3, 4, 5

Verdict: ACCEPTED

input
100 50
addabbbbbadddccadcabaacbbbaabd...

correct output
618572722

user output
618572722

Test 14

Group: 2, 3, 4, 5

Verdict: ACCEPTED

input
100 50
rrdumiqrjewanjplbyvkaytbcyzbyl...

correct output
35126431

user output
35126431

Test 15

Group: 2, 3, 4, 5

Verdict:

input
100 1
a

correct output
1

user output
962916014

Feedback: Incorrect character on line 1 col 1: expected "1", got "962916014"

Test 16

Group: 2, 3, 4, 5

Verdict: ACCEPTED

input
100 50
aabbbbbbbbbbbbbbbbbbbbbbbbbbbb...

correct output
460606355

user output
460606355

Test 17

Group: 2, 3, 4, 5

Verdict:

input
100 23
aybabtuxaybabtuyaybabtu

correct output
342213037

user output
462895168

Feedback: Incorrect character on line 1 col 1: expected "342213037", got "462895168"

Test 18

Group: 2, 3, 4, 5

Verdict: ACCEPTED

input
100 50
ababababababababababababababab...

correct output
775006564

user output
775006564

Test 19

Group: 3, 4, 5

Verdict:

input
1000 50
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
1

user output
955328477

Feedback: Incorrect character on line 1 col 1: expected "1", got "955328477"

Test 20

Group: 3, 4, 5

Verdict:

input
1000 50
bbabaabbabbbaaaaaaaaaababaabbb...

correct output
911592620

user output
748088724

Feedback: Incorrect character on line 1 col 1: expected "911592620", got "748088724"

Test 21

Group: 3, 4, 5

Verdict:

input
1000 50
cacabdddcbdadabdcbdddbdddbaccb...

correct output
12869296

user output
758145439

Feedback: Incorrect character on line 1 col 1: expected "12869296", got "758145439"

Test 22

Group: 3, 4, 5

Verdict:

input
1000 50
tqoyadbshyehwcwaxbtbsqtaswkyet...

correct output
741984969

user output
328967865

Feedback: Incorrect character on line 1 col 1: expected "741984969", got "328967865"

Test 23

Group: 3, 4, 5

Verdict:

input
1000 1
a

correct output
1

user output
538486918

Feedback: Incorrect character on line 1 col 1: expected "1", got "538486918"

Test 24

Group: 3, 4, 5

Verdict:

input
1000 50
aabbbbbbbbbbbbbbbbbbbbbbbbbbbb...

correct output
599950419

user output
901679773

Feedback: Incorrect character on line 1 col 1: expected "599950419", got "901679773"

Test 25

Group: 3, 4, 5

Verdict:

input
1000 23
aybabtuxaybabtuyaybabtu

correct output
548809016

user output
300085475

Feedback: Incorrect character on line 1 col 1: expected "548809016", got "300085475"

Test 26

Group: 3, 4, 5

Verdict:

input
1000 50
ababababababababababababababab...

correct output
765799780

user output
105453500

Feedback: Incorrect character on line 1 col 1: expected "765799780", got "105453500"

Test 27

Group: 4, 5

Verdict:

input
1000000 50
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
1

user output
498619992

Feedback: Incorrect character on line 1 col 1: expected "1", got "498619992"

Test 28

Group: 4, 5

Verdict:

input
1000000 50
bbaababbaaabbabababbaaaaaabbaa...

correct output
514073453

user output
870367532

Feedback: Incorrect character on line 1 col 1: expected "514073453", got "870367532"

Test 29

Group: 4, 5

Verdict:

input
1000000 50
aabccabbbbabccabcdcdadbcdccdac...

correct output
438094288

user output
518717810

Feedback: Incorrect character on line 1 col 1: expected "438094288", got "518717810"

Test 30

Group: 4, 5

Verdict:

input
1000000 50
yzfzimxrxfukhlkrtaylohyuqkupsn...

correct output
905445077

user output
418587464

Feedback: Incorrect character on line 1 col 1: expected "905445077", got "418587464"

Test 31

Group: 4, 5

Verdict:

input
1000000 1
a

correct output
1

user output
558619508

Feedback: Incorrect character on line 1 col 1: expected "1", got "558619508"

Test 32

Group: 4, 5

Verdict:

input
1000000 50
aabbbbbbbbbbbbbbbbbbbbbbbbbbbb...

correct output
280596224

user output
21701558

Feedback: Incorrect character on line 1 col 2: expected "280596224", got "21701558"

Test 33

Group: 4, 5

Verdict:

input
1000000 23
aybabtuxaybabtuyaybabtu

correct output
268144314

user output
366448859

Feedback: Incorrect character on line 1 col 1: expected "268144314", got "366448859"

Test 34

Group: 4, 5

Verdict:

input
1000000 50
ababababababababababababababab...

correct output
655244665

user output
186306248

Feedback: Incorrect character on line 1 col 1: expected "655244665", got "186306248"

Test 35

Group: 5

Verdict:

input
1000000000 50
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
1

user output
825344932

Feedback: Incorrect character on line 1 col 1: expected "1", got "825344932"

Test 36

Group: 5

Verdict:

input
1000000000 50
abbbaabbaaaaabbbbabbabbaaaaaba...

correct output
911059863

user output
107116897

Feedback: Incorrect character on line 1 col 1: expected "911059863", got "107116897"

Test 37

Group: 5

Verdict:

input
1000000000 50
cbabbcaadabbcabbdbdabbbcccbdca...

correct output
994268014

user output
162697385

Feedback: Incorrect character on line 1 col 1: expected "994268014", got "162697385"

Test 38

Group: 5

Verdict:

input
1000000000 50
pehyicejeninplaczwezhahmbhwfwi...

correct output
837165971

user output
55659218

Feedback: Incorrect character on line 1 col 1: expected "837165971", got "55659218"

Test 39

Group: 5

Verdict:

input
1000000000 1
a

correct output
1

user output
740562051

Feedback: Incorrect character on line 1 col 1: expected "1", got "740562051"

Test 40

Group: 5

Verdict:

input
1000000000 50
aabbbbbbbbbbbbbbbbbbbbbbbbbbbb...

correct output
114333489

user output
128307413

Feedback: Incorrect character on line 1 col 2: expected "114333489", got "128307413"

Test 41

Group: 5

Verdict:

input
1000000000 23
aybabtuxaybabtuyaybabtu

correct output
628064772

user output
168991881

Feedback: Incorrect character on line 1 col 1: expected "628064772", got "168991881"

Test 42

Group: 5

Verdict:

input
1000000000 50
ababababababababababababababab...

correct output
802946327

user output
275244705

Feedback: Incorrect character on line 1 col 1: expected "802946327", got "275244705"