Submission

Status:

PPPPP--P--

Subtask/Task Score:

60/100

Score: 60

User: Brabra475

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

Language: cpp

Time: 0.380 second

Submitted On: 2026-03-11 23:08:05

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

bool check(int n){
    if(n < 2) return false;
    if(n == 2) return true;
    if(n % 2 == 0) return false;
    for(int i = 3; i * i <= n; i += 2)
        if(n % i == 0) return false;
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int m, n;
    cin >> m >> n;
    
    while(m--) {
        string s;
        cin >> s;
        sort(s.begin(), s.end()); // ✅ sort ก่อนเพื่อให้ next_permutation ครบ
        
        set<int> seen; // ✅ กัน duplicate เช่น "113" มี 1 สองตัว
        int ans = 0;
        
        do {
            // แปลง string → int
            int num = 0;
            for(char c : s)
                num = num * 10 + (c - '0');
            
            if(seen.insert(num).second) { // ยังไม่เคยเช็ค
                if(check(num)) ans++;
            }
        } while(next_permutation(s.begin(), s.end()));
        
        cout << ans << "\n";
    }
}