1440 through a website: [1] example division and pruning the number of summary

Original title   Portal

Description [title]

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.

[Enter]

Two integers n, k (6 <n≤200,2≤k≤6), separated by a single space.

[Output]

An integer, i.e., different points system.

[Sample input]

7 3

[Sample Output]

4

【prompt】

Four kinds of points of law: 1,1,5; 1,2,4; 1,3,3; 2,2,3.

 

This problem is to compute the number n is divided into k parts random number scheme. Of Equation i.e. X1 + X2 + ...... Xk = n, 1 <= X1 <= X2 <= ...... Xk of the stops.

The search method is followed by an enumeration X1, X2, ...... Xk value, and then determine if this has been the direct search, the process speed is very slow. However, because the data is relatively small scale of this problem, if good control node extension upper and lower bounds, but also be able to quickly draw solution.

Restrictions:

1. Since the number of decomposition order does not, we set the number of decomposition in ascending order, the lower bound of the extension point value should not be less than the previous extension point, i.e., a [i-1] <= a [i];

2. Suppose we have n decomposed into a [1] + a [2] + ...... + a [i-1], if a [i] is the maximum value of this i ~ k k-i + 1 min Average division, i.e., set m = n- (a [1] + a [2] + ...... + a [i-1]), the a [i] <= m / (k-i + 1), so the extension point the upper bound is m / (k-i + 1);

code show as below:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int read()
{
    char ch=getchar();
    int a=0,x=1;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') x=-x;
        ch=getchar();
    }
    while(ch>='0'&&ch<='. 9 ' ) 
    { 
        A = (A << . 3 ) + (A << . 1 ) + (CH-, ' 0 ' ); 
        CH = getchar (); 
    } 
    return A * X; 
} 
int n-, m, A [ . 9 ] , S = 0 ;
 void DFS ( int X) 
{ 
    IF (n == 0 ) return ;        // if n or disassembly, directly returns 
    IF (X == m)                 // if the number of decomposition at this time to the number m 
    {
         IF (n-> A = [X- . 1]) S ++ ;
         return ; 
    } 
    for ( int I = A [X- . 1 ]; I <= n-/ (m-X + . 1 ); I ++)   // Enumeration to the upper bound from the lower bound of 
    { 
        A [X] = I; 
        n- - = I; 
        DFS (X + . 1 ); 
        n- + = I;       // back 
    } 
 } 
 int main () 
 { 
     n- = Read (); 
     m = Read (); 
     A [ 0 ] = . 1 ;         //Begins to decompose from 1 
     DFS ( 1 ); 
     COUT << S << endl;
      return  0 ; 
 }

 

God's pruning techniques were found Summary:

The so-called pruning is through some kind of judgment, avoid unnecessary traversal, figuratively speaking, is to subtract some of the search tree branches, pruning Old City.

Pruning principles:

1. validity;

2. accuracy;

3. efficiency;

The depth-first search optimization techniques:

1. Optimization of the search order;

2. excluding equivalents miscellaneous;

3. Feasibility pruning;

4. Optimal Pruning;

The memory of;

Guess you like

Origin www.cnblogs.com/xcg123/p/10991783.html