結果

問題 No.1848 Long Prefixes
ユーザー 沙耶花沙耶花
提出日時 2022-02-18 22:32:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 2,992 bytes
コンパイル時間 4,362 ms
コンパイル使用メモリ 266,184 KB
実行使用メモリ 13,152 KB
最終ジャッジ日時 2023-09-11 19:34:41
合計ジャッジ時間 7,788 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
13,152 KB
testcase_01 AC 51 ms
13,052 KB
testcase_02 AC 97 ms
13,108 KB
testcase_03 AC 92 ms
13,096 KB
testcase_04 AC 49 ms
13,060 KB
testcase_05 AC 116 ms
12,712 KB
testcase_06 AC 87 ms
12,652 KB
testcase_07 AC 49 ms
12,796 KB
testcase_08 AC 86 ms
12,676 KB
testcase_09 AC 89 ms
12,776 KB
testcase_10 AC 73 ms
13,076 KB
testcase_11 AC 93 ms
13,012 KB
testcase_12 AC 86 ms
12,580 KB
testcase_13 AC 46 ms
12,656 KB
testcase_14 AC 91 ms
13,016 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 39 ms
7,340 KB
testcase_34 AC 42 ms
11,004 KB
testcase_35 AC 23 ms
5,120 KB
testcase_36 AC 37 ms
7,300 KB
testcase_37 AC 35 ms
9,048 KB
testcase_38 AC 51 ms
8,372 KB
testcase_39 AC 38 ms
7,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define Inf 1000000001

struct rolling_hash{

	long long t_hash;
	static vector<long long> power;
	static const long long MOD = (1LL<<61)-1;
	static const long long b = 123456;
	int sz;
	
	rolling_hash(){		
		sz = 0;
		t_hash = 0;
	}
	
	rolling_hash(long long c){
		sz = 1;
		t_hash = b*c;
	}

	long long mul(__int128 x,__int128 y){
		__int128 t = x*y;
		t = (t>>61) + (t&MOD);
		
		if(t>=MOD)t -= MOD;
		return t;
	}
	
	long long get_pow(int sz){
		if(power.size()>sz)return power[sz];
		
		while(power.size()<=sz){
			if(power.size()==0)power.push_back(1);
			else power.push_back(mul(power.back(),b));
		}
		return power.back();
		
	}
	
	rolling_hash &operator+=(const rolling_hash &another){
		
		(*this).t_hash = mul((*this).t_hash,get_pow(another.sz));
		(*this).t_hash += another.t_hash;
		if((*this).t_hash>=MOD)(*this).t_hash -= MOD;
			
		(*this).sz += another.sz;
		
		return (*this);
	}
	
	rolling_hash operator+(const rolling_hash &another)const{
		return (rolling_hash(*this)+=another);
	}
	
	rolling_hash &operator-=(const rolling_hash &another){

		(*this).t_hash += MOD - mul(another.t_hash,get_pow((*this).sz-another.sz));
		if((*this).t_hash>=MOD)(*this).t_hash -= MOD;
			
		(*this).sz -= another.sz;

		return (*this);
	}
	
	rolling_hash operator-(const rolling_hash &another)const{
		return (rolling_hash(*this)-=another);
	}
	
	bool operator<(const rolling_hash &another)const{
		if((*this).t_hash!=another.t_hash)return (*this).t_hash<another.t_hash;
		return (*this).sz<another.sz;
	}
	
	bool operator==(const rolling_hash &another)const{
		return ((*this).t_hash==another.t_hash && (*this).sz==another.sz);
	}

	
};

vector<long long> rolling_hash::power;

mint sum(long long n){
	mint ret = n;
	ret *= n+1;
	ret /= 2;
	return ret;
}

int main(){
	
	int n;
	cin>>n;
	
	vector<long long> a(n);
	rep(i,n)cin>>a[i];
	string s;
	cin>>s;
	
	{
		
		vector<long long> b;
		string t = "";
		rep(i,n){
			if(t.size()==0 || t.back()!=s[i]){
				b.push_back(a[i]);
				t += s[i];
			}
			else{
				b.back() += a[i];
			}
		}
		a = b;
		s = t;
		n = a.size();
	}
	vector<rolling_hash> R(n+1);
	rep(i,n){
		long long t = s[i];
		t *= a[i];
		R[i+1] = rolling_hash(t);
		R[i+1] = R[i] + R[i+1];
	}
	vector<long long> SS(n+1,0);
	rep(i,n){
		SS[i+1] = SS[i] + a[i];
	}
	mint ans = 0;
	rep(i,n){
		if(s[i] != s[0])continue;
		
		if(a[i] < a[0]){
			ans += sum(a[i]);
		}
		else{
			ans += sum(a[0]);
			ans -= a[0];
			ans += a[0] * (a[i] - a[0]);
			
			int ok = 1,ng = n+1;
			while(ng-ok>1){
				int mid = (ok+ng)/2;
				if(i+mid>n)ng = mid;
				else if(R[mid] - R[1] == R[i+mid] - R[i+1])ok = mid;
				else ng=  mid;
			}
			mint temp = a[0] + SS[ok] - SS[1];
			if(i+ok<n){
				if(s[i+ok]==s[ok])ans += min(a[i+ok],a[ok]);
			}
			ans += temp;
		}
	}
	cout<<ans.val()<<endl;
	
	
	
	return 0;
}
0