luogu1025_数的划分(NOIP2001提高组第2题)

luogu1025_数的划分(NOIP2001提高组第2题)

时空限制    1000ms/128MB

题目描述

将整数 n分成 k 份,且每份不能为空,任意两个方案不相同(不考虑顺序)。

例如: n=7, k=3,下面三种分法被认为是相同的。

1,1,5 ;
1,5,1;
5,1,1;

问有多少种不同的分法。

输入输出格式

输入格式:

n,k ( 6<n≤20,2≤k≤6 )

输出格式:

1个整数,即不同的分法。

输入输出样例

输入样例#1:

7 3

输出样例#1:

4

说明

四种分法为:
1,1,5;
1,2,4;
1,3,3;
2,2,3;

代码

#include<iostream>
using namespace std;
int n,k,tot;

void search(int rest,int x,int last){	//rest划分x项,从last开始
	if (x==1){	//只有一项,不用划分
		tot++;
		return;
	}
	for (int i=last; i<=rest/x; i++)	//新划分项不小于前面
		search(rest-i,x-1,i);
}

int main(){
	cin>>n>>k;
	search(n,k,1);	//n划分k项,从1开始
	cout<<tot<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/WDAJSNHC/article/details/81302411