답안 #30652

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
30652 2017-07-26T02:17:26 Z zscoder 캥거루 (CEOI16_kangaroo) C++14
0 / 100
0 ms 17820 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<int,ll> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;

const int MOD = 1e9 + 7;

void add(int &a, int b)
{
	a+=b;
	while(a>=MOD) a-=MOD;
}

int mult(int a, int b)
{
	return (a*1LL*b)%MOD;
}

int dp[2001][2001];

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n,s,e;
	cin>>n>>s>>e;
	s--; e--;
	if(s==0)
	{
		dp[0][0]=1;
	}
	else if(e==0)
	{
		dp[0][0]=1;
	}
	else
	{
		dp[0][1]=1;
	}
	for(int i=0;i<n-1;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(dp[i][j]!=0)
			{
				int v=dp[i][j];
				if(i+1==n-1)
				{
					add(dp[i+1][j],v);
					continue;
				}
				if(i+1==s)
				{
					//just add
					add(dp[i+1][j],v);
					//combine with free
					if(j>=1) add(dp[i+1][j-1],mult(j,v));
				}
				else if(i+1==e)
				{
					//just add
					add(dp[i+1][j],v);
					//combine with free
					if(j>=1) add(dp[i+1][j-1],mult(j,v));
				}
				else
				{
					//new component
					add(dp[i+1][j+1],v);
					//merge two free components
					if(j>=2) add(dp[i+1][j-1],mult(j*(j-1),v));
					//append to right or left of free component
					if(j>=1) add(dp[i+1][j],mult(j+j,v));
					//append to starting component
					if(i+1>=s)
					{
						add(dp[i+1][j],v);
						//merge starting with free
						if(j>=1)
						{
							add(dp[i+1][j-1],mult(j,v));
						}
					}
					//append to ending component
					if(i+1>=e)
					{
						add(dp[i+1][j],v);
						//merge ending with free
						if(j>=1)
						{
							add(dp[i+1][j-1],mult(j,v));
						}
					}
				}
			}
		}
	}
	cout<<dp[n-1][0]<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 17820 KB Output is correct
2 Incorrect 0 ms 17820 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 17820 KB Output is correct
2 Incorrect 0 ms 17820 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 17820 KB Output is correct
2 Incorrect 0 ms 17820 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 17820 KB Output is correct
2 Incorrect 0 ms 17820 KB Output isn't correct