Submission

Status:

PPPPPxxPxx

Subtask/Task Score:

60/100

Score: 60

User: iij

Problemset: การเรียงสับเปลี่ยน

Language: cpp

Time: 0.005 second

Submitted On: 2025-10-15 23:30:43

#include <bits/stdc++.h>

using namespace std;
bool isNotPrime[10000002];

unsigned strToInt(string s) {
    unsigned n = 0, npow = 1;
    for (int i = s.length()-1; i >= 0; i--) {
        n += (s[i]-'0') * npow;
        npow *= 10;
    }
    return n;
}

int main() {
    vector<int> ans;
    int m, n;
    cin >> m >> n;
    unsigned maxN = strToInt(string(n, '9'));

    for (auto i = 2; i <= maxN; i++) {
        if (!isNotPrime[i]) {
            for (auto j = i*i; j <= maxN; j+=i) {
                isNotPrime[j] = 1;
            }
        }
    }

    while(m--) {
        unsigned c = 0;
        string s;
        cin >> s;

        sort(s.begin(), s.end());

        do if (!isNotPrime[strToInt(s)]) c++;
        while (next_permutation(s.begin(), s.end()));
        
        ans.emplace_back(c);
    }

    for (auto i : ans) cout << i << "\n";
}