[ CodeVS冲杯之路 ] P1039

  不充钱,你怎么AC?

  题目:http://codevs.cn/problem/1039/

  一道赤裸裸的嘲讽型数学题,推出来的话算法代码就3行,没有推出来连暴力都无从入手……

  设 f(n,m) 为整数 n 拆分成 m 个数字的方案数,那么分以下两种情况讨论

    1° 不选 1 的情况

      如果不选择 1,我们把 n 拆分成 m 块时可以看做先将每一块加上个 1,则 n 还剩余 n-m,即 f(n-m,m)

    2° 选 1 的情况

      那么就直接选一个 1,即 f(n-1,m-1),因为是递归的方法,所以选择 1 的个数通过迭代下去

  那么总递推式为 f(n,m)=f(n-m,m)+f(n-1,m-1)

  这里还要判断两种情况

    1° n=0 或 n<m 或 m=0 时,方案数为 0

    2° m=1 或 m=n 时,方案数为 1

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 int f(int n,int m)
10 {
11     if (n==0||n<m||m==0) return 0;
12     if (m==1||n==m) return 1;
13     return f(n-m,m)+f(n-1,m-1);
14 }
15 int main()
16 {
17     int n,m;
18     scanf("%d%d",&n,&m);
19     printf("%d
",f(n,m));
20     return 0;
21 } 
原文地址:https://www.cnblogs.com/hadilo/p/5882850.html