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;
typedef long long ll;
int n, cs, cf;
ll mod = 1000000007LL;
ll dp[2001][2001];
ll go(int now, int blocks){
if(now==n+1){
if(blocks==1){
return 1LL;
}
return 0LL;
}
if(dp[now][blocks]!=-1LL){
return dp[now][blocks];
}
ll ret = 0LL;
if(now==cs){
if(now>cf){
ret = go(now+1,blocks+1);
if(blocks>1 && now<n){
ret += go(now+1,blocks)*(ll)(blocks-1);
}
if(now==n){
ret += go(now+1,blocks);
}
ret %= mod;
}
else{
ret = go(now+1,blocks+1);
if(blocks>=1){
ret += go(now+1,blocks)*(ll)blocks;
}
ret %= mod;
}
}
else if(now==cf){
if(now>cs){
ret = go(now+1,blocks+1);
if(blocks>1 && now<n){
ret += go(now+1,blocks)*(blocks-1);
}
if(now==n){
ret += go(now+1,blocks);
}
ret %= mod;
}
else{
ret = go(now+1,blocks+1);
if(blocks>=1){
ret += go(now+1,blocks)*(ll)blocks;
}
ret %= mod;
}
}
else{
ret = go(now+1,blocks+1);
int cnt = 0;
if(now>cs){
cnt++;
}
if(now>cf){
cnt++;
}
if(cnt==0){
//rand and then rand
if(blocks>=2){
ret += go(now+1,blocks-1) * ((blocks*(blocks-1))%mod);
ret %= mod;
}
}
else if(cnt==1){
//rand and then rand
if(blocks>=3){
ret += go(now+1,blocks-1) * (((blocks-2)*(blocks-1))%mod);
ret %= mod;
}
//imp and something
if(blocks>=2){
ret += go(now+1,blocks-1) * (blocks-1);
ret %= mod;
}
}
else{
//rand and then rand
if(blocks>=4){
ret += go(now+1,blocks-1) * (((blocks-2)*(blocks-3))%mod);
ret %= mod;
}
//imp and something
if(blocks>=3){
ret += 2LL * go(now+1,blocks-1) * (ll)(blocks-2);
ret %= mod;
}
//imp and imp
if(now==n){
ret += go(now+1,blocks-1);
ret %= mod;
}
}
}
dp[now][blocks] = ret;
return ret;
}
int main(){
cin >> n >> cs >> cf;
for(int a = 0; a<=2000; a++){
for(int b= 0; b<=2000; b++){
dp[a][b] = -1LL;
}
}
cout << go(1,0) << 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... |