Submission
Status:
-TTTTTTTTT
Subtask/Task Score:
0/100
Score: 0
User: C12
Problemset: ทางเชื่อม
Language: cpp
Time: 1.096 second
Submitted On: 2026-01-07 18:34:37
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pii pair<ll,ll>
#define puii pair<ull,ull>
#define piii pair<ll,pii>
#define tiiii tuple<ll,ll,ll,ll>
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define mt make_tuple
#define mpiiii(a,b,c) make_pair(a,make_pair(b,c));
// ll mod = 1000000007;
ll m;
string t,neww;
ll num;
const ll n = 1 * 1000 * 1000;
int8_t p[n] = {0};
ll cnt = 0;
int pos[6];
int checkpos[9];
set<ll>st;
map<string,ll>mapp;
ll power;
void recursive(int i){
if(i == m){
power = 1;
num = 0;
for(int j = m-1;j >= 0;j--){
num += (t[pos[j]] - '0') * power;
power *= 10;
}
// cout << num << '\n';
if(p[num] == 0 && st.find(num) == st.end()){
cnt++;
st.insert(num);
}
}
for(int j = 0;j < m;j++){
if(checkpos[j]) continue;
checkpos[j] = 1;
pos[i] = j;
recursive(i+1);
checkpos[j] = 0;
}
}
void solve(){
cin >> t;
sort(t.begin(),t.end());
if(mapp.find(t) != mapp.end()){
cout << mapp[t] << '\n';
// cout << "Y";
return;
}
st.clear();
cnt = 0;
recursive(0);
cout << cnt << '\n';
mapp[t] = cnt;
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
// cout << "start";
for(ll i = 2;i*i < n;i++){
if(p[i] == 1) continue;
for(ll j = i;i*j < n;j++){
// cout << (i*j) << '\n';
p[i*j] = 1;
}
}
// cout << "finish\n";
ll q;
cin >> q >> m;
while(q--)
solve();
return 0;
}
/*
4
2
13
97
12
91
10
3
013
113
302
312
313
013
113
302
312
313
*/