Submission #513823

#TimeUsernameProblemLanguageResultExecution timeMemory
513823julian33Kangaroo (CEOI16_kangaroo)C++14
100 / 100
36 ms31696 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cerr<<vars<<" = ";
    string delim="";
    (...,(cerr<<delim<<values,delim=", "));
    cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif

#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);} 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN=2e3+5; //make sure this is right
const int mod=1e9+7;

ll dp[mxN][mxN];
void add(ll &a, ll b){a=(a+b)%mod;}

/*
dp[i][j] -> at i, you have j components
create new component: dp[i][j] += dp[i-1][j-1]*j
merge 2 components: dp[i][j] += dp[i-1][j+1]*j
*/

int main(){
    cin.sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n,s,f; cin>>n>>s>>f;
    dp[0][0]=1;
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(i==s || i==f) cnt++;
        for(int j=0;j<=n;j++){
            if(i==s || i==f){
                if(j) add(dp[i][j],dp[i-1][j-1]);
                if(j) add(dp[i][j],dp[i-1][j]);
            } else{
                if(j-cnt>0 && j) add(dp[i][j],dp[i-1][j-1]*(j-cnt));
                add(dp[i][j],dp[i-1][j+1]*j);
            }
            // deb(i,j,dp[i][j]);
        }
    }
    cout<<dp[n][1]<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...