This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 2000+5;
const int MOD = 1000000007;
inline int add(int a, int b){return (a+b>=MOD)?a+b-MOD:a+b;}
inline void inc(int& a, int b){a = add(a, b);}
inline int sub(int a, int b){return (a-b<0)?a-b+MOD:a-b;}
inline void dec(int &a, int b){a = sub(a, b);}
inline int mul(int a, int b){return 1ll*a*b%MOD;}
inline void grow(int &a, int b){a = mul(a, b);}
int n, cs, cf;
int dp[N][N];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> cs >> cf;
if(cs > cf) cs = n+1-cs, cf = n+1-cf;
dp[0][0] = 1;
for(int i = 1; i<cs; i++) for(int j = 1; j<=i; j++) inc(dp[i][j], add(mul(dp[i-1][j+1], j), mul(dp[i-1][j-1], j)));
for(int j = 1; j<=cs; j++) inc(dp[cs][j], add(dp[cs-1][j], dp[cs-1][j-1]));
for(int i = cs+1; i<cf; i++) for(int j = 1; j<=i; j++) inc(dp[i][j], add(mul(dp[i-1][j+1], j), mul(dp[i-1][j-1], j-1)));
for(int j = 1; j<=cf; j++) inc(dp[cf][j], add(dp[cf-1][j], dp[cf-1][j-1]));
for(int i = cf+1; i<=n; i++) for(int j = 1; j<=i; j++) inc(dp[i][j], add(mul(dp[i-1][j+1], j), mul(dp[i-1][j-1], j-2)));
cout << dp[n][1] << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |