NC54733. Ambulance
描述
输入描述
The first line contains one integer N(N ≤100000), the number of the system’s wire(s).
The next line contains N integers, the i-th number is Ai.
It is guaranteed that A1,A2,...An is a permutation of n.
输出描述
Output an integer which denotes the answer.
示例1
输入:
2 1 2
输出:
1
说明:
There two ways to connect the electric wire.示例2
输入:
3 2 3 1
输出:
4
C++14(g++5.4) 解法, 执行用时: 20ms, 内存消耗: 760K, 提交时间: 2020-01-11 13:35:51
#include <bits/stdc++.h> using namespace std; const int N=1e5+5; const int MD=1e9+7; int f[N],n; int main() { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%*d"); f[0]=0,f[1]=1; for(int i=2;i<=n;i++) { f[i]=1LL*(f[i-1]+f[i-2])*(i-1)%MD; } printf("%d\n",f[n]); return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 1244K, 提交时间: 2020-02-25 14:57:01
#include<bits/stdc++.h> using namespace std; const int Mod=1e9+7; int main() { int n; string s; cin>>n; long long a[n+5]; getline(cin,s); a[0]=0,a[1]=1; for(int i=2;i<=n;i++) a[i]=(i-1)*(a[i-1]+a[i-2])%Mod; cout<<a[n]<<"\n"; return 0; }