Deep search optimization pruning

Before the title search done a lot of deep, a lot of TLE, so pruning is very important, how to "take precautions" to avoid unnecessary search tree branch?

example:

Dividing the number of

The integer n into k parts, and each can not be empty, any two can have the same (irrespective of order).

For example: n = 7, k = 3, the following three sub-methods are considered the same.

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

Q. How many different points system. Output an integer, i.e., different points system.

Meaning of the questions was, distribute duplicate number combinations, compared with an algorithm to ensure that the number is incremented to 1-k

Code:

#include<bits/stdc++.h>
using namespace std;
int n,m,k,s;
int a[10];
void search(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;
        search(k+1);
        n+=i;
    }
}
int main(){
    scanf("%d%d",&n,&m);
    a[0]=1;
    search(1);
    printf("%d",s);
}

 

Guess you like

Origin www.cnblogs.com/648-233/p/10992971.html