题意node
给定一个字符串\(s\),长度为\(n\),一根项链为一个环,定义一根项链为\(k-beautiful\),则该项链顺时针转\(k\)下后与原项链相等,给出\(k\),请构造一根最长的\(k-beautiful\)项链,项链由\(s\)中的一些字符组成,长度为\(1\)的项链和组成字符所有相等的项链知足任意\(k\)ios
首先最小的答案是最大的字符个数,而后考虑项链中字符不全相等的状况,一根项链转\(k\)下不变,则\(k\)的某个因子可能也知足,不妨设为\(j\),则\(j-beautiful\)的项链也知足\(k-beautiful\),咱们枚举因子\(j\),而后找到能够构造出的最长项链,设项链为字符串\(t\),注意到\(j-beautiful\)的项链有\(t[1]=t[j+1],\cdots ,t[j-1]=t[2*j-1]\),注意到这个等式能够继续下去,那么咱们要考虑项链的节数,每节有\(j\)个字符,那么要找到能够知足的最大节数,最长的\(j-beautiful\)项链即为:最大节数乘以\(j\),这个最大节数具备二分性质,二分便可c++
#pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define ll long long #define int ll #define ls st<<1 #define rs st<<1|1 #define pii pair<int,int> #define rep(z, x, y) for(int z=x;z<=y;++z) #define com bool operator<(const node &b) using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); const int maxn = (ll) 5e6 + 5; const int mod = 998244353; const int inf = 0x3f3f3f3f; int T = 1; int num[26]; bool check(int each, int jie) { rep(i, 0, 25) { each -= num[i] / jie; if (each <= 0) return true; } return false; } void solve() { int n, k; cin >> n >> k; string s; cin >> s; int ans = 1; rep(i, 0, 25)num[i] = 0; rep(i, 0, s.size() - 1)++num[s[i] - 'a'], ans = max(ans, num[s[i] - 'a']); vector<int> v; for (int i = 2; i <= k; ++i) { if (k % i == 0) v.push_back(i); } for (auto &each:v) { int l = 1, r = n / each; while (l <= r) { int mid = (l + r) >> 1; if (check(each, mid)) ans = max(ans, mid * each), l = mid + 1; else r = mid - 1; } } cout << ans << '\n'; } signed main() { start; cin >> T; while (T--) solve(); return 0; }