# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1121386 | kingmessi | 캥거루 (CEOI16_kangaroo) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include<atcoder/all>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define int long long
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define repin rep(i,0,n)
#define di(a) int a;cin>>a;
#define precise(i) cout<<fixed<<setprecision(i)
#define vi vector<int>
#define si set<int>
#define mii map<int,int>
#define take(a,n) for(int j=0;j<n;j++) cin>>a[j];
#define give(a,n) for(int j=0;j<n;j++) cout<<a[j]<<' ';
#define vpii vector<pair<int,int>>
#define sis string s;
#define sin string s;cin>>s;
#define db double
#define be(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bpc(x) __builtin_popcountll(x)
#define btz(x) __builtin_ctz(x)
using namespace std;
using namespace atcoder;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<pair<int, int>, null_type,less<pair<int, int> >, rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
const long long INF=1e18;
const long long M=1e9+7;
const long long MM=998244353;
using mint = static_modint<M>;
int power( int N, int M){
int power = N, sum = 1;
if(N == 0) sum = 0;
while(M > 0){if((M & 1) == 1){sum *= power;}
power = power * power;M = M >> 1;}
return sum;
}
const int N = 2005;
mint dp[N][N];
/**
i : number of elements processed
j : number of components made
**/
void solve()
{
int n,cs,cf;
cin >> n >> cs >> cf;
if(cs > cf)swap(cs,cf);
dp[1][1] = 1;
bool bs = 0,bf = 0;
rep(i,1,n){
rep(j,1,i+1){
//create new cc
dp[i+1][j+1] += dp[i][j];
if(i+1 == cs){
//append to the start
dp[i+1][j] += dp[i][j]*j;
bs = 1;
}
else if(i+1 == cf){
//append to the end
dp[i+1][j] += dp[i][j]*max(j-1,1LL);
bf = 1;
}
else{
//merge two cc
if(j > 1){
if(bs == 0){
dp[i+1][j-1] += dp[i][j]*j*(j-1);
}
else if(bf == 0){
dp[i+1][j-1] += dp[i][j]*(j-1)*(j-1);
}
else{
dp[i+1][j-1] += dp[i][j]*(j-2)*(j-2);//one with cf not merged
dp[i+1][j-1] += dp[i][j]*(j-1);//one with cf merged
}
}
}
}
}
cout << dp[n][1].val() << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef NCR
init();
#endif
#ifdef SIEVE
sieve();
#endif
// int t; cin >> t; while(t--)
solve();
return 0;
}