結果

問題 No.1848 Long Prefixes
ユーザー square1001square1001
提出日時 2022-02-18 22:40:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 3,196 bytes
コンパイル時間 950 ms
コンパイル使用メモリ 98,980 KB
実行使用メモリ 8,400 KB
最終ジャッジ日時 2023-09-11 19:41:10
合計ジャッジ時間 5,567 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
8,224 KB
testcase_01 AC 58 ms
8,148 KB
testcase_02 AC 98 ms
8,208 KB
testcase_03 AC 90 ms
8,228 KB
testcase_04 AC 51 ms
8,400 KB
testcase_05 AC 154 ms
7,852 KB
testcase_06 AC 83 ms
7,672 KB
testcase_07 AC 52 ms
7,964 KB
testcase_08 AC 87 ms
7,676 KB
testcase_09 AC 88 ms
7,952 KB
testcase_10 AC 115 ms
7,896 KB
testcase_11 AC 100 ms
7,984 KB
testcase_12 AC 87 ms
7,552 KB
testcase_13 AC 42 ms
7,876 KB
testcase_14 AC 88 ms
7,936 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 38 ms
5,148 KB
testcase_34 AC 41 ms
6,736 KB
testcase_35 AC 30 ms
4,376 KB
testcase_36 AC 36 ms
4,924 KB
testcase_37 AC 37 ms
6,204 KB
testcase_38 AC 51 ms
5,740 KB
testcase_39 AC 37 ms
5,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef CLASS_MODINT
#define CLASS_MODINT

#include <cstdint>

template <std::uint32_t mod>
class modint {
private:
	std::uint32_t n;
public:
	modint() : n(0) {};
	modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {};
	static constexpr std::uint32_t get_mod() { return mod; }
	std::uint32_t get() const { return n; }
	bool operator==(const modint& m) const { return n == m.n; }
	bool operator!=(const modint& m) const { return n != m.n; }
	modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
	modint operator+(const modint& m) const { return modint(*this) += m; }
	modint operator-(const modint& m) const { return modint(*this) -= m; }
	modint operator*(const modint& m) const { return modint(*this) *= m; }
	modint inv() const { return (*this).pow(mod - 2); }
	modint pow(std::uint64_t b) const {
		modint ans = 1, m = modint(*this);
		while (b) {
			if (b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}
};

#endif // CLASS_MODINT

#include <random>
std::random_device rd;
bool isprime(long long x) {
	if (x <= 1) return false;
	for (int i = 2; 1LL * i * i <= x; i++) {
		if (x % i == 0) {
			return false;
		}
	}
	return true;
}
int generate_random_prime(int L, int R) {
	std::uniform_int_distribution<int> p(L, R - 1);
	int answer;
	do {
		answer = p(rd);
	} while(!isprime(answer));
	return answer;
}

#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
using mint = modint<1000000007>;

int main() {
	// step #1. read input
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	string S;
	cin >> S;
	
	// step #2. make rolling hash
	const int P = generate_random_prime(1000000000, 1500000000);
	const int base = 311;
	vector<int> val(N), pw(N + 1), hashval(N + 1);
	for (int i = 0; i < N; i++) {
		val[i] = ((S[i] - 'a') + A[i] * 26LL) % P;
	}
	pw[0] = 1;
	for (int i = 0; i < N; i++) {
		pw[i + 1] = 1LL * pw[i] * base % P;
		hashval[i + 1] = (1LL * hashval[i] * base + val[i]) % P;
	}
	function<int(int, int)> gethash = [&](int l, int r) {
		return (hashval[r] - 1LL * hashval[l] * pw[r - l] % P + P) % P;
	};
	
	// step #3. calculate cumulative sum
	vector<long long> SA(N + 1);
	for (int i = 0; i < N; i++) {
		SA[i + 1] = SA[i] + A[i];
	}
	
	// step #4. calculation
	mint answer = 0;
	for (int i = 0; i < N; i++) {
		if (S[i] == S[0] && A[i] >= A[0]) {
			int l = 1, r = N - i + 1;
			while (r - l > 1) {
				int m = (l + r) / 2;
				if (gethash(1, m) == gethash(i + 1, i + m)) {
					l = m;
				}
				else {
					r = m;
				}
			}
			if (l == N - i) {
				answer += SA[l];
			}
			else if (S[l] == S[i + l]) {
				answer += SA[l] + min(A[l], A[i + l]);
			}
			else {
				answer += SA[l];
			}
			answer += mint(1LL * A[0] * (A[0] - 1) / 2);
			answer += mint(A[0]) * (A[i] - A[0]);
		}
		if (S[i] == S[0] && A[i] < A[0]) {
			answer += mint(1LL * A[i] * (A[i] + 1) / 2);
		}
	}
	
	// step #5. output the answer
	cout << answer.get() << endl;
	
	return 0;
}
0