萌新刚注册账号,T148457能用dfs吗?
  • 板块学术版
  • 楼主_QyGyQ_
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/3/17 16:27
  • 上次更新2024/3/17 18:14:43
查看原帖
萌新刚注册账号,T148457能用dfs吗?
797897
_QyGyQ_楼主2024/3/17 16:27

T148457

#include<cstdio>
using namespace std;
const int N=1e6+7;
char *p1,*p2,buf[N];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read(){
    int x=0,f=1;
    char ch=nc();
    while(ch<48||ch>57){
        if(ch=='-') f=-1;
        ch=nc();
    }
    while(ch>=48&&ch<=57) x=x*10+ch-48,ch=nc();
   	return x*f;
}
int a[N];
int ans=1e6;
int S[N],V[N];
int n,m;
#define min_sum(a,b) (a<b ? a : b)
void dfs(int floor,int r,int h,int s,int v){
	int highy=h;
	if(!floor){
		if(v==n){
			ans=min_sum(ans,s);
		}
		return ;
	}
	if(s+S[floor-1]>=ans) return ;
	if(2*(n-v)/r+s>=ans) return ;
	if(v+V[floor-1]>n) return ;
	for(int i=r-1;i>=floor;i--){
		if(floor==m){
			s=i*i;
		}
		highy=min_sum(h-1,(n-V[floor-1]-v)/i/i);
		for(int j=highy;j>=floor;j--){
			dfs(floor-1,i,j,s+2*i*j,v+i*i*j);
		}
	}
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=m;i++){
		S[i]=S[i-1]+2*i*i;
		V[i]=V[i-1]+i*i*i;
	}
	dfs(m,n,n,0,0);
	if(ans==1e6) puts("-1");
	else printf("%d",ans);
	return 0;
}
2024/3/17 16:27
加载中...