답안 #893712

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
893712 2023-12-27T10:09:32 Z vjudge1 캥거루 (CEOI16_kangaroo) C++11
6 / 100
2000 ms 111840 KB
#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 2000 + 10;
const ll mod = 1e9 + 7;

ll um(ll a, ll b){
	return ((1LL * a * b) % mod + mod) % mod;
}
ll subr(ll a, ll b){
	return ((1LL * a - b) % mod + mod) % mod;
}
ll add(ll a, ll b){
	return ((1LL * a + b) % mod + mod) % mod;
}
ll binpow(ll x, ll step){
	ll res = 1LL;
	while(step){
		if(step & 1) res = um(res, x);
		x = um(x, x);
		step /= 2;
	}
	return res;
}
bool was[N];
ll cnt, n, f, h[N];
map <pair <ll, pll>, pair <ll, bool>> mp;
ll calc(ll index, ll dir, ll hsh){
	//cout << index << " "<< dir << endl;
	pair <ll, bool> cur = mp[{hsh, {index, dir}}];
	//cout << index << " " << dir << " "<< hsh << endl;
	if(cur.S) return cur.F;
	if(cnt == n){
		if(index == f) return 1LL;
		return 0LL;
	}
	ll local = 0LL;
	if(dir <= 0){
		for(ll i = index + 1; i <= n; i++){
			if(was[i]) continue;
			was[i] = true;
			cnt++;
			local = add(local, calc(i, 1, add(hsh, h[i])));
			cnt--;
			was[i] = false;
		}
	} 
	if(dir >= 0){
		for(ll i = index - 1; i >= 1; i--){
			if(was[i]) continue;
			was[i] = true;
			cnt++;
			local = add(local, calc(i, -1, add(hsh, h[i])));
			cnt--;
			was[i] = false;
		}
	}
	mp[{hsh, {index, dir}}] = {local, true};
	return local;
}
int main() {
	// freopen("kangaroo.txt", "r", stdin);
	// freopen("kangaroo.txt", "w", stdout);
	ios::sync_with_stdio(false);
  	cin.tie(NULL);
  	ll s;
  	cin >> n >> s >> f;
  	h[1] = 1LL;
  	for(ll i = 2; i <= n; i++){
  		h[i] = um(h[i - 1], 2LL);
  	}
  	was[s] = true;
  	cnt = 1;
  	cout << calc(s, 0, 0LL);
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Execution timed out 2078 ms 111840 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Execution timed out 2078 ms 111840 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Execution timed out 2078 ms 111840 KB Time limit exceeded
4 Halted 0 ms 0 KB -