CodeForces 1367E Necklace Assembly

题意

给定一个字符串\(s\),长度为\(n\),一根项链为一个环,定义一根项链为\(k-beautiful\),则该项链顺时针转\(k\)下后与原项链相等,给出\(k\),请构造一根最长的\(k-beautiful\)项链,项链由\(s\)中的一些字符组成,长度为\(1\)的项链和组成字符全部相等的项链满足任意\(k\)

首先最小的答案是最大的字符个数,然后考虑项链中字符不全相等的情况,一根项链转\(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\),这个最大节数具有二分性质,二分即可

#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;
}
上一篇:.netCore各种通过反射加载程序集的方法的总结


下一篇:Eclipse中Project的属性Deployment Assembly(部署程序集)消失了,不存在了,去哪儿了