結果

問題 No.1848 Long Prefixes
ユーザー 👑 tatyamtatyam
提出日時 2021-12-23 13:10:36
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 1,528 bytes
コンパイル時間 4,380 ms
コンパイル使用メモリ 276,828 KB
実行使用メモリ 7,268 KB
最終ジャッジ日時 2023-09-11 18:02:41
合計ジャッジ時間 7,570 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
6,952 KB
testcase_01 AC 44 ms
7,068 KB
testcase_02 AC 88 ms
7,140 KB
testcase_03 AC 88 ms
7,096 KB
testcase_04 AC 43 ms
7,268 KB
testcase_05 AC 86 ms
6,820 KB
testcase_06 AC 81 ms
6,712 KB
testcase_07 AC 41 ms
6,736 KB
testcase_08 AC 81 ms
6,696 KB
testcase_09 AC 83 ms
6,692 KB
testcase_10 AC 43 ms
6,748 KB
testcase_11 AC 83 ms
6,748 KB
testcase_12 AC 81 ms
6,804 KB
testcase_13 AC 40 ms
6,872 KB
testcase_14 AC 82 ms
6,744 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 36 ms
4,640 KB
testcase_34 AC 38 ms
6,016 KB
testcase_35 AC 17 ms
4,380 KB
testcase_36 AC 34 ms
4,736 KB
testcase_37 AC 32 ms
5,508 KB
testcase_38 AC 48 ms
5,284 KB
testcase_39 AC 35 ms
4,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <atcoder/string>
#include <atcoder/modint>
using namespace std;
using Modint = atcoder::modint1000000007;

template<class T> vector<int> z_algorithm(vector<T> A){
    const int N = A.size();
    vector<int> a(N);
    {
        auto B = A;
        sort(B.begin(), B.end());
        for(int i = 0; i < N; i++) a[i] = lower_bound(B.begin(), B.end(), A[i]) - B.begin();
    }
    vector<int> ans(N);
    for(int i = 0; i < N; i++){
        const int c = a[i];
        for(int j = i; j < N; j++){
            if(a[j] == c && ans[j - i] == i) ans[j - i]++;
        }
    }
    return ans;
}
int main(){
    int N;
    cin >> N;
    vector<pair<char, int>> A(N);
    for(auto& [c, a] : A) cin >> a;
    for(auto& [c, a] : A) cin >> c;
    auto Z = atcoder::z_algorithm(vector(A.begin() + 1, A.end()));
    Z.push_back(0);
    vector<Modint> sum(N);
    sum[0] = Modint::raw(A[0].second);
    for(int i = 1; i < N; i++) sum[i] = sum[i - 1] + Modint::raw(A[i].second);
    const auto [c0, a0] = A[0];
    Modint ans = 0;
    for(int i = 0; i < N; i++) if(auto [c, a] = A[i]; c == c0){
        if(a < a0){
            ans += uint64_t(a) * (a + 1) / 2;
            continue;
        }
        ans += uint64_t(a0) * (a + a - a0 - 1) / 2;
        const int z = Z[i];
        ans += sum[z];
        if(i + 1 + z < N && A[i + 1 + z].first == A[1 + z].first){
            ans += Modint::raw(min(A[i + 1 + z].second, A[1 + z].second));
        }
    }
    cout << ans.val() << endl;
}
0