/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 388.0 KiB
#2 Accepted 26ms 532.0 KiB
#3 Accepted 28ms 588.0 KiB
#4 Wrong Answer 21ms 568.0 KiB
#5 Accepted 22ms 532.0 KiB
#6 Accepted 29ms 588.0 KiB
#7 Accepted 26ms 532.0 KiB
#8 Accepted 28ms 588.0 KiB
#9 Accepted 27ms 584.0 KiB

Code

#include <bits/stdc++.h>

using namespace std;

// #include "debug.h"

template <const int SZ>
struct numb {
        static constexpr int M = SZ;
        int pc = 0;
        array<int, M> primes, lpf;
        bitset<M> isPrime;
        constexpr numb() {
                for (int i = 2; i < M; ++i) {
                        if (!lpf[i]) {
                                primes[pc++] = i;
                                lpf[i] = i;
                                isPrime[i] = 1;
                        }
                        for (int j = 0;
                             j < pc && 1LL * i * primes[j] < M && primes[j] <= lpf[i]; j++)
                                lpf[i * primes[j]] = primes[j];
                }
        }
        pair<int, array<int, 30>> pfact(int V) {
                array<int, 30> ret;
                int sz = 0;
                for (int p = lpf[V], cnt = 0; V > 1; p = lpf[V], cnt = 0) {
                        while (V % p == 0)
                                V /= p;
                        ret[sz++] = p;
                }
                return {sz, ret};
        }
        pair<int, array<pair<int, int>, 30>> pfact_with_frq(int V) {
                array<pair<int, int>, 30> ret;
                int sz = 0;
                for (int p = lpf[V], cnt = 0; V > 1; p = lpf[V], cnt = 0) {
                        while (V % p == 0)
                                V /= p, ++cnt;
                        ret[sz++] = {p, cnt};
                }
                return {sz, ret};
        }
};

const int N = 2e3 + 10;
numb<N> nb;

const int inf = 1e9;

void solve() {
        int n;
        cin >> n;

        string s;
        cin >> s;

        vector<int> cnt(3);
        for (int i = 0; i < n; ++i) {
                ++cnt[s[i] - 'a'];
        }

        int ans = inf;
        for (int i = 0; i <= nb.pc; ++i) {
                for (int j = 0; j <= nb.pc; ++j) {
                        int p1 = (i ? nb.primes[i - 1] : 0);
                        int p2 = (j ? nb.primes[j - 1] : 0);
                        int p3 = n - (p1 + p2);
                        if (p3 < 0 || (p3 > 0 && !nb.isPrime[p3])) {
                                continue;
                        }

                        int ansH = 0;
                        ansH += max(0, p1 - cnt[0]);
                        ansH += max(0, p2 - cnt[1]);
                        ansH += max(0, p3 - cnt[2]);

                        ans = min(ansH, ans);
                }
        }

        cout << ans << '\n';
}

int32_t main() {
        ios_base::sync_with_stdio(false);
        cin.tie(0);

        int tc = 1;
        cin >> tc;

        while (tc--) {
                solve();
        }
}

Information

Submit By
Type
Submission
Problem
P1158 Yet another Beautiful String
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 15:14:41
Judged At
2025-01-02 15:14:41
Judged By
Score
90
Total Time
29ms
Peak Memory
588.0 KiB