Submission
Status:
PPPPP--P--
Subtask/Task Score:
60/100
Score: 60
User: Imorange
Problemset: การเรียงสับเปลี่ยน
Language: cpp
Time: 0.046 second
Submitted On: 2025-10-15 13:45:29
#include <bits/stdc++.h>
using namespace std;
int sz,n;
bool p[1000009];
int ans=0;
string s;
set<long long int> arr;
bool rep[10000009];
long long int tran(string s){
int k=0;
for(int i=0;i<s.size();i++){
k*=10;
k+=s[i]-'0';
}
return k;
}
void think(string w,bool b[]){
if(w.size()==sz){
// int k=stoi(w);
long long int k=tran(w);
// cout << w << '\n';
if(p[k]==0 && rep[k]==0){
rep[k]=1;
arr.insert(k);
ans++;
}
return ;
}
for(int i=1;i<=sz;i++){
if(b[i]==1) continue;
b[i]=1;
w+=s[i-1];
think(w,b);
w.pop_back();
b[i]=0;
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
for(int i=2;i*i<=1000000;i++){
if(p[i]==0){
for(int j=i*i;j<=1000000;j+=i) p[j]=1;
}
}
p[0]=1;
p[1]=1;
cin >> n >> sz;
for(int i=1;i<=n;i++){
bool b[9]={};
cin >> s;
ans=0;
string w="";
think(w,b);
cout << ans << '\n';
for(auto e:arr) rep[e]=0;
arr.clear();
}
return 0;
}