#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator<<(auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
auto operator<<(auto &o, auto a) -> decltype(all(a), o);
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x)
#else
#define deb(...) 0
#endif
int P, phiP;
struct mint {
int x = 0;
mint operator+(mint o) const {return {x + o.x >= P ? x + o.x - P : x + o.x}; }
mint operator-(mint o) const {return {x < o.x ? x - o.x + P : x - o.x}; }
mint operator+() const {return *this; }
mint operator-() const {return {x ? P - x : 0}; }
mint operator*(mint o) const {return {int(ll(x) * o.x % P)}; }
mint operator/(mint o) const {return *this * o.inv(); }
mint & operator+=(mint o) {return *this = *this + o; }
mint & operator-=(mint o) {return *this = *this - o; }
mint & operator*=(mint o) {return *this = *this * o; }
mint & operator/=(mint o) {return *this = *this / o; }
mint pow(ll e) const {
mint ret{1}, b(*this);
for (; e; e >>= 1) {
if (e & 1)
ret *= b;
b *= b;
}
return ret;
}
mint inv() const {return pow(phiP - 1); }
};
array<mint, int(1e6) + 6> fact, ifact, invs, sfact;
void initmint() {
fact[0] = ifact[0] = sfact[0] = mint{1};
fact[1] = ifact[1] = sfact[1] = invs[1] = mint{1};
fwd(i, 2, sz(fact)) {
invs[i] = -mint{P / i} * invs[P % i];
fact[i] = fact[i-1] * mint{i};
ifact[i] = ifact[i-1] * invs[i];
sfact[i] = sfact[i-1] * fact[i];
}
};
auto &operator<<(auto &os, mint m) {
return os << m.x - (2 * m.x >= P && &os == &cerr ? P : 0);
}
mint evalCatConvFast(int n, int x, int skip) {
mint r{1};
fwd(i, 1, n) {
int donate = max((skip+1-i)/2, 0);
int tu = n-i - donate;
assert(tu >= 0);
// deb(i, n, skip, donate, tu);
r *= mint(x+i).pow(tu);
r *= mint(x+1-i).pow(donate);
}
return r;
}
mint evalOtherCatConv(int n, int x, int skip) {
mint r{1};
fwd(i, 1, n) {
r *= mint{x+i}.pow(max(0, n-i-skip));
}
vi zera;
rep(idskip, skip) {
for (int &i : zera) i -= 2;
rep(xx, n-1-idskip)
zera.pb(-idskip-1+xx*2);
}
for (int i : zera)
r *= mint{2*x + i};
return r;
}
mint evalValue(int a, int b, int skip) {
mint p1 = evalCatConvFast(a, b, skip);
mint p2 = evalOtherCatConv(a, b, skip);
vi values(a, b);
rep(i, skip)
values[i] -= skip - i;
mint ret = fact[accumulate(all(values), 0)];
for (int i : values)
ret *= ifact[i];
ret *= ret;
ret *= p1.inv();
ret *= p2.inv();
return ret;
}
const int MAXA = 21;
array<array<mint, MAXA + 1>, MAXA + 1> multipliers;
mint evalValueWithMultiplier(int a, int b, int skip) {
mint el = evalValue(a, b, skip);
mint mu = multipliers[a][skip];
return el * mu;
}
void initmuls() {
fwd(a, 1, MAXA + 1) {
multipliers[a][0] = sfact[a-1].pow(2);
multipliers[a][a-1] = multipliers[a][0] * mint{2}.pow(a * (a-1) / 2);
fwd(skip, 1, a - 1) {
mint shouldBe = evalValueWithMultiplier(skip + 1, a, skip);
mint our = evalValue(a, skip + 1, skip);
multipliers[a][skip] = shouldBe * our.inv();
}
}
}
void solve() {
int a, b, c;
cin >> a >> b >> c >> P;
phiP = P - 1;
initmint();
initmuls();
c = a + b - 1 - c;
mint ans = evalValueWithMultiplier(a, b, c);
int n = a * b - c * (c + 1) / 2;
cout << n << ' ' << ans.x << '\n';
// cout << (ans + mint{n}).x << '\n';
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cout << fixed << setprecision(10);
int z = 1;
// cin >> z;
rep(_, z) solve();
cout << flush;
_Exit(0);
}
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 161 162 163 164 165 166 167 168 169 170 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator<<(auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } auto operator<<(auto &o, auto a) -> decltype(all(a), o); DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x) #else #define deb(...) 0 #endif int P, phiP; struct mint { int x = 0; mint operator+(mint o) const {return {x + o.x >= P ? x + o.x - P : x + o.x}; } mint operator-(mint o) const {return {x < o.x ? x - o.x + P : x - o.x}; } mint operator+() const {return *this; } mint operator-() const {return {x ? P - x : 0}; } mint operator*(mint o) const {return {int(ll(x) * o.x % P)}; } mint operator/(mint o) const {return *this * o.inv(); } mint & operator+=(mint o) {return *this = *this + o; } mint & operator-=(mint o) {return *this = *this - o; } mint & operator*=(mint o) {return *this = *this * o; } mint & operator/=(mint o) {return *this = *this / o; } mint pow(ll e) const { mint ret{1}, b(*this); for (; e; e >>= 1) { if (e & 1) ret *= b; b *= b; } return ret; } mint inv() const {return pow(phiP - 1); } }; array<mint, int(1e6) + 6> fact, ifact, invs, sfact; void initmint() { fact[0] = ifact[0] = sfact[0] = mint{1}; fact[1] = ifact[1] = sfact[1] = invs[1] = mint{1}; fwd(i, 2, sz(fact)) { invs[i] = -mint{P / i} * invs[P % i]; fact[i] = fact[i-1] * mint{i}; ifact[i] = ifact[i-1] * invs[i]; sfact[i] = sfact[i-1] * fact[i]; } }; auto &operator<<(auto &os, mint m) { return os << m.x - (2 * m.x >= P && &os == &cerr ? P : 0); } mint evalCatConvFast(int n, int x, int skip) { mint r{1}; fwd(i, 1, n) { int donate = max((skip+1-i)/2, 0); int tu = n-i - donate; assert(tu >= 0); // deb(i, n, skip, donate, tu); r *= mint(x+i).pow(tu); r *= mint(x+1-i).pow(donate); } return r; } mint evalOtherCatConv(int n, int x, int skip) { mint r{1}; fwd(i, 1, n) { r *= mint{x+i}.pow(max(0, n-i-skip)); } vi zera; rep(idskip, skip) { for (int &i : zera) i -= 2; rep(xx, n-1-idskip) zera.pb(-idskip-1+xx*2); } for (int i : zera) r *= mint{2*x + i}; return r; } mint evalValue(int a, int b, int skip) { mint p1 = evalCatConvFast(a, b, skip); mint p2 = evalOtherCatConv(a, b, skip); vi values(a, b); rep(i, skip) values[i] -= skip - i; mint ret = fact[accumulate(all(values), 0)]; for (int i : values) ret *= ifact[i]; ret *= ret; ret *= p1.inv(); ret *= p2.inv(); return ret; } const int MAXA = 21; array<array<mint, MAXA + 1>, MAXA + 1> multipliers; mint evalValueWithMultiplier(int a, int b, int skip) { mint el = evalValue(a, b, skip); mint mu = multipliers[a][skip]; return el * mu; } void initmuls() { fwd(a, 1, MAXA + 1) { multipliers[a][0] = sfact[a-1].pow(2); multipliers[a][a-1] = multipliers[a][0] * mint{2}.pow(a * (a-1) / 2); fwd(skip, 1, a - 1) { mint shouldBe = evalValueWithMultiplier(skip + 1, a, skip); mint our = evalValue(a, skip + 1, skip); multipliers[a][skip] = shouldBe * our.inv(); } } } void solve() { int a, b, c; cin >> a >> b >> c >> P; phiP = P - 1; initmint(); initmuls(); c = a + b - 1 - c; mint ans = evalValueWithMultiplier(a, b, c); int n = a * b - c * (c + 1) / 2; cout << n << ' ' << ans.x << '\n'; // cout << (ans + mint{n}).x << '\n'; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); int z = 1; // cin >> z; rep(_, z) solve(); cout << flush; _Exit(0); } |
English