链接:https://ac.nowcoder.com/acm/contest/338/J
来源:牛客网
Less taolu, more sincerity.
You just copy and paste this code and you will get AC!
#include<iostream> using namespace std; const long long mod = 1e9+7; long long func(int x){ if (x==1||x==0){ return 1; } return (x*func(x-1)+(x-1)*func(x-2))%mod; } int n; int main(){ cin>>n; cout<<func(n); return 0; }
Input only a single
integer n.
Please output
the answer by this code.
0<=N<=1e5
记忆化搜索,懒得解释
#include<iostream> using namespace std; const int maxn = 100000+10; int f[maxn]; const long long mod = 1e9+7; long long func(int x) { if (x==1||x==0) { return 1; } if(f[x]) return f[x]; else return f[x] = (x*func(x-1)+(x-1)*func(x-2))%mod; } int n; int main() { cin>>n; cout<<func(n); return 0; }
原文:https://www.cnblogs.com/DWVictor/p/10229930.html