Submission #406338

#TimeUsernameProblemLanguageResultExecution timeMemory
406338AriaHKangaroo (CEOI16_kangaroo)C++11
100 / 100
85 ms72232 KiB
/** I can do this all day **/

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;

typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define SZ(x)			    		(int)x.size()
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);

const int N = 2e3 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;

ll pw(ll a , ll b, ll M)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }

ll inv, dp[N][4][N << 1];

inline ll C2(int x)
{
	return 1ll * x * (x - 1) / 2;
}

int main()
{
	inv = pw(2, mod - 2, mod);
	int n, s, f;
	scanf("%d%d%d", &n, &s, &f);
	dp[0][0][0] = 1;
	for(int i = 0; i < n; i ++)
	{
		for(int c = 0; c < 3; c ++)
		{
			for(int j = 0; j <= i * 2; j ++)
			{
				ll cu = dp[i][c][j];
				if(!cu)  continue;
				if(i + 1 == s || i + 1 == f)
				{
					/// match be ye C beshe
					if(c) dp[i + 1][c - 1][j] = (dp[i + 1][c - 1][j] + cu * c) % mod;
					/// match be ye sare masir
					if(j > 1) dp[i + 1][c + 1][j - 2] = (dp[i + 1][c + 1][j - 2] + cu * j) % mod;
					dp[i + 1][c + 1][j] = (dp[i + 1][c + 1][j] + cu) % mod;
				}
				else
				{
					/// match beshe be 2 ta C
					if(c > 1)
					{
						dp[i + 1][c - 2][j] = (dp[i + 1][c - 2][j] + cu * C2(c)) % mod;
					}
					/// match beshe be 2 ta masir
					if(j > 1)
					{
						dp[i + 1][c][j - 2] = (dp[i + 1][c][j - 2] + cu * j * (j - 2) / 2) % mod;
					}
					/// ye masir c ye j
					if(c > 0 && j > 1)
					{
						dp[i + 1][c][j - 2] = (dp[i + 1][c][j - 2] + cu * c * j) % mod;
					}
					dp[i + 1][c][j + 2] = (dp[i + 1][c][j + 2] + cu * inv) % mod;
				}
			}
		}
	}
	/*for(int i = 0; i <= n; i ++)
	{
		for(int c = 0; c < 3; c ++)
		{
			for(int j = 0; j <= i; j ++)
			{
				if(dp[i][c][j])
				{
					printf("i = %d c = %d j = %d dp = %lld\n", i, c, j, dp[i][c][j]);
				}
			}
		}
	}
	*/
	printf("%lld\n", dp[n][0][0]);
    return 0;
}

/** test corner cases(n = 1?) watch for overflow or minus indices **/

Compilation message (stderr)

kangaroo.cpp: In function 'int main()':
kangaroo.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |  scanf("%d%d%d", &n, &s, &f);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...