CSES - Aalto Competitive Programming 2024 - wk9 - Mon - Results
Submission details
Task:String padding
Sender:bubu2006
Submission time:2024-11-04 17:13:54 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.02 sdetails
#7ACCEPTED0.09 sdetails
#8ACCEPTED0.03 sdetails
#9ACCEPTED0.05 sdetails
#10ACCEPTED0.12 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.01 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.01 sdetails

Code

#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

string to_string(string s) {
    return '"' + s + '"';
}
 
string to_string(const char* s) {
    return to_string((string) s);
}
 
string to_string(bool b) {
    return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}
 
void debug_out() {
    cerr << endl;
}
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}
 
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif


const int mod = 1e9 + 7;

using type = int;

struct Matrix {
    vector <vector <type> > data;

    int row() const { return data.size(); }

    int col() const { return data[0].size(); }

    auto & operator [] (int i) { return data[i]; }

    const auto & operator[] (int i) const { return data[i]; }

    Matrix() = default;

    Matrix(int r, int c): data(r, vector <type> (c)) { }

    Matrix(const vector <vector <type> > &d): data(d) { }

    friend ostream & operator << (ostream &out, const Matrix &d) {
        for (auto x : d.data) {
            for (auto y : x) out << y << ' ';
            out << '\n';
        }
        return out;
    }

    static Matrix identity(long long n) {
        Matrix a = Matrix(n, n);
        while (n--) a[n][n] = 1;
        return a;
    }

    Matrix operator * (const Matrix &b) {
        Matrix a = *this;
        assert(a.col() == b.row());
        Matrix c(a.row(), b.col());
        for (int i = 0; i < a.row(); ++i)
            for (int j = 0; j < b.col(); ++j)
                for (int k = 0; k < a.col(); ++k){
                    c[i][j] += 1ll * a[i][k] % mod * (b[k][j] % mod) % mod;
                    c[i][j] %= mod;
                }
        return c;
    }

    Matrix pow(long long exp) {
        assert(row() == col());
        Matrix base = *this, ans = identity(row());
        for (; exp > 0; exp >>= 1, base = base * base)
            if (exp & 1) ans = ans * base;
        return ans;
    }
};

const int N = 1001;
const int M = 101;

int n, m;
string s;

int dfa[26][M];


void buildDFA() {
    dfa[s[0] - 'A'][0] = 1;
    for (int x = 0, j = 1; j < m; j++) {
        for (int c = 0; c < 26; c++) {
            dfa[c][j] = dfa[c][x];
        }

        dfa[s[j] - 'A'][j] = j + 1;
        x = dfa[s[j] - 'A'][x];
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit); // RTE if input wrong datatype

    cin >> n >> s;
    m = sz(s);

    buildDFA();

    Matrix path(m + 1, m + 1);
    for (int k = 0; k < 26; k++) {
        for (int i = 0; i < m; i++) {
            path[i][dfa[k][i]] += 1;
        }
    }
    path[m][m] = 26;

    Matrix ans = path.pow(n);
    cout << (ans[0][m]);
}

Test details

Test 1

Verdict: ACCEPTED

input
100
YWANYWAZYWANYWA

correct output
134837038

user output
134837038

Test 2

Verdict: ACCEPTED

input
100
EDMXEDVNEDMXED

correct output
642715950

user output
642715950

Test 3

Verdict: ACCEPTED

input
100
SDARSDAWVSDARSDA

correct output
748728234

user output
748728234

Test 4

Verdict: ACCEPTED

input
100
HBDHBSHBDHB

correct output
594110560

user output
594110560

Test 5

Verdict: ACCEPTED

input
100
CUNXUYGNGNEROXVLASQB

correct output
675706202

user output
675706202

Test 6

Verdict: ACCEPTED

input
1000
LZAOLRKGLZAOLXLZAOLRKGLZAOLTLZ...

correct output
318756627

user output
318756627

Test 7

Verdict: ACCEPTED

input
1000
SUASJSUASSGDSUASJSUASGKSUASJSU...

correct output
367950233

user output
367950233

Test 8

Verdict: ACCEPTED

input
1000
NHYGNHEWNHYGNHFSNHYGNHEWNHYGNH...

correct output
849646061

user output
849646061

Test 9

Verdict: ACCEPTED

input
1000
ZOCUZOCGRFZOCUZOCOQZOCUZOCGRFZ...

correct output
32142571

user output
32142571

Test 10

Verdict: ACCEPTED

input
1000
LJZMKDTECKBXBTQQUMLGADBDNGWGPY...

correct output
26128120

user output
26128120

Test 11

Verdict: ACCEPTED

input
1
A

correct output
1

user output
1

Test 12

Verdict: ACCEPTED

input
100
AVXETIDRHKPAKRBEAAVHLOPFACULSE...

correct output
228794815

user output
228794815

Test 13

Verdict: ACCEPTED

input
3
ABC

correct output
1

user output
1

Test 14

Verdict: ACCEPTED

input
745
RRQOVUJRQBIMIQK

correct output
504765084

user output
504765084

Test 15

Verdict: ACCEPTED

input
666
ABCABCDABCABCX

correct output
920654188

user output
920654188

Test 16

Verdict: ACCEPTED

input
6
AA

correct output
2212651

user output
2212651

Test 17

Verdict: ACCEPTED

input
1
B

correct output
1

user output
1

Test 18

Verdict: ACCEPTED

input
1000
OMAKARAAA

correct output
408042378

user output
408042378