結果

問題 No.1845 Long Substrings
ユーザー vjudge1vjudge1
提出日時 2024-11-19 19:17:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 345 ms / 2,000 ms
コード長 7,409 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 145,156 KB
実行使用メモリ 12,548 KB
最終ジャッジ日時 2024-11-19 19:17:52
合計ジャッジ時間 6,497 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 322 ms
9,420 KB
testcase_01 AC 24 ms
11,064 KB
testcase_02 AC 345 ms
9,164 KB
testcase_03 AC 322 ms
9,388 KB
testcase_04 AC 35 ms
11,108 KB
testcase_05 AC 290 ms
10,688 KB
testcase_06 AC 310 ms
12,116 KB
testcase_07 AC 25 ms
12,548 KB
testcase_08 AC 305 ms
11,052 KB
testcase_09 AC 309 ms
11,780 KB
testcase_10 AC 32 ms
9,092 KB
testcase_11 AC 292 ms
9,412 KB
testcase_12 AC 299 ms
11,500 KB
testcase_13 AC 27 ms
11,872 KB
testcase_14 AC 317 ms
11,712 KB
testcase_15 AC 7 ms
6,820 KB
testcase_16 AC 4 ms
6,820 KB
testcase_17 AC 7 ms
6,820 KB
testcase_18 AC 7 ms
6,816 KB
testcase_19 AC 3 ms
6,820 KB
testcase_20 AC 3 ms
6,820 KB
testcase_21 AC 2 ms
6,820 KB
testcase_22 AC 3 ms
6,816 KB
testcase_23 AC 2 ms
6,816 KB
testcase_24 AC 2 ms
6,820 KB
testcase_25 AC 2 ms
6,820 KB
testcase_26 AC 3 ms
6,820 KB
testcase_27 AC 2 ms
6,820 KB
testcase_28 AC 2 ms
6,816 KB
testcase_29 AC 2 ms
6,816 KB
testcase_30 AC 2 ms
6,816 KB
testcase_31 AC 2 ms
6,816 KB
testcase_32 AC 2 ms
6,820 KB
testcase_33 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2,tune=native")

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>
#include <chrono>

using namespace std;

#define          BIT(i,j)  (((i)>>(j))&1LL)
#define           MASK(i)  (1LL<<(i))
#define            ALL(x)  (x).begin(),(x).end()
#define             SZ(x)  (int)(x).size()
#define                fi  first
#define                se  second
#define               ull  ungsigned long long
#define                ll  long long
#define                ld  long double
#define                vi  vector<int>
#define               vvi  vector<vi>
#define              vvvi  vector<vvi>
#define               pii  pair<int,int>
#define              vpii  vector<pii>
#define             vvpii  vector<vpii>
#define REPDIS(i,be,en,j)  for(int i = (be); i<=(en); i+=j)
#define     REPD(i,be,en)  for(int i = (be); i>=(en); i--)
#define      REP(i,be,en)  for(int i = (be); i<=(en); i++)
#define              endl  "\n"
#define                MP  make_pair
#define               int  ll

//-----------------------------------------------------------------------------------------------//
inline void scan(){} template<typename F, typename... R> inline void scan(F &f,R&... r){cin>>f;scan(r...);}
inline void print(){} template<typename F, typename... R> inline void print(F f,R... r){cout<<f;print(r...);}
//-----------------------------------------------------------------------------------------------//

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//------------------------------------------------------------------------------------------------//
const  ll   LINF = 1e18;
const int    INF = 1e9;
const int    LOG = 20;
const int   MAXN = 5e5+7;
const int      N = 1e2+3;
const int    MOD = 1e9+7;
const int   BASE = 1e5;
const  ld    EPS = 1e-9;
const  ld     PI = acos(-1);
const int OFFSET = 1e3;
//------------------------------------------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
//------------------------------------------------------------------------------------------------//

/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
*/

struct Matrix{
    int a[2][2];

    Matrix(){
        a[0][0] = a[1][1] = 0;
        a[0][1] = a[1][0] = 0;
    }
};

Matrix operator*(Matrix a , Matrix b){
    Matrix c = Matrix();

    c.a[0][0] = c.a[0][1] = c.a[1][0] = c.a[1][1] = 0;

    for(int i = 0 ; i < 2 ; i++){
        for(int j = 0 ; j < 2 ; j++){
            for(int k = 0 ; k < 2 ; k++){
                // cout << i << " " << j << " " << k << " " << a.a[i][k] << " " << b.a[k][j] << endl; 
                c.a[i][j] = (c.a[i][j] + (a.a[i][k] * b.a[k][j]) % MOD) % MOD;
            }
        }
    }

    return c;
}

Matrix BinPow(Matrix& a , int b){
    if(b == 1)
        return a;
    if(b < 1)
        return Matrix();

    Matrix mid = BinPow(a , b / 2);

    if(b % 2 == 0)
        return mid * mid;
    return mid * mid * a;
}

int n;
int a[MAXN],DP[MAXN][2];
string s;
int last[26];
void solve(){
    cin >> n;
    for(int i = 1 ; i <= n ; i++)
        cin >> a[i];
    cin >> s;

    s = '*' + s;

    DP[0][1] = 1;

    for(int i = 1 ; i < s.size() ; i++){
        int ch = s[i] - 'a';
        DP[i][0] = DP[i - 1][1];
        DP[i][1] = (((DP[i][0] * 2) % MOD - DP[last[ch]][0]) % MOD + MOD) % MOD;

        // for(int j = 2 ; j <= a[i] ; j++){
        //     int tmp = DP[i][1];
        //     DP[i][1] = (((DP[i][1] * 2) % MOD - DP[i][0]) % MOD + MOD) % MOD;
        //     DP[i][0] = tmp;
        // }
        // cout << DP[i][0] << " " << DP[i][1] << endl;
        if(a[i] > 1){
            Matrix Base = Matrix();

            Base.a[0][0] = 2;
            Base.a[0][1] = -1;
            Base.a[1][0] = 1;
            Base.a[1][1] = 0;

            Base = BinPow(Base , a[i] - 1);

            Matrix A = Matrix();
            A.a[0][0] = DP[i][1];
            A.a[1][0] = DP[i][0];
            A.a[0][1] = A.a[1][1] = 0;
            /*
            2 -1
            1 0
            */

        //    cout << Base.a[0][0] << " " << Base.a[0][1] << endl;
        //    cout << Base.a[1][0] << " " << Base.a[1][1] << endl;
        //     cout << endl;
        //    cout << A.a[0][0] << " " << A.a[0][1] << endl;
        //    cout << A.a[1][0] << " " << A.a[1][1] << endl;
        //     cout << endl;

            // cout << "mul here" << endl;
            A = Base * A;
        //    cout << A.a[0][0] << " " << A.a[0][1] << endl;
        //    cout << A.a[1][0] << " " << A.a[1][1] << endl;
        //     cout << endl;

            DP[i][1] = A.a[0][0];
            DP[i][0] = A.a[1][0];
        }
        last[ch] = i;
        // cout << DP[i][0] << " " << DP[i][1] << endl;
    }

    cout << (DP[n][1] - 1 + MOD) % MOD << endl;
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #define task "a"
    if(fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    #define task1 "nothing"
    if(fopen(task1".inp","r")){
        freopen(task1".inp","r",stdin);
        freopen(task1".out","w",stdout);
    }

    int test = 1;

    while(test--){
        solve();
    }
    cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
    return 0;
}

/**  /\_/\
 *  (= ._.)
 *  / >TL \>AC
**/
0