結果
| 問題 | No.775 tatyamと素数大富豪(hard) | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2018-12-22 01:47:35 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                WA
                                 
                            (最新) 
                                AC
                                 
                            (最初) | 
| 実行時間 | - | 
| コード長 | 1,257 bytes | 
| コンパイル時間 | 651 ms | 
| コンパイル使用メモリ | 64,984 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-14 01:32:27 | 
| 合計ジャッジ時間 | 1,284 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 9 WA * 1 | 
ソースコード
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <cstdlib>
using namespace std;
int n, K, m;
int a[100];
int cnt[100];
string curr;
int ju(int k) {
    int res = 0;
    for (int i = 0; i < 10; i++) {
        res += cnt[k * 10 + i];
    }
    return res;
}
void dfs(int d) {
    if (curr.size() == m) {
        cout << curr << endl;
        K--;
        if (K == 0) exit(0);
        return;
    }
    if (d == -1) {
        for (int i = 9; i >= 1; i--) {
            dfs(i);
        }
    } else {
        for (int i = 9; i >= 0; i--) {
            int x = d * 10 + i;
            if (cnt[x] > 0) {
                cnt[x]--;
                curr += to_string(x);
                dfs(-1);
                curr.pop_back();
                curr.pop_back();
                cnt[x]++;
            } else if (i > 0 && cnt[d] > 0 && (cnt[i] > 0 || ju(i) > 0)) {
                cnt[d]--;
                curr += to_string(d);
                dfs(i);
                curr.pop_back();
                cnt[d]++;
            }
        }
    }
}
int main() {
    cin >> n >> K;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        cnt[a[i]]++;
        m += to_string(a[i]).size();
    }
    dfs(-1);
}
            
            
            
        