Source Code
/// Frail the skin is dry and pale
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;

#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

using ll = long long;
using ii = pair<int, int>;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define pb push_back
#define F first
#define S second
#define f(i, a, b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define mp make_pair

const int N = 5e4 + 5, LG = 18, MOD = 31607;
const double PI = acos(-1);
void doWork() {

    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<int> cnt(26);
    f(i,0,n)cnt[s[i] -'a'] += 1;
    bool ok = true;
    f(i,0,26)ok &= cnt[i] % k == 0;
    if(ok) {
        cout << "1\n";
        string str = "";
        f(i,0,26)str += string(cnt[i] / k, 'a' + i);
        f(i,0,k)cout << str;
        cout << '\n';
    }   else {
        bool ok = true;
        f(i,0,26) {
//            cout << cnt[i] << ' ' << cnt[i]/k << ' ' << cnt[i]%k << endl;
            if(cnt[i]/k!=cnt[i]%k&&cnt[i]%k!=0) {
                ok &= (cnt[i] % k) < (cnt[i] / k);
            }
        }

        if(ok) {
            string str = "";
            int sum = 0;
            f(i,0,26)
                if(cnt[i]%k!=0) {
                    str += string(cnt[i]%k,'a' + i);
                    int x = cnt[i] % k;
                    cnt[i] -= x;
                    cnt[i] -= x * k;
                }
            f(i,0,26)str += string(cnt[i] / k, 'a' + i);
            cout << "2\n";
            f(i,0,k)
                cout << str;
            cout << str.substr(0,n-sum) << "\n";
        }   else {
            cout << "3\n";
        }

    }

}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE
    int t = 1;
//    cin >> t;
    while (t--) {
        doWork();
    }


    return 0;
}
Copy
Repeating Strings DeadlyPillow
GNU G++17
0 ms
388 KB
Wrong Answer