이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int32_t main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N, U, V;
cin >> N >> U >> V;
-- U;
-- V;
map<long long, int> memo[N][3];
function<int(long long, int, int )> func = [&](long long tmp, int ind, int dir) -> int {
if (ind == V) {
memo[ind][dir][tmp] = (tmp == 0);
return (tmp == 0);
}
if (memo[ind][dir].find(tmp) != memo[ind][dir].end()) return memo[ind][dir][tmp];
if (dir != 1) {
bool as = false;
for (int i = ind + 1;i < N;i ++) if ((tmp & (1LL << i)) > 0) {
as = true;
break;
}
if (!as) {
if (memo[ind][dir].find(tmp) != memo[ind][dir].end()) memo[ind][dir][tmp] = 0;
} else {
for (int i = ind + 1;i < N;i ++) if ((tmp & (1LL << i)) > 0) {
if (memo[i][dir].find((tmp ^ (1LL << i))) == memo[i][dir].end()) {
memo[ind][dir][tmp] = (func ((tmp ^ (1LL << i)), i, 1) + memo[ind][dir][tmp]) % mod;
} else {
memo[ind][dir][tmp] = (memo[i][1][(tmp ^ (1LL << i))] + memo[ind][dir][tmp]) % mod;
}
}
// ans += memo[ind][dir][tmp];
}
}
if (dir != 2) {
bool as = false;
for (int i = ind - 1;i >= 0;i --) if ((tmp & (1LL << i)) > 0) {
as = true;
break;
}
if (!as) {
if (memo[ind][dir].find(tmp) != memo[ind][dir].end()) memo[ind][dir][tmp] = 0;
} else {
for (int i = ind - 1;i >= 0;i --) if ((tmp & (1LL << i)) > 0) {
if (memo[i][dir].find((tmp ^ (1LL << i))) == memo[i][dir].end()) {
memo[ind][dir][tmp] = (func ((tmp ^ (1LL << i)), i, 2) + memo[ind][dir][tmp]) % mod;
} else {
memo[ind][dir][tmp] = (memo[i][2][(tmp ^ (1LL << i))] + memo[ind][dir][tmp]) % mod;
}
}
// ans += memo[ind][dir][tmp];
}
}
// memo[tmp] = (memo[tmp] + ans) % mod;
return memo[ind][dir][tmp];
};
int answer = func (((1LL << N) - 1) ^ (1LL << U), U, 0);
// cout << (((1 << N) - 1) ^ (1 << U)) << "\n";
cout << answer << "\n";
// for (auto v : memo[0]) {
// cout << v.first << " " << v.second << "\n";
// } cout << "\n";
// for (auto v : memo[1]) {
// cout << v.first << " " << v.second << "\n";
// } cout << "\n";
// for (auto v : memo[2]) {
// cout << v.first << " " << v.second << "\n";
// } cout << "\n";
// for (auto v : memo[3]) {
// cout << v.first << " " << v.second << "\n";
// } cout << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |