Submission

Status:

PPPPPxxPxx

Subtask/Task Score:

60/100

Score: 60

User: foldnut

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

Language: cpp

Time: 0.010 second

Submitted On: 2025-11-13 21:14:41

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
vector<bool> p(N, 1);
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    p[0] = p[1] = 0;
    for(int i = 2;i<N;i++){
        if(p[i]){
            for(int j = i + i;j<N;j+=i){
                p[j] = 0;
            }
        }
    }
    int q, n;
    cin >> q >> n;
    while(q--){
        string s;
        cin >> s;
        vector<char> v;
        for(auto x : s) v.push_back(x);
        sort(v.begin(), v.end());
        int ans = 0;
        do{
            int t = 0;
            for(int i = 0;i<n;i++) t += (v[i] - '0') * pow(10, n - i - 1);
            if(p[t]) ++ans;
        }while(next_permutation(v.begin(), v.end()));
        cout << ans << '\n';
    }
}