結果

問題 No.2858 Make a Palindrome
ユーザー shobonvipshobonvip
提出日時 2024-08-25 15:31:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,466 bytes
コンパイル時間 4,247 ms
コンパイル使用メモリ 265,596 KB
実行使用メモリ 15,804 KB
最終ジャッジ日時 2024-08-25 15:31:33
合計ジャッジ時間 10,421 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 572 ms
6,816 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 60 ms
6,940 KB
testcase_19 RE -
testcase_20 AC 65 ms
14,412 KB
testcase_21 AC 65 ms
14,572 KB
testcase_22 AC 37 ms
9,056 KB
testcase_23 AC 44 ms
14,420 KB
testcase_24 AC 58 ms
12,184 KB
testcase_25 AC 53 ms
11,184 KB
testcase_26 AC 33 ms
10,904 KB
testcase_27 AC 76 ms
15,580 KB
testcase_28 AC 63 ms
11,844 KB
testcase_29 AC 46 ms
14,776 KB
testcase_30 AC 76 ms
15,700 KB
testcase_31 AC 77 ms
15,764 KB
testcase_32 AC 84 ms
15,804 KB
testcase_33 AC 75 ms
15,800 KB
testcase_34 AC 74 ms
15,700 KB
testcase_35 AC 76 ms
15,704 KB
testcase_36 AC 79 ms
15,748 KB
testcase_37 AC 73 ms
15,784 KB
testcase_38 AC 74 ms
15,604 KB
testcase_39 AC 77 ms
15,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

vector< int > manacher(const string &s) {
  vector< int > radius(s.size());
  int i = 0, j = 0;
  while(i < s.size()) {
    while(i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]) {
      ++j;
    }
    radius[i] = j;
    int k = 1;
    while(i - k >= 0 && i + k < s.size() && k + radius[i - k] < j) {
      radius[i + k] = radius[i - k];
      ++k;
    }
    i += k;
    j -= k;
  }
  return radius;
}

int max_length_palindrome(string s) {
	vector<int> targ = manacher(s);
	int odd = max(targ) * 2 - 1;

	string t = "";
	rep(i,0,(int)s.size()){
		t += s[i];
		t += '$';
	}
	t.pop_back();
	int even = 0;
	vector<int> tmp = manacher(t);
	for (int i=1; i<(int)tmp.size(); i+=2){
		even = max(even, tmp[i]/2*2);
	}
	return max(odd, even);
}

void solve(){
	int n; cin >> n;
	ll m; cin >> m;
	
	string s; cin >> s;
	if (max_length_palindrome(s) >= m){
		cout << 1 << endl;
		return;
	}

	string t1 = s;
	string t2 = s;
	t2 += s;
	string t3 = s;
	t3 += s;
	t3 += s;

	if (max_length_palindrome(t1) >= m){
		cout << 2 << endl;
		return;
	}

	if (max_length_palindrome(t2) >= m){
		cout << 3 << endl;
		return;
	}

	if (max_length_palindrome(t1) == max_length_palindrome(t3)){
		cout << -1 << endl;
		return;
	}

	ll a = - max_length_palindrome(t2) + max_length_palindrome(t3);
	ll b = max_length_palindrome(t2) - a * 2;
	ll f = (m - b + a - 1) / a;
	cout << f << endl;
}

int main(){

	int t; cin >> t;
	while(t--) solve();
}

0