結果

問題 No.1845 Long Substrings
ユーザー kai4096_don
提出日時 2022-02-18 23:09:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 2,138 bytes
コンパイル時間 3,321 ms
コンパイル使用メモリ 231,828 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-29 09:47:37
合計ジャッジ時間 5,557 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
const long nPrime = 1000000007;
//const long nPrime = 998244353;
typedef long long ll;
const long long nMod = nPrime;
class ModInt {
    long long x;
public:
    ModInt(long long x=0) : x((x%nMod+nMod)%nMod) {}
    ModInt operator-() const { 
      return ModInt(-x);
    }
    ModInt& operator+=(const ModInt& a) {
        if ((x += a.x) >= nMod) x -= nMod;
        return *this;
    }
    ModInt& operator-=(const ModInt& a) {
        if ((x += nMod-a.x) >= nMod) x -= nMod;
        return *this;
    }
    ModInt& operator*=(const  ModInt& a) {
        (x *= a.x) %= nMod;
        return *this;
    }
    ModInt operator+(const ModInt& a) const {
        ModInt res(*this);
        return res+=a;
    }
    ModInt operator-(const ModInt& a) const {
        ModInt res(*this);
        return res-=a;
    }
    ModInt operator*(const ModInt& a) const {
        ModInt res(*this);
        return res*=a;
    }
    ModInt Pow(long long t) const {
        if (!t) return 1;
        ModInt a = Pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime nMod
    ModInt Inv() const {
        return Pow(nMod-2);
    }
    ModInt& operator/=(const ModInt& a) {
        return (*this) *= a.Inv();
    }
    ModInt operator/(const ModInt& a) const {
        ModInt res(*this);
        return res/=a;
    }

    friend ostream& operator<<(ostream& os, const ModInt& m){
        os << m.x;
        return os;
    }
};
int main() {
    long n;
    cin >> n;
    vector<long> viNum(n);
    for(long i = 0; i < n; i++){
        cin >> viNum[i];
    }
    string s;
    cin >> s;
    
    vector<ModInt> viCount(26,0);
    for(long i = 0; i < n; i++){
        long iThis = s[i]-'a';
        ModInt nSum = 0;
        for(long j = 0; j < 26; j++){
            if(iThis != j){
                nSum += viCount[j];
            }
        }
        viCount[iThis] += (nSum+1)*viNum[i];
    }
     ModInt nAns = 0;
     for(long i = 0; i < 26; i++){
         nAns += viCount[i];
     }
     cout << nAns <<endl;
    return 0;
}
0