#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
void run();
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(12);
run();
cout << flush; _Exit(0);
}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
int mod;
ll modPow(ll a, ll e, ll m) {
ll t = 1 % m;
while (e) {
if (e % 2) t = t*a % m;
e /= 2; a = a*a % m;
}
return t;
}
struct Zp {
ll x;
Zp() : x(0) {}
Zp(ll a) : x(a%mod) { if (x < 0) x += mod; }
#define OP(c,d) Zp& operator c##=(Zp r) { x = x d; return *this; } Zp operator c(Zp r) const { Zp t = *this; return t c##= r; }
OP(+, +r.x - mod*(x+r.x >= mod));
OP(-, -r.x + mod*(x-r.x < 0));
OP(*, *r.x % mod);
OP(/, *r.inv().x % mod);
Zp operator-() const { return Zp() - *this; }
Zp inv() const { return pow(mod-2); }
Zp pow(ll e) const{ return modPow(x, e, mod); }
void print() { cerr << x; }
};
constexpr int MAX_A = 20;
using Key = array<uint16_t, MAX_A>;
mt19937_64 rng(1337);
size_t muls[MAX_A];
struct Hash {
size_t operator()(const Key& key) const {
size_t ret = 0;
rep(i, 0, MAX_A) ret ^= muls[i] * key[i];
return ret;
}
};
vector<Zp> fac;
vi revShape;
gp_hash_table<Key, Zp, Hash> cache;
Zp shiftedYoung(const vi& shape) {
Zp div = 1;
int cells = 0;
rep(i, 0, sz(shape)) {
rep(j, 0, shape[i]) {
int hook = sz(shape)-i-1 + shape[i]-j;
if (i+1 < sz(shape) && shape[i] < shape[i+1]) hook += sz(shape)-i-1;
div *= hook;
cells++;
}
}
return fac[cells] / div;
}
Zp young(const vi& shape) {
Zp div = 1;
int cells = 0;
vi below(shape[0]);
for (int i = sz(shape)-1; i >= 0; i--) {
rep(j, 0, shape[i]) {
div *= shape[i]-j+below[j];
below[j]++;
cells++;
}
}
return fac[cells] / div;
}
Zp getDP(Key& key) {
rep(i, 0, sz(revShape)) if (key[i] > revShape[i]) return 0;
auto it = cache.find(key);
if (it != cache.end()) return it->y;
Zp ret = 0;
key[0]++;
ret = getDP(key);
key[0]--;
rep(i, 1, sz(revShape)) {
if (key[i] < key[i-1] || key[i-1] == revShape[i-1]) {
key[i]++;
ret += getDP(key);
key[i]--;
}
}
cache[key] = ret;
return ret;
}
vi getShape(int a, int b, int c) {
vi shape(a);
rep(i, 0, a) shape[i] = min(b, c-i);
return shape;
}
void run() {
int a, b, c;
cin >> a >> b >> c >> mod;
fac.resize(a*b+100'000);
fac[0] = 1;
rep(i, 1, sz(fac)) fac[i] = fac[i-1] * i;
vi shape = getShape(a, b, c);
Zp c1 = young(shape), c2;
if (c == a+b-1) {
c2 = c1;
} else if (c == b) {
vi kek = getShape(b, a, c);
reverse(all(kek));
c2 = shiftedYoung(kek);
} else {
each(m, muls) m = rng();
revShape = shape;
reverse(all(revShape));
Key good = {}, start = {};
rep(i, 0, a) good[i] = uint16_t(revShape[i]);
cache[good] = 1;
c2 = getDP(start);
}
int len = accumulate(all(shape), 0);
Zp cnt = c1 * c2;
cout << len << ' ' << cnt.x << '\n';
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define deb(...) #define DBP(...) using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); run(); cout << flush; _Exit(0); } #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; int mod; ll modPow(ll a, ll e, ll m) { ll t = 1 % m; while (e) { if (e % 2) t = t*a % m; e /= 2; a = a*a % m; } return t; } struct Zp { ll x; Zp() : x(0) {} Zp(ll a) : x(a%mod) { if (x < 0) x += mod; } #define OP(c,d) Zp& operator c##=(Zp r) { x = x d; return *this; } Zp operator c(Zp r) const { Zp t = *this; return t c##= r; } OP(+, +r.x - mod*(x+r.x >= mod)); OP(-, -r.x + mod*(x-r.x < 0)); OP(*, *r.x % mod); OP(/, *r.inv().x % mod); Zp operator-() const { return Zp() - *this; } Zp inv() const { return pow(mod-2); } Zp pow(ll e) const{ return modPow(x, e, mod); } void print() { cerr << x; } }; constexpr int MAX_A = 20; using Key = array<uint16_t, MAX_A>; mt19937_64 rng(1337); size_t muls[MAX_A]; struct Hash { size_t operator()(const Key& key) const { size_t ret = 0; rep(i, 0, MAX_A) ret ^= muls[i] * key[i]; return ret; } }; vector<Zp> fac; vi revShape; gp_hash_table<Key, Zp, Hash> cache; Zp shiftedYoung(const vi& shape) { Zp div = 1; int cells = 0; rep(i, 0, sz(shape)) { rep(j, 0, shape[i]) { int hook = sz(shape)-i-1 + shape[i]-j; if (i+1 < sz(shape) && shape[i] < shape[i+1]) hook += sz(shape)-i-1; div *= hook; cells++; } } return fac[cells] / div; } Zp young(const vi& shape) { Zp div = 1; int cells = 0; vi below(shape[0]); for (int i = sz(shape)-1; i >= 0; i--) { rep(j, 0, shape[i]) { div *= shape[i]-j+below[j]; below[j]++; cells++; } } return fac[cells] / div; } Zp getDP(Key& key) { rep(i, 0, sz(revShape)) if (key[i] > revShape[i]) return 0; auto it = cache.find(key); if (it != cache.end()) return it->y; Zp ret = 0; key[0]++; ret = getDP(key); key[0]--; rep(i, 1, sz(revShape)) { if (key[i] < key[i-1] || key[i-1] == revShape[i-1]) { key[i]++; ret += getDP(key); key[i]--; } } cache[key] = ret; return ret; } vi getShape(int a, int b, int c) { vi shape(a); rep(i, 0, a) shape[i] = min(b, c-i); return shape; } void run() { int a, b, c; cin >> a >> b >> c >> mod; fac.resize(a*b+100'000); fac[0] = 1; rep(i, 1, sz(fac)) fac[i] = fac[i-1] * i; vi shape = getShape(a, b, c); Zp c1 = young(shape), c2; if (c == a+b-1) { c2 = c1; } else if (c == b) { vi kek = getShape(b, a, c); reverse(all(kek)); c2 = shiftedYoung(kek); } else { each(m, muls) m = rng(); revShape = shape; reverse(all(revShape)); Key good = {}, start = {}; rep(i, 0, a) good[i] = uint16_t(revShape[i]); cache[good] = 1; c2 = getDP(start); } int len = accumulate(all(shape), 0); Zp cnt = c1 * c2; cout << len << ' ' << cnt.x << '\n'; } |
English