看题很容易想到一个递推式,但是题目给出的数据很大,所以,虽然题目保证答案You may safely assume that this number fits into a 32-bit unsigned integer.。
递推肯定会超时的额。得另外想个办法。
把递推式写下来,可以发现f(n,m)=C(n+m,n);所以其实就是让你输出C(n+m,n)。
虽然数据很大,但是由于答案很小,所以用白书上的那个递推求组合数的方法就能解决了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
long long C(double n,double m)
{
long long a,b;
a=1;
b=1;
for (long long i=1; i<=m; i++)
{
a=b*(n-i+1)/i;
b=a;
}
return a;
}
int main()
{
double t,a,b;
while (1)
{
cin>>a>>b;
if (a == 0 && b == 0)
break;
if (a < b)
{
t=b;
b=a;
a=t;
}
cout<<C(a+b,b)<<endl;
}
}