NC21187. Less taolu
描述
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.
示例1
输入:
3
输出:
11
示例2
输入:
100
输出:
372497045
C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 1148K, 提交时间: 2019-01-06 18:28:28
#include<iostream> using namespace std; const long long mod = 1e9+7; long long int i,n,a[100000]={1,1}; int main(){ cin>>n; for(i=2;i<=n;i++){ a[i]=(i*a[i-1]+(i-1)*a[i-2])%mod; } cout<<a[n]; return 0; }
C(clang 3.9) 解法, 执行用时: 3ms, 内存消耗: 1128K, 提交时间: 2019-01-06 16:04:58
#include<stdio.h> const long long mod = 1e9 + 7; int main() { long long int a[100000],n; scanf("%lld",&n); a[0]=a[1]=1; for(int i=2;i<=n;i++) a[i]=(i*a[i-1]+(i-1)*a[i-2])%mod; printf("%lld\n",a[n]); return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 396K, 提交时间: 2020-01-12 09:20:26
#include<stdio.h> const long long mod = 1e9+7; int main() { int n; long long a=1,b=1,c=1; scanf("%d",&n); for(int i=2;i<=n;i++) { c=(i*b+(i-1)*a)%mod; a=b; b=c; } printf("%lld",c); return 0; }
Python3(3.5.2) 解法, 执行用时: 107ms, 内存消耗: 7460K, 提交时间: 2019-01-06 12:17:06
n=int(input()) a=[1,1] for i in range(2,n+1): a.append((i*a[i-1]+(i-1)*a[i-2])%1000000007) print(a[n]);