Through a 1440: 1] divided [example number

Depth-first search

Search, it is my nightmare, because Wan accidentally, it is necessary TLE, the last reference books a bit, and finally get out of the AC code, which is the subject of a pass, the more difficult.

topic


                      1440:【例题1】数的划分

             时间限制: 1000 ms         内存限制: 65536 KB
                    提交数: 1029     通过数: 692 
【题目描述】
将整数nn分成kk份,且每份不能为空,任意两份不能相同(不考虑顺序)。
例如:n=7n=7,k=3k=3,下面三种分法被认为是相同的。
{1,1,5}{1,5,1}{5,1,1}{1,1,5}{1,5,1}{5,1,1};
问有多少种不同的分法。 输出一个整数,即不同的分法。

【输入】
两个整数nn,k(6<n≤200,2≤k≤6)k(6<n≤200,2≤k≤6),中间用单个空格隔开。
【输出】
一个整数,即不同的分法。

【输入样例】
7 3
【输出样例】
4
【提示】
四种分法为:{1,1,5}{1,2,4}{1,3,3}{2,2,3}{1,1,5}{1,2,4}{1,3,3}{2,2,3}。

【来源】

No

Code

#include<iostream>
using namespace std;
int n,m,a[8],s=0;
void Dfs(int k){
    if(n==0) return ;
    if(k==m){
        if(n>=a[k-1]) s++;
        return ;
    }
    for(int i=a[k-1];i<=n/(m-k+1);i++){
        a[k]=i;
        n-=i;
        Dfs(k+1);
        n+=i;
    }
}
int main(){
    cin>>n>>m;
    a[0]=1;
    Dfs(1);
    cout<<s<<endl;
    return 0;
}

welcome! ! ! Questions can click on the link below ↓↓↓

1440: division [1] number of examples

Published 14 original articles · won praise 8 · views 1162

Guess you like

Origin blog.csdn.net/Horse_Lake/article/details/103922743