/**
* user: volkov-d03
* fname: Ivan
* lname: Volkov
* task: fishing
* score: 100.0
* date: 2019-10-11 08:23:34.758620
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#define int long long
using namespace std;
const int M = 1e9 + 7, MAX_N = 302;
//signed dp[2][3][MAX_N][MAX_N][MAX_N];
unordered_map <signed, signed> dp;
long long hashh(int i, int j, int k, int t, int flag) {
long long res = flag * 3 * MAX_N * MAX_N * MAX_N;
res += t * MAX_N * MAX_N * MAX_N;
res += i * MAX_N * MAX_N;
res += j * MAX_N;
res += k;
return res;
}
int calc(int i, int j, int k, int t, bool flag) {
int h = hashh(i, j, k, t, flag);
if (i + j + k == 0) {
dp[h] = 1;
//dp[flag][t][i][j][k] = 1;
return 1;
}
if (t == 0 && !flag) return 0;
if (t == 0) flag = 0;
if (dp.find(h) != dp.end()) return dp[h];
dp[h] = 0;
if (t == 0) {
if (j + k == 0) {
dp[h] = calc(i, j, k, 1, flag);
return dp[h];
}
if (j != 0) dp[h] = (j * calc(i + 1, j - 1, k, 1, flag)) % M;
if (k != 0) dp[h] = (dp[h] + k * calc(i, j, k - 1, 1, 1)) % M;
}
if (t == 1) {
if (i + k == 0) {
dp[h] = calc(i, j, k, 2, flag);
return dp[h];
}
if (i != 0) dp[h] = (i * calc(i - 1, j, k, 2, 1)) % M;
if (k != 0) dp[h] = (dp[h] + k * calc(i, j + 1, k - 1, 2, flag)) % M;
}
if (t == 2) {
if (i + j == 0) {
dp[h] = calc(i, j, k, 0, flag);
return dp[h];
}
if (i != 0) dp[h] = (i * calc(i - 1, j, k + 1, 0, flag)) % M;
if (j != 0) dp[h] = (dp[h] + j * calc(i, j - 1, k, 0, 1)) % M;
}
//cout << i << ' ' << j << ' ' << k << ' ' << t << ' ' << dp[i][j][k][t] << endl;
return dp[h];
}
vector <int> have[MAX_N];
signed main() {
int n, t;
cin >> n >> t;
for (int tt = 0; tt < t; tt++) {
int a = 0, b = 0, c = 0;
for (int i = 0; i < 3 * n; i++) have[i].clear();
for (int i = 0; i < 2 * n; i++) {
int x;
cin >> x;
have[x - 1].push_back(0);
}
for (int i = 0; i < 2 * n; i++) {
int x;
cin >> x;
have[x - 1].push_back(1);
}
for (int i = 0; i < 2 * n; i++) {
int x;
cin >> x;
have[x - 1].push_back(2);
}
for (int i = 0; i < 3 * n; i++) {
if (have[i][0] == 0 && have[i][1] == 1) c++;
if (have[i][0] == 0 && have[i][1] == 2) b++;
if (have[i][0] == 1 && have[i][1] == 2) a++;
}
cout << calc(a, b, c, 0, 1) << "\n";
}
//cout << calc(0, 2, 0, 0) << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
252 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
8 ms |
1272 KB |
Output is correct |
5 |
Correct |
149 ms |
14084 KB |
Output is correct |
6 |
Correct |
305 ms |
24984 KB |
Output is correct |
7 |
Correct |
464 ms |
35180 KB |
Output is correct |
8 |
Correct |
763 ms |
55916 KB |
Output is correct |
9 |
Correct |
1244 ms |
101304 KB |
Output is correct |
10 |
Correct |
1526 ms |
107180 KB |
Output is correct |