結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー chocoruskchocorusk
提出日時 2018-12-22 12:05:41
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,124 bytes
コンパイル時間 1,124 ms
コンパイル使用メモリ 113,376 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-22 02:54:59
合計ジャッジ時間 2,734 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<string, int> Ps;
const ll INF=1e9;
bool comp(string x, string y){
	if(x[0]!=y[0]) return x[0]>y[0];
	Ps xp, yp;
	if(x.size()==1) xp=Ps(x+x, 1);
	else xp=Ps(x, 2);
	if(y.size()==1) yp=Ps(y+y, 1);
	else yp=Ps(y, 2);
	return xp>yp;
}
int k;
string b[100];
int ct[10];
ll comb[201][201];
set<string> st;
void zentan(int p){
	st.clear();
	int c=0;
	sort(b, b+p, comp);
	int ind[100]; ind[0]=0; string b2[100]; b2[0]=b[0];
	for(int i=1; i<p; i++){
		if(b[i]!=b[i-1]){
			ind[i]=ind[i-1]+1;
			b2[ind[i]]=b[i];
		}else{
			ind[i]=ind[i-1];
		}
	}
	while(c<400*k){
		string s;
		for(int i=0; i<p; i++) s+=b2[ind[i]];
		if(st.size()<k){
			st.insert(s);
		}else{
			auto itr=st.begin();
			if((*itr)<s) st.insert(s);
		}
		c++;
		if(!next_permutation(ind, ind+p)) break;
	}
}
int main()
{
	int n;
	cin>>n>>k;
	string a[100];
	for(int i=0; i<n; i++){
		cin>>a[i];
	}
	sort(a, a+n, comp);
	if(k==1){
		for(int i=0; i<n; i++) cout<<a[i];
		cout<<endl;
		return 0;
	}
	comb[0][0]=1;
	for(int i=1; i<=200; i++){
		comb[i][0]=comb[i][i]=1;
		for(int j=1; j<i; j++){
			comb[i][j]=min(comb[i-1][j-1]+comb[i-1][j], INF);
		}
	}
	for(int i=0; i<n; i++){
		for(int j=0; j<a[n-1-i].size(); j++) ct[a[n-1-i][j]-'0']++;
		ll ct0=1; int s=0;
		for(int j=0; j<10; j++) s+=ct[j];
		for(int j=0; j<10; j++){
			ct0*=comb[s][ct[j]];
			ct0=min(ct0, INF);
			s-=ct[j];
		}
		if(i==n-1 || ct0>=k){
			for(int j=n-1-i; j<n; j++){
				b[j-(n-1-i)]=a[j];
			}
			zentan(i+1);
			if(st.size()>=k){
				auto itr=st.end(); itr--;
				string s;
				for(int l=0; l<n-1-i; l++) s+=a[l];
				int ck=0;
				for(; ; itr--){
					cout<<s+(*itr)<<endl;
					ck++;
					if(ck==k) break;
					if(itr==st.begin()) break;
				}
				return 0;
			}
		}
	}
	return 0;
}
0