Submission

Status:

PPPPP--P--

Subtask/Task Score:

60/100

Score: 60

User: august

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

Language: cpp

Time: 0.053 second

Submitted On: 2026-03-19 21:48:37

#include <bits/stdc++.h>
using namespace std;

const int mx = 1e6+5;
bool prime[mx], c;
int m,n;


void fnd(string &a, vector<bool> &vst, string &s, unordered_set<int> &ans) {
    if (a.size() >= n) {
        int check = stoi(a);
       // cout<< a<< ' ';
        if (prime[check]) return;
        ans.insert(check);
    }
    for (int i=0; i<n; i++) {
        if (vst[i]) continue;
        string tem=a+s[i];

        vst[i] = true;
        fnd(tem, vst, s, ans);
        vst[i] = false;
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin>> m>> n;

    prime[0] = prime[1] = true;
    for (int i=2; i*i<mx; i++) {
        if (prime[i]) continue;
        for (int j=i*i; j<mx; j+=i) {
            prime[j] = true;
        }
    }

    while (m--) {
        string s;
        cin>> s;

        vector<bool> vst(n, false);
        string tem = "";
        unordered_set<int> ans;
        fnd(tem, vst, s, ans);
        cout<< ans.size()<< '\n';
    }
}