C++4、5超时请求支援
查看原帖
C++4、5超时请求支援
549494
SongGefei楼主2021/10/17 11:15
#include<iostream>
using namespace std;
int a[10],n,m,k,sum=0;
void check()
{
	int tot=0;
	for(int i=1;i<=k;i++)
	  tot+=a[i];
    for(int i=2;i<=k;i++)
      if(a[i]<a[i-1]) return;
	if(m==tot) sum++;
}
void dfs(int t)
{
	for(int i=1;i<=n;i++)
	{
		if(n-i>=0)
		{
			a[t]=i;
			n-=i;
			if(t==k) check();
			else dfs(t+1);
			n+=i;
		}
	}
}
int main()
{
	cin>>n>>k;
	m=n;
	dfs(1);
	cout<<sum<<endl;
	return 0;
} 
2021/10/17 11:15
加载中...