結果

問題 No.1171 Runs in Subsequences
ユーザー IKyoproIKyopro
提出日時 2020-08-14 22:04:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,164 bytes
コンパイル時間 2,428 ms
コンパイル使用メモリ 201,536 KB
実行使用メモリ 8,232 KB
最終ジャッジ日時 2024-04-18 21:51:15
合計ジャッジ時間 3,178 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 8 ms
6,144 KB
testcase_16 AC 8 ms
5,888 KB
testcase_17 AC 8 ms
6,016 KB
testcase_18 AC 8 ms
6,016 KB
testcase_19 AC 8 ms
8,232 KB
testcase_20 AC 9 ms
7,296 KB
testcase_21 AC 9 ms
6,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 1e9+7;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;
    int N = S.size();
    int M = 26;
    vvec<int> pos(M);
    for(int i=0;i<N;i++){
        pos[S[i]-'a'].push_back(i);
    }
    vec<mint> pow2(N+1,1);
    for(int i=1;i<=N;i++) pow2[i] = pow2[i-1]*2;

    auto calc = [&](vec<int> A){
        mint res = 0;
        int n = A.size();
        vec<mint> R(n+1);
        for(int i=n-1;i>=0;i--){
            R[i] = R[i+1]+pow2[N-1-A[i]];
        }
        for(int i=0;i<n;i++) res += pow2[A[i]]*R[i+1];
        return res;
    };

    mint ans = pow2[N-1]*N;
    for(int i=0;i<26;i++) if(!pos[i].empty()) ans -= calc(pos[i]);
    cout << ans << "\n";
}
0