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 k_N = 2000 + 3;
const int k_Mod = 1e9 + 7;
int n, cs, cf;
long long dp[k_N][k_N];
void fix(long long &x){
x = (x >= k_Mod) ? (x - k_Mod) : x;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> cs >> cf;
//cs--, cf--;
if(cs > cf)
swap(cs, cf);
for(int pos = 0; pos <= n; ++pos){
for(long long group = 0; group <= n; ++group){
long long &answer = dp[pos][group];
if(pos == 0){
answer = (group == 1);
continue;
}
answer = 0;
if(pos > cf){
answer = dp[pos - 1][group + 1];
if(group >= 2){
answer += dp[pos - 1][group - 1] * group % k_Mod * (group - 1) % k_Mod;
fix(answer);
}
}
else if(pos == cf){
answer = dp[pos - 1][group + 1];
answer += dp[pos - 1][group] * group % k_Mod;
fix(answer);
}
else if(pos > cs){
answer = dp[pos - 1][group + 1];
if(group >= 2){
answer += dp[pos - 1][group - 1] * (group - 1) % k_Mod * (group - 1) % k_Mod;
fix(answer);
}
}
else if(pos == cs){
answer = dp[pos - 1][group + 1];
if(pos != 1){
if(group)
answer += dp[pos - 1][group] * (group - 1) % k_Mod;
}
else
answer += dp[pos - 1][group] * group % k_Mod;
fix(answer);
}
else if(pos < cs){
answer = dp[pos - 1][group + 1];
if(group >= 2){
if(pos == 1)
answer += dp[pos - 1][group - 1] * (((group * group % k_Mod - 3 * group + 3) % k_Mod + k_Mod) % k_Mod) % k_Mod;
else
answer += dp[pos - 1][group - 1] * (((group * group % k_Mod - 3 * group + 2) % k_Mod + k_Mod) % k_Mod) % k_Mod;
fix(answer);
}
}
//cout << answer << " - " << pos << " " << group << endl;
}
}
cout << dp[n][0] << "\n";
}
# | 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... |