結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー 👑 kmjpkmjp
提出日時 2018-12-22 02:58:38
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,894 bytes
コンパイル時間 1,679 ms
コンパイル使用メモリ 165,936 KB
実行使用メモリ 80,568 KB
最終ジャッジ日時 2023-08-04 04:34:11
合計ジャッジ時間 5,053 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------


int N,K;
string A[101];
vector<pair<string,int>> V;
int B[101];
vector<string> S;
unordered_set<ll> memo[101];
ll mo[2]={1000000007,1000000021};


void dfs(int id,string& cur,ll H1,ll H2,ll ma) {
	if(S.size()>K+600) {
		return;
	}
	
	ll H3=((H1<<32)|H2)^ma;
	
	if(memo[id].count(H3)) return;
	memo[id].insert(H3);
	
	if(id==N) {
		S.push_back(cur);
		return;
	}
	
	
	
	int i;
	FOR(i,V.size()) if(V[i].second) {
		if(S.size()>K+600) return;
		V[i].second--;
		cur+=V[i].first;
		ll G1,G2;
		if(B[i]<10) {
			G1=H1*10+B[i];
			G2=H2*10+B[i];
		}
		else {
			G1=H1*100+B[i];
			G2=H2*100+B[i];
		}
		
		dfs(id+1,cur,G1%mo[0],G2%mo[1],ma^(1LL<<(i%50)));
		V[i].second++;
		cur.resize(cur.size()-V[i].first.size());
	}
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N) cin>>A[i];
	FOR(i,N) {
		FOR(j,N-1) {
			if(A[j]+A[j+1]<A[j+1]+A[j]||(A[j]+A[j+1]==A[j+1]+A[j]&&A[j]<A[j+1])) {
				swap(A[j],A[j+1]);
			}
		}
	}
	
	FOR(i,N) {
		if(V.size()==0 || V.back().first!=A[i]) V.push_back({A[i],0});
		V.back().second++;
	}
	FOR(i,V.size()) {
		FORR(c,V[i].first) B[i]=B[i]*10+c-'0';
	}
	string C;
	dfs(0,C,0,0,0);
	
	sort(ALL(S));
	while(K && S.size()) {
		cout<<S.back()<<endl;
		S.pop_back();
		K--;
	}
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0