Submission #125567

#TimeUsernameProblemLanguageResultExecution timeMemory
125567tutisKangaroo (CEOI16_kangaroo)C++17
51 / 100
2086 ms322920 KiB
/*input
10 2 3
*/
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll modulo = 1000000007;
int sgn(ll x)
{
	if (x < 0)
		return -1;
	if (x > 0)
		return 1;
	return 0;
}
map<tuple<int, int, int, int>, ll>M;
ll f(int n, int a, int b, int s)
{
	if (a == b)
		return 0;
	if (M.count(make_tuple(n, a, b, s)))
		return M[make_tuple(n, a, b, s)];
	if (n == 2)
	{
		if (sgn(b - a) != s)
			return 0;
		else
			return 1;
	}
	ll ret = 0;
	for (int c = 1; c <= n; c++)
	{
		if (sgn(b - c) == s)
		{
			ret += f(n - 1, a - (a > b), c - (c > b), -s);
		}
	}
	ret %= modulo;
	return M[make_tuple(n, a, b, s)] = ret;
}
ll f(int n, int a, int b)
{
	ll ans = f(n, a, b, 1) + f(n, a, b, -1);
	ans %= modulo;
	return ans;
}
ll fast(int N, int A, int B)
{
	ll L[N + 1][N + 1][N + 1];
	ll R[N + 1][N + 1][N + 1];
	for (int n = 2; n <= N; n++)
	{
		for (int a = 1; a <= n; a++)
		{
			for (int b = 1; b <= n; b++)
			{
				L[n][a][b] = 0;
				R[n][a][b] = 0;
				if (a == b)
					continue;
				if (n == 2)
				{
					if (a > b)
					{
						L[n][a][b] = 1;
					}
					if (a < b)
					{
						R[n][a][b] = 1;
					}
				}
				else
				{
					for (int c = 1; c <= n; c++)
					{
						if (c > b)
						{
							L[n][a][b] += R[n - 1][a - (a > b)][c - (c > b)];
						}
						if (c < b)
						{
							R[n][a][b] += L[n - 1][a - (a > b)][c - (c > b)];
						}
					}
				}
				L[n][a][b] %= modulo;
				R[n][a][b] %= modulo;
			}
		}
	}
	ll ret = L[N][A][B] + R[N][A][B];
	ret %= modulo;
	return ret;
}
int main()
{
	int n, a, b;
	cin >> n >> a >> b;
	//cout << f(n, a, b) << "\n";
	cout << fast(n, a, b) << "\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...