QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#883460 | #9642. 冲刺 | hos_lyric# | 30 | 855ms | 239092kb | C++14 | 8.3kb | 2025-02-05 16:30:19 | 2025-02-05 16:30:19 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 1004535809;
using Mint = ModInt<MO>;
constexpr int LIM_INV = 10'100'000;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];
void prepare() {
inv[1] = 1;
for (int i = 2; i < LIM_INV; ++i) {
inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
}
fac[0] = invFac[0] = 1;
for (int i = 1; i < LIM_INV; ++i) {
fac[i] = fac[i - 1] * i;
invFac[i] = invFac[i - 1] * inv[i];
}
}
Mint binom(Int n, Int k) {
if (n < 0) {
if (k >= 0) {
return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
} else if (n - k >= 0) {
return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
} else {
return 0;
}
} else {
if (0 <= k && k <= n) {
assert(n < LIM_INV);
return fac[n] * invFac[k] * invFac[n - k];
} else {
return 0;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// [z^m] ((1-P)z/(1-Pz))^(a+b-1) (1 + z/(1-Pz))
Mint brute(Mint P, Mint Q, int u, int v) {
if (u == 0 && v == 0) return 1;
Mint ret = 0;
for (int m = 0; m <= u && m <= v; ++m) {
const int a = u - m, b = v - m;
if (a+b <= 0) break;
auto at = [&](int n) -> Mint {
return (n >= a+b-1) ? (binom(n, a+b-1) * P.pow(n - (a+b-1))) : 0;
};
ret += binom(a+b, a) * Q.pow(a) * (1-Q).pow(b) * (1-P).pow(a+b-1) * (at(m) + (1-P) * at(m-1));
}
return ret;
}
/*
d := v - u >= 0
+(1,0) i times
+(0,1) (d+i) times
count by z^u
\sum[i>=[d=0]] binom(d+2i,i) Q^i (1-Q)^(d+i) z^i ((1-P)z/(1-Pz))^(d+2i-1) (1 + z/(1-Pz))
= (1-Q)^d ((1-P)z/(1-Pz))^(d-1) (1 + z/(1-Pz)) \sum[i>=0] (binom(d+2i,i) (Q (1-Q) z ((1-P)z/(1-Pz))^2)^i - [d=0])
C(t) := (1 - sqrt(1-4x)) / 2x
F := Q (1-Q) z ((1-P)z/(1-Pz))^2
= (1-Q)^d ((1-P)z/(1-Pz))^(d-1) (1 + z/(1-Pz)) ((1-4F)^(-1/2) C(F)^d - [d=0])
*/
/*
F := Q (1-Q) z ((1-P)z/(1-Pz))^2
(1-4F)^(-1/2) = (1-Pz) ((1-Pz)^2 - 4 Q (1-Q) (1-P)^2 z^3)^(-1/2)
*/
vector<Mint> calcInvSqrt(Mint P, Mint Q, int len) {
const Mint fs[4] = {1, -2*P, P*P, -4 * Q * (1-Q) * (1-P)*(1-P)};
vector<Mint> gs(len, 0);
gs[0] = 1;
for (int i = 1; i < len; ++i) {
for (int j = 1; j <= i && j <= 3; ++j) gs[i] += ((-inv[2] + 1) * j - i) * fs[j] * gs[i - j];
gs[i] *= inv[i];
}
for (int i = len; --i >= 1; ) gs[i] -= P * gs[i - 1];
return gs;
}
/*
d = 0
((1-P)z/(1-Pz))^(-1) (1 + z/(1-Pz)) ((1-4F)^(-1/2) - 1)
= ((1 + (1-P)z) / (1-P)z) ((1-4F)^(-1/2) - 1)
*/
vector<Mint> calcDiag(Mint P, Mint Q, int len) {
auto hs = calcInvSqrt(P, Q, len + 1);
// cerr<<"hs = "<<hs<<endl;
assert(hs[0] == 1);
hs.erase(hs.begin());
const Mint val = (1-P).inv();
for (int i = 0; i < len; ++i) hs[i] *= val;
for (int i = len; --i >= 1; ) hs[i] += (1-P) * hs[i - 1];
hs[0] = 1;
return hs;
}
/*
d = 1
(1-Q) (1 + z/(1-Pz)) (1-4F)^(-1/2) C(F)
= (1-Q) ((1 + (1-P)z) / (1-Pz)) ((1-4F)^(-1/2) - 1) (2F)^-1
*/
vector<Mint> calcDiag1(Mint P, Mint Q, int len) {
auto hs = calcInvSqrt(P, Q, len + 4);
assert(hs[0] == 1);
assert(hs[1] == 0);
assert(hs[2] == 0);
hs.erase(hs.begin(), hs.begin() + 3);
const Mint val = (1-Q) * (2 * Q * (1-Q) * (1-P)*(1-P)).inv();
for (int i = 0; i < len; ++i) hs[i] *= val;
for (int i = len; --i >= 1; ) hs[i] += (1-P) * hs[i - 1];
for (int i = len; --i >= 1; ) hs[i] -= P * hs[i - 1];
return hs;
}
/*
(1-Q)^d (1-P)^(d-1) z^(d-1) (1 + (1-P)z) (1/(1-Pz))^d (1-4F)^(-1/2) C(F)^d
1-4F =: G/(1-Pz)^2
H := (1-4F)^(-1/2)
(1-4F)^(-1/2) C(F)^d
= \sum[0<=k<=d] binom(d,k) (-1/2)^k sqrt(1-4F)^(k-1) / F^k
= \sum[0<=k<=d] binom(d,k) (-1/2)^k (Q(1-Q)(1-P)^2)^-k (
[k:even] H G^(k/2) (1-Pz)^k z^(-3k)
+ [k:odd ] G^((k-1)/2) (1-Pz)^(k+1) z^(-3k)
)
*/
int main() {
prepare();
int N;
Mint P, Q;
for (; ~scanf("%d%u%u", &N, &P.x, &Q.x); ) {
vector<int> U(N), V(N);
for (int n = 0; n < N; ++n) {
scanf("%d%d", &U[n], &V[n]);
}
int len = 0;
for (int n = 0; n < N; ++n) {
chmax(len, U[n]);
chmax(len, V[n]);
}
len += 10;
const auto diag = calcDiag(P, Q, len);
// cerr<<"diag = "<<diag<<endl;
// for(int i=0;i<len;++i)assert(diag[i]==brute(P,Q,i,i));
const auto diag1U = calcDiag1(P, Q, len);
const auto diag1V = calcDiag1(P, 1-Q, len);
auto solve = [&](int u, int v) -> Mint {
const int d = v - u;
if (d == 0) return diag[u];
if (d == 1) return diag1U[u];
if (d == -1) return diag1V[v];
assert(false);
};
Mint ans = 0;
for (int n = 0; n < N; ++n) ans += solve(U[n], V[n]);
printf("%u\n", ans.x);
}
return 0;
}
详细
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
4872 301135505 501969174 62 292 76 226 20 67 11 117 145 33 28 37 43 16 26 147 148 257 282 249 275 89 129 30 100 253 110 206 100 230 2 103 71 260 154 164 14 183 185 184 227 25 166 164 187 144 181 62 176 139 121 253 252 181 87 145 255 63 66 2 178 209 161 30 112 49 71 178 264 281 81 190 124 216 231 91 ...
output:
result:
Subtask #2:
score: 0
Runtime Error
Test #4:
score: 0
Runtime Error
input:
5000 732289608 658888527 232 5000 2921 5000 5000 3706 5000 4148 5000 2105 1771 5000 3970 5000 3729 5000 1614 5000 5000 151 5000 3553 5000 3405 5000 2869 5000 835 2673 5000 3238 5000 2853 5000 5000 999 5000 3 420 5000 5000 3025 5000 6 830 5000 723 5000 5000 851 5000 1210 1844 5000 5000 4389 2203 5000...
output:
result:
Subtask #3:
score: 0
Runtime Error
Test #8:
score: 0
Runtime Error
input:
5000 0 454262500 3448016 3452435 2687053 2686221 8169887 8172208 9741693 9745191 2134360 2132632 2223989 2222297 6946537 6946296 2695577 2695329 1450857 1451876 8453241 8451200 9107846 9110910 8988968 8987572 9169892 9169540 7695546 7690794 5077074 5072683 7532989 7530319 7944119 7943232 2293369 229...
output:
result:
Subtask #4:
score: 0
Runtime Error
Test #12:
score: 0
Runtime Error
input:
5000 102882260 0 6819081 6816853 5149145 5145899 9892659 9895176 8631345 8627935 349191 353992 7035406 7036453 5912292 5916306 9657928 9662549 6200108 6198289 2101773 2106094 9309396 9307219 7369903 7374376 9787731 9790309 9108307 9103593 3772703 3768160 976239 977233 3825147 3825573 7626595 7626660...
output:
result:
Subtask #5:
score: 5
Accepted
Test #16:
score: 5
Accepted
time: 141ms
memory: 127516kb
input:
4976 574513706 284978964 52150 52150 482959 482959 189922 189922 459237 459237 168102 168102 367208 367208 291040 291040 272705 272705 3179 3179 179983 179983 307014 307014 66641 66641 384965 384965 263794 263794 61297 61297 33430 33430 376221 376221 69803 69803 328604 328604 208254 208254 441178 44...
output:
685390943
result:
ok single line: '685390943'
Test #17:
score: 5
Accepted
time: 138ms
memory: 127516kb
input:
4977 738732183 48211757 224643 224643 231072 231072 350520 350520 353788 353788 177601 177601 266218 266218 54772 54772 441204 441204 126298 126298 210653 210653 406849 406849 89882 89882 399624 399624 6984 6984 230890 230890 449088 449088 206247 206247 286000 286000 177809 177809 351108 351108 3601...
output:
834673621
result:
ok single line: '834673621'
Test #18:
score: 5
Accepted
time: 141ms
memory: 127768kb
input:
5000 82619521 288046394 499070 499070 499507 499507 499421 499421 499996 499996 497341 497341 499880 499880 498466 498466 497215 497215 497717 497717 499041 499041 499450 499450 498373 498373 495077 495077 498387 498387 499689 499689 497281 497281 497675 497675 497312 497312 498371 498371 499832 499...
output:
256096624
result:
ok single line: '256096624'
Subtask #6:
score: 15
Accepted
Test #19:
score: 15
Accepted
time: 855ms
memory: 238884kb
input:
5000 235325377 512694546 5854662 5854662 315229 315229 3137556 3137556 7608068 7608068 9583921 9583921 4958765 4958765 3109547 3109547 3431604 3431604 3400130 3400130 5073586 5073586 2742677 2742677 5111263 5111263 5485216 5485216 7602789 7602789 1522223 1522223 2264844 2264844 8283226 8283226 80915...
output:
374571076
result:
ok single line: '374571076'
Test #20:
score: 15
Accepted
time: 848ms
memory: 239048kb
input:
4999 577526640 536592123 8982831 8982831 8658108 8658108 3051124 3051124 6249675 6249675 736542 736542 1907066 1907066 7853081 7853081 4392691 4392691 6371987 6371987 7681658 7681658 367808 367808 4584307 4584307 6635109 6635109 1920456 1920456 9443645 9443645 7198022 7198022 4573392 4573392 8832798...
output:
858926241
result:
ok single line: '858926241'
Test #21:
score: 15
Accepted
time: 852ms
memory: 239092kb
input:
5000 967858359 21174968 9997118 9997118 9998285 9998285 9996180 9996180 9999627 9999627 9996927 9996927 9999741 9999741 9997786 9997786 9997212 9997212 9996650 9996650 9996632 9996632 9996692 9996692 9999548 9999548 9998795 9998795 9998904 9998904 9997909 9997909 9998051 9998051 9995365 9995365 9995...
output:
236292211
result:
ok single line: '236292211'
Subtask #7:
score: 10
Accepted
Test #22:
score: 10
Accepted
time: 846ms
memory: 238920kb
input:
5000 736097577 890228464 1825247 1825246 4650840 4650840 2234311 2234312 7343146 7343145 3787634 3787634 2656735 2656734 1463014 1463013 3322493 3322494 8952134 8952133 9554797 9554798 9938426 9938425 7324212 7324212 9852188 9852188 4135136 4135135 2970038 2970037 216167 216166 5330601 5330602 72371...
output:
60773436
result:
ok single line: '60773436'
Test #23:
score: 10
Accepted
time: 848ms
memory: 239052kb
input:
4999 71938875 980975552 9165546 9165547 6658015 6658014 715219 715219 2770358 2770357 6724087 6724088 190936 190937 762142 762143 7697875 7697876 2560417 2560416 244298 244297 5390425 5390425 2948144 2948144 5979604 5979605 7544060 7544060 7640731 7640731 9811715 9811715 8451395 8451396 3957616 3957...
output:
553974017
result:
ok single line: '553974017'
Test #24:
score: 10
Accepted
time: 849ms
memory: 238940kb
input:
5000 718317306 176345776 9998853 9998852 9997215 9997216 9998526 9998526 9997709 9997708 9995955 9995955 9995446 9995447 9997855 9997855 9995342 9995342 9999947 9999946 9997976 9997976 9999200 9999199 9998210 9998211 9998941 9998941 9996613 9996613 9996078 9996077 9995751 9995752 9999173 9999172 999...
output:
947034016
result:
ok single line: '947034016'
Subtask #8:
score: 0
Runtime Error
Test #25:
score: 0
Runtime Error
input:
5000 118468585 976109781 377801 381122 359413 364103 226131 224910 163598 165233 16964 20842 113098 111221 129456 130508 478339 478755 182926 179190 143979 142184 388264 390144 365026 366013 16759 19223 359155 361291 62049 57288 66890 63415 425143 423399 382820 384122 488899 485326 218034 220421 554...
output:
result:
Subtask #9:
score: 0
Runtime Error
Test #29:
score: 0
Runtime Error
input:
3000 177229351 510136745 179259 179695 2298716 2302777 4922758 4926164 3300268 3302823 1818638 1817659 4429564 4426586 3915685 3911287 2997695 2999804 459298 460850 52829 57660 4442042 4438650 1148083 1150580 4093935 4089443 2032081 2031185 1910400 1912395 3852021 3853590 1073219 1068243 1986701 198...
output:
result:
Subtask #10:
score: 0
Runtime Error
Test #33:
score: 0
Runtime Error
input:
5000 73170491 768796521 670558 670089 2958025 2960803 5414101 5416526 4699271 4699813 4561016 4565915 7035233 7030323 1378672 1378584 4964463 4966800 7242131 7240789 8247079 8245607 9279015 9283822 6727462 6723335 438898 443570 8300373 8299256 6811203 6810475 3356946 3358886 3697156 3698634 2948112 ...