18.04.11 luoguP1025 数的划分

题目描述

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

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

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

问有多少种不同的分法。

输入输出格式

输入格式:

 

n,k (6<n<=200,2<=k<=6)

 

输出格式:

 

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

 

输入输出样例

输入样例#1: 
7 3
输出样例#1: 
4

说明

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <algorithm>
 5 #include <stdlib.h>
 6 
 7 using namespace std;
 8 int count0=0,n,k;//n num k part
 9 
10 void dfs(int n,int part,int t){
11     if(t==k-1&&n-part>=part)count0++;
12     if(t==k-1)return;
13     for(int i=part;i<=n-part;i++)
14         dfs(n-part,i,t+1);
15 }
16 
17 int main()
18 {
19     scanf("%d%d",&n,&k);
20     for(int i=1;i<=n/2;i++)
21         dfs(n,i,1);
22     printf("%d
",count0);
23     return 0;
24 }
View Code
注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
原文地址:https://www.cnblogs.com/yalphait/p/8797020.html