Test instructions: Give n apples, m plates, ask how many different Apple placement methods
It can be abstracted as a number n, split into A1,A2,A3,---, am, so that they are N,
Say this topic is the study of PPT inside, it is the idea is to search
Search criteria settings: When placing apples to the K-plate, the number of apples on the K-plate is greater than the number of apples on the k-1 plate, if it is greater, place it on the K-plate, if not greater, continue to place the apples, if the remaining apples are smaller than the apples on the k-1 plate, Stop the search on this branch.
Learning to search is still a misty---
This problem if you simulate the sample is how to search, so understand a little
7 3
8
These are the 8 cases, respectively.
0 0 7
0 1 6
0 2 5
0 3 4
1 1 5
1 2 4
1 3 3
2 2 3
------------------------
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace Std;int num[10010],cnt,n,m;void dfs (int k,int W)//k represents the number of plates that have been used now, w means that several apples have been placed {if (k==m) // When placed on the last plate to determine {if (n-w>=num[k-1])//If the remaining number of apples at this time is greater than the number of apples on the k-1 plate, then put the remaining apples on the last plate {num[k]=n-w;cnt++;} return;} for (int i=0;i<=n;i++) {if (i>=num[k-1]) {Num[k]=i;w=w+i;k=k+1;dfs (K,W);//Continue to find the next dish on the Apple W=w-i;k=k-1;}} int main () {int ncase;scanf ("%d", &ncase), while (ncase--) {scanf ("%d%d", &n,&m); Cnt=0;dfs (1,0);// At first it was a plate, with 0 apple printf ("%d\n", CNT);}
POJ 1664 put Apple "DFS"