結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー kriiikriii
提出日時 2018-12-22 01:35:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,007 bytes
コンパイル時間 382 ms
コンパイル使用メモリ 47,320 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-22 02:49:23
合計ジャッジ時間 4,292 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <algorithm>
using namespace std;

int N,K,L,A[202],C[100],U[100],X;

bool good(int a, int r)
{
	if (X < a) X = a;
	if ((L - a) / 2 < r) return false;
	if (a == L) return true;
	if (U[A[a]] < C[A[a]]){
		U[A[a]]++;
		if (good(a+1,r)) return true;
		U[A[a]]--;
	}
	if (a + 2 <= L && A[a]){
		int u = A[a] * 10 + A[a+1];
		if (U[u] < C[u]){
			U[u]++;
			if (good(a+2, r-1)) return true;
			U[u]--;
		}
	}
	return false;
}

int main()
{
#ifdef __LOCAL
	freopen ("input.txt","r",stdin);
#endif

	scanf ("%d %d",&N,&K);
	for (int i=0;i<N;i++){
		int x; scanf ("%d",&x); C[x]++;
		if (x < 10) A[L++] = x;
		else A[L++] = x / 10, A[L++] = x % 10;
	}
	sort(A,A+L);
	reverse(A,A+L);

	int rem = 0;
	for (int i=10;i<100;i++) rem += C[i];

	do{
		for (int i=0;i<100;i++) U[i] = 0;
		X = 0;
		if (good(0,rem)){
			for (int i=0;i<L;i++) printf ("%d",A[i]);
			puts("");
			if (--K == 0) break;
		}
		else{
			if (X + 2 < L) sort(A+X+2,A+L);
		}
	}while(prev_permutation(A,A+L));

	return 0;
}
0