結果

問題 No.1171 Runs in Subsequences
ユーザー pes_magicpes_magic
提出日時 2020-08-14 22:49:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,303 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 75,832 KB
実行使用メモリ 11,012 KB
最終ジャッジ日時 2024-04-18 22:40:12
合計ジャッジ時間 1,593 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 18 ms
10,496 KB
testcase_16 AC 17 ms
10,240 KB
testcase_17 AC 18 ms
10,624 KB
testcase_18 AC 18 ms
10,752 KB
testcase_19 AC 17 ms
11,012 KB
testcase_20 AC 17 ms
10,844 KB
testcase_21 AC 18 ms
10,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int MOD = 1000000007;

long long solve(const string& S){
    const int N = S.size();
    const int maxSize = N+1;
	vector<long long> inv(maxSize);
	vector<long long> fact(maxSize);
	vector<long long> factInv(maxSize);
	for(int i=0;i<2;i++) inv[i] = fact[i] = factInv[i] = 1;
	for(int i=2;i<maxSize;i++){
		inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
		fact[i] = fact[i-1] * i % MOD;
		factInv[i] = factInv[i-1] * inv[i] % MOD;
	}
	auto comb = [&](int n, int r){
		if(n < r || r < 0) return 0LL;
		return fact[n] * factInv[n-r] % MOD * factInv[r] % MOD;
	};
    vector<vector<int>> pos(26);
    vector<long long> pow(N+1, 1);
    for(int i=1;i<=N;i++) pow[i] = 2*pow[i-1]%MOD;
    long long res = 0;
    for(int i=1;i<=N;i++) res = (res + i * comb(N, i)) % MOD;
    for(int i=0;i<N;i++) pos[S[i]-'a'].push_back(i);
    for(auto& v : pos){
        if(v.size() < 2) continue;
        long long base = pow[v[0]];
        for(int i=1;i<v.size();i++){
            long long sub = (base * pow[N-v[i]-1]) % MOD;
            res = (res + MOD - sub) % MOD;
            base = (base + pow[v[i]]) % MOD;
        }
    }
    return res;
}

int main(){
    string S;
    while(cin >> S){
        cout << solve(S) << endl;
    }
}
0