Graduates' Leaving (2013年省赛第二题)

题目描述

June is the month for leaving. Many graduates have to leave colleage and they need to pick up their own things. TT is one of the graduates. He has M same books and N same boxes. He needs to divide these books into K(0<K<=N) groups and put them into the boxes. What's more, the boxes are allowed to be empty and are able to set as many books as he wants.
For example, now TT has 5 books and 3 boxes. (1,3,1),(1,1,3) and (3,1,1) are considered as the same division. 
Can you calculate how many divisions TT has.

输入


The input files consists of several test cases. Each test case has two integers M and N.(0<M<=300, 0<N<=300)

输出

Output the answer for each test case in a single line.

样例输入

5 3
3 2

样例输出

5
2

提示

Take (M=3,N=2) for example, it has 2 division (3,0) and (1,2).


觉得这道题也很巧妙,题目要我们求得是将一个数分为若干块 问最多能分几块。

我们将求将一个数分为n块转化为将一个数分为最大块为n的方法,他们两个是等价的,可以用ferrers图证明。

任何将n分为m块的方法都可以画成ferrers图:

                                   1                                          1 1                   
                                   1 1                        转化为最大值是3  1 1 1                    
                                   1 1 1                     ———————>   1 1 1 1 这是最大值为3的的分法
                                   1 1 1 这是将9分为{4 3 2}
                        将n分为m块的方法 与 将n分为最大值为m的方法 数目相同
于是我们就可以写出动态转移方程

F[n][m]=F[n-m][m]+F[n-m][m-1]+F[n-m][m-2]+········+F[n-m][1]                 

由于F[n-1][m-1]=F[n-m][m-2]+······+F[n-m][1],

于是上面的公式可以化简为:F[n][m]=F[n-m][m]+F[n-1][m-1]

我的代码:

#include <cstdio>
using namespace std;
long long f[301][301];
int main (){
  long long  n,m;
  f[0][0]=1;
  for (int i=1;i<=300;i++)
    for (int j=1;j<=300;j++)if (i>=j){
        f[i][j]=f[i-j][j]+f[i-1][j-1];
      }
  while (scanf("%lld%lld",&n,&m)!=EOF){
    long long ans=0;
    for (int i=1;i<=m;i++) ans+=f[n][i];
    printf("%lld\n",ans);
  }
  return 0;
}
感谢王涵大神的讲解

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值