嗯...
题目链接:https://www.luogu.org/problemnew/show/P1025
这或许是最基础的dfs了,但是其中需要剪枝一下...
AC代码:
1 #include<cstdio> 2 3 int n, k, cnt; 4 5 inline void dfs(int last, int sum, int cur){ 6 if(cur == k){ 7 if(sum == n) cnt++;//注意 8 return; 9 } 10 for(int i = last; sum + i * (k - cur) <= n; i++)//剪枝 11 dfs(i, sum + i, cur + 1); 12 } 13 14 int main(){ 15 scanf("%d%d", &n, &k); 16 dfs(1, 0, 0); 17 printf("%d ", cnt); 18 return 0; 19 }