이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |