結果
問題 | No.2858 Make a Palindrome |
ユーザー | t98slider |
提出日時 | 2024-08-25 16:32:06 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,395 ms / 3,000 ms |
コード長 | 3,652 bytes |
コンパイル時間 | 2,417 ms |
コンパイル使用メモリ | 208,844 KB |
実行使用メモリ | 49,956 KB |
最終ジャッジ日時 | 2024-08-25 16:32:44 |
合計ジャッジ時間 | 37,903 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 58 ms
6,812 KB |
testcase_01 | AC | 396 ms
6,944 KB |
testcase_02 | AC | 367 ms
6,940 KB |
testcase_03 | AC | 368 ms
6,940 KB |
testcase_04 | AC | 362 ms
6,940 KB |
testcase_05 | AC | 356 ms
6,940 KB |
testcase_06 | AC | 193 ms
6,944 KB |
testcase_07 | AC | 196 ms
6,940 KB |
testcase_08 | AC | 191 ms
6,944 KB |
testcase_09 | AC | 193 ms
6,940 KB |
testcase_10 | AC | 374 ms
6,940 KB |
testcase_11 | AC | 379 ms
6,944 KB |
testcase_12 | AC | 401 ms
6,940 KB |
testcase_13 | AC | 376 ms
6,944 KB |
testcase_14 | AC | 380 ms
6,940 KB |
testcase_15 | AC | 874 ms
6,940 KB |
testcase_16 | AC | 910 ms
6,944 KB |
testcase_17 | AC | 876 ms
6,940 KB |
testcase_18 | AC | 872 ms
6,940 KB |
testcase_19 | AC | 879 ms
6,940 KB |
testcase_20 | AC | 1,179 ms
43,852 KB |
testcase_21 | AC | 1,222 ms
44,944 KB |
testcase_22 | AC | 607 ms
24,936 KB |
testcase_23 | AC | 1,172 ms
43,816 KB |
testcase_24 | AC | 1,057 ms
38,876 KB |
testcase_25 | AC | 908 ms
34,652 KB |
testcase_26 | AC | 838 ms
33,352 KB |
testcase_27 | AC | 1,371 ms
49,224 KB |
testcase_28 | AC | 1,029 ms
37,076 KB |
testcase_29 | AC | 1,234 ms
45,680 KB |
testcase_30 | AC | 1,378 ms
49,956 KB |
testcase_31 | AC | 1,376 ms
49,800 KB |
testcase_32 | AC | 1,385 ms
49,912 KB |
testcase_33 | AC | 1,395 ms
49,892 KB |
testcase_34 | AC | 1,383 ms
49,784 KB |
testcase_35 | AC | 1,379 ms
49,736 KB |
testcase_36 | AC | 1,386 ms
49,860 KB |
testcase_37 | AC | 1,382 ms
49,780 KB |
testcase_38 | AC | 1,355 ms
49,744 KB |
testcase_39 | AC | 1,383 ms
49,776 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; template<class T> istream& operator >> (istream& is, vector<T>& vec) { for(T& x : vec) is >> x; return is; } template<class T> ostream& operator << (ostream& os, const vector<T>& vec) { if(vec.empty()) return os; os << vec[0]; for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it; return os; } struct RollingHash{ long long BASE = 257, RMOD = (1ll<<61)-1; long long MASK31 = (1ll<<31)-1, MASK30 = (1ll<<30)-1; int n; std::vector<long long> dat, dat2, base_pow; RollingHash() {} RollingHash(std::string &s) : n(s.size()), dat(n+1), dat2(n+1), base_pow(n+1) { base_pow[0] = 1; for(int i = 0; i < n; i++){ dat[i+1] = safe_mod(internal_mul(dat[i], BASE) + s[i]); dat2.rbegin()[i+1] = safe_mod(internal_mul(dat2.rbegin()[i], BASE) + s.rbegin()[i]); base_pow[i+1] = internal_mul(base_pow[i], BASE); } } long long internal_mul(long long a, long long b){ long long au = a >> 31, ad = a & MASK31; long long bu = b >> 31, bd = b & MASK31; long long mid = ad * bu + au * bd; long long midu = mid >> 30; long long midd = mid & MASK30; return safe_mod(au * bu * 2 + midu + (midd << 31) + ad * bd); } long long safe_mod(long long x){ long long xu = x >> 61, xd = x & RMOD; long long res = xu + xd; if (res >= RMOD) res -= RMOD; return res; } long long hash(int l, int r){ long long res = dat[r] - internal_mul(dat[l], base_pow[r - l]); if(res < 0)res += RMOD; return res; } long long revhash(int l, int r){ long long res = dat2[l] - internal_mul(dat2[r], base_pow[r - l]); if(res < 0)res += RMOD; return res; } long long joint(long long lhs, long long rhs, long long rsize){ long long res = internal_mul(lhs, base_pow[rsize]) + rhs; if(res >= RMOD) res -= RMOD; return res; } }; bool para(string &s){ int l = 0, r = s.size() - 1; while(l < r){ if(s[l] != s[r]) return false; l++, r--; } return true; } 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 check(string &s){ int n = s.size(); RollingHash RH(s); auto vec = manacher(s); int ans = 0; for(int i = 0; i < n; i++) ans = max(ans, 2 * vec[i] - 1); for(int i = 1; i < n; i++){ int ok = 0, ng = min(i, n - i) + 1; while(ok + 1 < ng){ int mid = (ok + ng) / 2; if(RH.hash(i - mid, i) == RH.revhash(i, i + mid)) ok = mid; else ng = mid; } ans = max(ans, ok * 2); } return ans; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while(T--){ int n, m; string s; cin >> n >> m >> s; if(para(s)){ cout << (m + n - 1) / n << '\n'; continue; } string t; int ans = -1; vector<int> d(9); for(int i = 1; i <= 8; i++){ t += s; d[i] = check(t); if(d[i] >= m){ ans = i; break; } } if(d[8] > d[7]){ int D = d[8] - d[7]; ans = (m - d[7] + D - 1) / D + 7; } cout << ans << '\n'; } }