Submission
Status:
PPPPP--P--
Subtask/Task Score:
60/100
Score: 60
User: mantaggez
Problemset: การเรียงสับเปลี่ยน
Language: cpp
Time: 0.052 second
Submitted On: 2026-03-20 14:42:58
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll px = 2e6;
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 = stoll(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;
}