Submission

Status:

PPPPP--P--

Subtask/Task Score:

60/100

Score: 60

User: mantaggez

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

Language: cpp

Time: 0.038 second

Submitted On: 2026-03-20 14:38:08

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll px = 1e6;

ll n, m, ans;
bool prime[px + 5], vs[px + 5];
string s;

void permute(string &str, ll l, ll r)
{
    if(l == r) {
        ll val = stoi(str);
        if(prime[val] && !vs[val]) ans++;
        vs[val] = true;
        // cout << val << ' ';
        return ;
    }

    for(ll i=l;i<=r;i++) {
        swap(str[l], str[i]);
        permute(str, l + 1, r);
        swap(str[l], str[i]);
    }
}

void solve()
{
    ans = 0;
    cin >> s;
    memset(vs, false, sizeof(vs));
    permute(s, 0, n - 1);
    // cout << '\n';
    cout << ans << '\n';
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin >> m >> n;
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = false;
    for(ll i=2;i<=px;i++) {
        if(prime[i]) {
            for(ll j=i*i;j<=px;j+=i) {
                prime[j] = false;
            }
        }
    }

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

    return 0;
}