結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー pekempeypekempey
提出日時 2018-12-22 01:47:35
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,257 bytes
コンパイル時間 530 ms
コンパイル使用メモリ 65,872 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-04 04:30:01
合計ジャッジ時間 1,368 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 AC 18 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
}

0