题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=695&pid=1001
大搬家
Accepts: 619
Submissions: 2535
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
f[1]=1,f[2]=2,f[3]=4,f[4]=10;
公式为: f[i]=(f[i-1]+(i-1)*f[i-2])%Mod;(注意要取模哦~~)
详见代码。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define N 1000000+10
#define Mod 1000000007
ll f[N];
int main()
{
int T,n;
int flag=1;
f[1]=1,f[2]=2;
for (int i=3; i<1000010; i++)
{
f[i]=(f[i-1]+(i-1)*f[i-2])%Mod;
}
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
printf ("Case #%d:\n",flag++);
printf("%lld\n",f[n]%Mod);
}
return 0;
}