搜索解法求复杂度分析
查看原帖
搜索解法求复杂度分析
631544
li_cat楼主2024/10/27 09:39

RT,考场写了搜索+剪枝,自测1e5数据秒过,民间数据也很快就过了

#include <bits/stdc++.h>
#define str string
using namespace std;

const int MAXN=1e5+5;

int num[MAXN],cost[10]={6,2,5,5,4,5,6,3,7,6};

int Get(int x){
	return (x+6)/7;
}//

bool dfs(int x,int stick,int dep){
	if(stick<0) return 0;
	if((stick+6)/7>dep-x) return 0;
	if(x==dep) return stick==0;
	
	for(int i=0;i<=9;++i){
		if(i==0 && x==0) continue;
		num[x]=i;
		if(dfs(x+1,stick-cost[i],dep)) return 1;
	}
	return 0;
}//

int n;

void read(){
	cin>>n;
}//

str solve(){
	if(n==0 || n==1) return "-1";
	if(!dfs(0,n,Get(n))) return "-1";
	str s;
	for(int i=0;i<Get(n);++i)
		s+=(num[i]+'0');
	
	return s;
}//

int main(){
	int T;
	cin>>T;
	while(T--){
		read();
		cout<<solve()<<'\n';
	}
	return 0;
}
2024/10/27 09:39
加载中...