結果

問題 No.2858 Make a Palindrome
ユーザー 沙耶花沙耶花
提出日時 2024-08-25 14:20:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 448 ms / 3,000 ms
コード長 3,164 bytes
コンパイル時間 4,735 ms
コンパイル使用メモリ 268,364 KB
実行使用メモリ 34,964 KB
最終ジャッジ日時 2024-08-25 14:20:15
合計ジャッジ時間 10,462 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 448 ms
6,816 KB
testcase_01 AC 94 ms
6,940 KB
testcase_02 AC 96 ms
6,940 KB
testcase_03 AC 94 ms
6,940 KB
testcase_04 AC 97 ms
6,940 KB
testcase_05 AC 78 ms
6,940 KB
testcase_06 AC 74 ms
6,944 KB
testcase_07 AC 72 ms
6,940 KB
testcase_08 AC 71 ms
6,940 KB
testcase_09 AC 71 ms
6,940 KB
testcase_10 AC 74 ms
6,944 KB
testcase_11 AC 71 ms
6,940 KB
testcase_12 AC 72 ms
6,940 KB
testcase_13 AC 73 ms
6,940 KB
testcase_14 AC 74 ms
6,940 KB
testcase_15 AC 66 ms
6,940 KB
testcase_16 AC 66 ms
6,944 KB
testcase_17 AC 67 ms
6,944 KB
testcase_18 AC 68 ms
6,940 KB
testcase_19 AC 66 ms
6,940 KB
testcase_20 AC 115 ms
30,872 KB
testcase_21 AC 108 ms
31,488 KB
testcase_22 AC 55 ms
17,664 KB
testcase_23 AC 104 ms
28,456 KB
testcase_24 AC 93 ms
28,516 KB
testcase_25 AC 78 ms
24,888 KB
testcase_26 AC 70 ms
21,212 KB
testcase_27 AC 122 ms
34,544 KB
testcase_28 AC 94 ms
28,060 KB
testcase_29 AC 99 ms
29,520 KB
testcase_30 AC 123 ms
34,072 KB
testcase_31 AC 110 ms
34,200 KB
testcase_32 AC 116 ms
34,328 KB
testcase_33 AC 114 ms
34,076 KB
testcase_34 AC 110 ms
33,812 KB
testcase_35 AC 113 ms
34,964 KB
testcase_36 AC 116 ms
34,588 KB
testcase_37 AC 115 ms
34,716 KB
testcase_38 AC 114 ms
34,328 KB
testcase_39 AC 112 ms
34,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct rolling_hash{

	long long t_hash;
	static inline 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(char 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);
	}

	
};
void solve(){
	long long n,m;
	cin>>n>>m;
	string S;
	cin>>S;
	S = S+S+S+S;
	vector<rolling_hash> h(n*4+1), rh(n*4+1);
	//cout<<'a'<<endl;
	rep(i,n*4){
		h[i+1] = h[i]+rolling_hash(S[i]);
		rh[i+1] = rh[i]+rolling_hash(S[n*4-i-1]);
	}
	//cout<<'b'<<endl;
	long long ans = Inf64;
	rep(i,n){
		int ok = 0,ng = n+1;
		while(ng-ok>1){
			int mid = (ok+ng)/2;
			int l = n+i;
			int r = l+1;
			l -= mid,r += mid;
			if(h[r]-h[l]==rh[n*4-l]-rh[n*4-r])ok = mid;
			else ng = mid;
		}
		if(ok==n||ok*2+1 >= m){
			long long mm = m;
			if(mm%2==0)mm++;
			long long l = i - mm/2;
			long long r = l + mm;
			if(l<0){
				long long D  =-l;
				D = (D+n-1)/n;
				l += D*n, r+=D*n;
			}
			ans = min((r+n-1)/n, ans);
		}
	}
	rep(i,n){
		if(S[i]!=S[i+1])continue;
		int ok = 0,ng = n+1;
		while(ng-ok>1){
			int mid = (ok+ng)/2;
			int l = n+i;
			int r = l+2;
			l -= mid,r += mid;
			if(h[r]-h[l]==rh[n*4-l]-rh[n*4-r])ok = mid;
			else ng = mid;
		}
		if(ok==n||ok*2+2 >= m){
			long long mm = m;
			if(mm%2==1)mm++;
			long long l = i - (mm-2)/2;
			long long r = l + mm;
			if(l<0){
				long long D  =-l;
				D = (D+n-1)/n;
				l += D*n, r+=D*n;
			}
			ans = min((r+n-1)/n, ans);
		}
	}
	if(ans==Inf64)ans = -1;
	cout<<ans<<endl;
}
int main(){
	int _t;
	cin>>_t;
	rep(_,_t)solve();
	
    return 0;
}
0