QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#187054 | #4910. Numbers | hos_lyric | 48 | 2652ms | 3784kb | C++14 | 6.6kb | 2023-09-24 14:09:46 | 2023-09-24 14:09:46 |
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 <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")
////////////////////////////////////////////////////////////////////////////////
// Barrett
struct ModInt {
static unsigned M;
static unsigned long long NEG_INV_M;
static void setM(unsigned long long m) { M = m; NEG_INV_M = -1ULL / M; }
unsigned x;
ModInt() : x(0U) {}
ModInt(unsigned x_) : x(x_ % M) {}
ModInt(unsigned long long x_) : x(x_ % M) {}
ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
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) {
const unsigned long long y = static_cast<unsigned long long>(x) * a.x;
const unsigned long long q = static_cast<unsigned long long>((static_cast<unsigned __int128>(NEG_INV_M) * y) >> 64);
const unsigned long long r = y - M * q;
x = r - M * (r >= 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; }
};
unsigned ModInt::M;
unsigned long long ModInt::NEG_INV_M;
// !!!Use ModInt::setM!!!
////////////////////////////////////////////////////////////////////////////////
using Mint = ModInt;
/*
fix t != 0
F[u] := Pr[u -> t]
F[0] = 0
F[t] = 1
F[u] = \sum[i] P[i] F[u+e[i]] (u != 0, t)
F(x) = \sum[u] F[u] x^u
= x^t + \sum[u!=0,t] F[u] x^u
= x^t + \sum[u!=0,t] \sum[i] P[i] F[u+e[i]] x^u
= x^t + \sum[v] \sum[i] P[i] F[v] x^(v-e[i]) - \sum[i] P[i] F[e[i]] - \sum[i] P[i] F[t+e[i]] x^t
=: G(x) F(x) + a + b x^t
G(x) = \sum[i] P[i] x^(-i)
Pr[start -> t] = -a
F(1) = G(1) F(1) + a + b
G(1) = 1
b = -a
(1 - G(x) F(x) = a (x^0 - x^t)
\sum[w] F(w) = F[0] = 0
*/
int N, MO, I;
vector<int> L;
vector<Mint> P, D;
int M;
vector<int> LL;
void dft(vector<Mint> &fs, bool inv) {
for (int i = 0; i < N; ++i) {
const Mint d = inv ? D[i].inv() : D[i];
for (int u = 0; u < M; u += LL[i + 1]) for (int v = u; v < u + LL[i]; ++v) {
vector<Mint> as(L[i]);
for (int k = 0; k < L[i]; ++k) as[k] = fs[v + k * LL[i]];
vector<Mint> bs(L[i], 0);
Mint dk = 1;
for (int k = 0; k < L[i]; ++k) {
Mint dkl = 1;
for (int l = 0; l < L[i]; ++l) {
bs[k] += dkl * as[l];
dkl *= dk;
}
dk *= d;
}
for (int k = 0; k < L[i]; ++k) fs[v + k * LL[i]] = bs[k];
}
}
if (inv) {
const Mint invM = Mint(M).inv();
for (int u = 0; u < M; ++u) {
fs[u] *= invM;
}
}
}
int main() {
for (; ~scanf("%d%d%d", &N, &MO, &I); ) {
Mint::setM(MO);
L.resize(N);
P.resize(N);
D.resize(N);
for (int i = 0; i < N; ++i) {
scanf("%d%u%u", &L[i], &P[i].x, &D[i].x);
}
{
Mint sumP = 0;
for (int i = 0; i < N; ++i) {
sumP += P[i];
}
const Mint invSumP = sumP.inv();
for (int i = 0; i < N; ++i) {
P[i] *= invSumP;
}
}
LL.resize(N + 1);
LL[0] = 1;
for (int i = 0; i < N; ++i) {
LL[i + 1] = LL[i] * L[i];
}
M = LL[N];
cerr<<"N = "<<N<<", M = "<<M<<", I = "<<I<<", L = "<<L<<endl;
vector<Mint> gs(M, 0);
gs[0] += 1;
for (int i = 0; i < N; ++i) {
gs[(L[i] - 1) * LL[i]] -= P[i];
}
dft(gs, false);
assert(!gs[0]);
for (int u = 1; u < M; ++u) {
assert(gs[u]);
gs[u] = gs[u].inv();
}
Mint ans = 1;
for (int t = 1; t < M; ++t) {
vector<Mint> fs(M, 0);
fs[0] += 1;
fs[t] -= 1;
dft(fs, false);
fs[0] = 0;
for (int u = 1; u < M; ++u) {
fs[0] -= (fs[u] *= gs[u]);
}
dft(fs, true);
ans -= fs[t].inv();
}
printf("%u\n", ans.x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
1 1040016149 1 114514 86782 975423317
output:
result:
Subtask #2:
score: 8
Accepted
Test #2:
score: 8
Accepted
time: 0ms
memory: 3680kb
input:
1 917829557 2 7 409960 84299716
output:
7
result:
ok 1 number(s): "7"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3684kb
input:
2 1021037011 2 3 673845 456586624 2 557323 1021037010
output:
325765596
result:
ok 1 number(s): "325765596"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
2 974672641 2 2 919159 974672640 4 945246 788001635
output:
206340059
result:
ok 1 number(s): "206340059"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
3 942949663 2 2 900268 942949662 2 314911 942949662 2 488210 942949662
output:
697012073
result:
ok 1 number(s): "697012073"
Subtask #3:
score: 10
Accepted
Dependency #2:
100%
Accepted
Test #6:
score: 10
Accepted
time: 2ms
memory: 3700kb
input:
2 1040469361 3 3 607396 156553896 20 622587 835710357
output:
212836966
result:
ok 1 number(s): "212836966"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3684kb
input:
6 932284961 3 2 976786 932284960 2 296977 932284960 2 640048 932284960 2 883210 932284960 2 178849 932284960 2 292747 932284960
output:
767388139
result:
ok 1 number(s): "767388139"
Test #8:
score: 0
Accepted
time: 2ms
memory: 3736kb
input:
3 972511489 3 4 270846 275326774 6 901035 3644392 3 450749 3644391
output:
386017324
result:
ok 1 number(s): "386017324"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3756kb
input:
4 952654361 3 4 353315 567578568 2 265582 952654360 2 429959 952654360 5 62389 840524015
output:
942289666
result:
ok 1 number(s): "942289666"
Test #10:
score: 0
Accepted
time: 2ms
memory: 3776kb
input:
3 969859729 3 3 342202 745159492 9 270897 686337727 3 216159 745159492
output:
184152966
result:
ok 1 number(s): "184152966"
Test #11:
score: 0
Accepted
time: 2ms
memory: 3736kb
input:
3 953647801 3 7 943891 755724372 4 151642 109446108 3 775757 89434891
output:
811899700
result:
ok 1 number(s): "811899700"
Test #12:
score: 0
Accepted
time: 3ms
memory: 3708kb
input:
3 1029304937 3 4 54303 379091496 2 193487 1029304936 11 607170 762447147
output:
626421900
result:
ok 1 number(s): "626421900"
Test #13:
score: 0
Accepted
time: 3ms
memory: 3752kb
input:
3 904885561 3 3 554090 196965144 2 945499 904885560 15 747460 217098071
output:
676301027
result:
ok 1 number(s): "676301027"
Test #14:
score: 0
Accepted
time: 3ms
memory: 3700kb
input:
6 986788531 3 2 522554 986788530 2 316305 986788530 2 94022 986788530 2 249256 986788530 2 625960 986788530 3 405298 837112629
output:
441366932
result:
ok 1 number(s): "441366932"
Test #15:
score: 0
Accepted
time: 3ms
memory: 3784kb
input:
2 1023351421 3 20 337665 403345072 5 40276 480359844
output:
1002751099
result:
ok 1 number(s): "1002751099"
Subtask #4:
score: 8
Accepted
Test #16:
score: 8
Accepted
time: 2136ms
memory: 3684kb
input:
2 998244353 4 4 61786 911660635 238 287234 493901365
output:
223055892
result:
ok 1 number(s): "223055892"
Test #17:
score: 0
Accepted
time: 705ms
memory: 3708kb
input:
2 998244353 4 7 25813 683624219 112 96355 961521397
output:
97474170
result:
ok 1 number(s): "97474170"
Test #18:
score: 0
Accepted
time: 413ms
memory: 3668kb
input:
2 998244353 4 56 87114 727469702 14 24912 983690962
output:
592417090
result:
ok 1 number(s): "592417090"
Test #19:
score: 0
Accepted
time: 456ms
memory: 3664kb
input:
2 998244353 4 32 147776 617152567 28 775643 859007132
output:
566596649
result:
ok 1 number(s): "566596649"
Test #20:
score: 0
Accepted
time: 635ms
memory: 3700kb
input:
2 998244353 4 17 545281 464157011 56 816599 3898319
output:
469481867
result:
ok 1 number(s): "469481867"
Subtask #5:
score: 10
Accepted
Test #21:
score: 10
Accepted
time: 6ms
memory: 3680kb
input:
7 1023063703 5 2 265354 1023063702 2 526733 1023063702 2 685323 1023063702 2 856929 1023063702 2 116643 1023063702 2 909182 1023063702 2 533391 1023063702
output:
72258463
result:
ok 1 number(s): "72258463"
Test #22:
score: 0
Accepted
time: 25ms
memory: 3692kb
input:
8 909973201 5 2 803753 909973200 2 909951 909973200 2 686418 909973200 2 751586 909973200 2 596938 909973200 2 931460 909973200 2 613477 909973200 2 716815 909973200
output:
446664445
result:
ok 1 number(s): "446664445"
Test #23:
score: 0
Accepted
time: 102ms
memory: 3692kb
input:
9 1016555329 5 2 955958 1016555328 2 672234 1016555328 2 870436 1016555328 2 31291 1016555328 2 206731 1016555328 2 727640 1016555328 2 134125 1016555328 2 893866 1016555328 2 138706 1016555328
output:
808692189
result:
ok 1 number(s): "808692189"
Test #24:
score: 0
Accepted
time: 469ms
memory: 3688kb
input:
10 1007394217 5 2 961834 1007394216 2 209391 1007394216 2 715582 1007394216 2 553353 1007394216 2 213960 1007394216 2 589617 1007394216 2 666503 1007394216 2 407731 1007394216 2 152967 1007394216 2 848445 1007394216
output:
9280802
result:
ok 1 number(s): "9280802"
Test #25:
score: 0
Accepted
time: 467ms
memory: 3688kb
input:
10 968522221 5 2 21932 968522220 2 675564 968522220 2 378437 968522220 2 81037 968522220 2 963992 968522220 2 311430 968522220 2 699121 968522220 2 45417 968522220 2 275308 968522220 2 411066 968522220
output:
692011298
result:
ok 1 number(s): "692011298"
Subtask #6:
score: 12
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #26:
score: 12
Accepted
time: 180ms
memory: 3688kb
input:
4 972401761 6 2 972661 972401760 15 255992 126248662 12 878880 606133410 2 224371 972401760
output:
115576065
result:
ok 1 number(s): "115576065"
Test #27:
score: 0
Accepted
time: 255ms
memory: 3728kb
input:
4 904442113 6 4 455381 759185044 32 370753 618464921 3 500887 212337160 2 124789 904442112
output:
423186989
result:
ok 1 number(s): "423186989"
Test #28:
score: 0
Accepted
time: 1272ms
memory: 3684kb
input:
2 1044288001 6 200 909466 831914154 4 635879 349141366
output:
992703804
result:
ok 1 number(s): "992703804"
Test #29:
score: 0
Accepted
time: 1459ms
memory: 3740kb
input:
3 995557889 6 208 995170 150328412 2 962966 995557888 2 870197 995557888
output:
559608682
result:
ok 1 number(s): "559608682"
Test #30:
score: 0
Accepted
time: 270ms
memory: 3732kb
input:
3 926933113 6 4 600105 859154928 24 395570 903534782 9 869027 266882176
output:
586077830
result:
ok 1 number(s): "586077830"
Test #31:
score: 0
Accepted
time: 348ms
memory: 3760kb
input:
4 1018608697 6 28 136320 512857858 2 391617 1018608696 2 416876 1018608696 8 640950 421343802
output:
275871736
result:
ok 1 number(s): "275871736"
Test #32:
score: 0
Accepted
time: 263ms
memory: 3688kb
input:
5 909759601 6 10 839897 853552356 2 969093 909759600 4 795795 674156232 2 423545 909759600 6 600787 419741593
output:
241411297
result:
ok 1 number(s): "241411297"
Test #33:
score: 0
Accepted
time: 409ms
memory: 3716kb
input:
4 998016823 6 27 155873 262940187 2 158035 998016822 9 202421 553668428 2 21539 998016822
output:
118420215
result:
ok 1 number(s): "118420215"
Test #34:
score: 0
Accepted
time: 388ms
memory: 3736kb
input:
4 910702297 6 3 890425 906157449 24 469847 206340805 2 422607 910702296 7 67244 714860697
output:
836808927
result:
ok 1 number(s): "836808927"
Test #35:
score: 0
Accepted
time: 2652ms
memory: 3736kb
input:
2 965490689 6 4 468310 246473372 256 256288 754419658
output:
20502222
result:
ok 1 number(s): "20502222"
Subtask #7:
score: 0
Time Limit Exceeded
Dependency #6:
100%
Accepted
Test #36:
score: 0
Time Limit Exceeded
input:
4 1040169409 7 2 835454 1040169408 3 772774 636723579 4 342482 586400067 192 334236 48598666
output:
result:
Subtask #8:
score: 0
Time Limit Exceeded
Dependency #4:
100%
Accepted
Test #51:
score: 0
Time Limit Exceeded
input:
2 998244353 8 229376 553453 626702417 2 148397 998244352
output:
result:
Subtask #9:
score: 0
Skipped
Dependency #8:
0%
Subtask #10:
score: 0
Time Limit Exceeded
Dependency #5:
100%
Accepted
Test #71:
score: 0
Time Limit Exceeded
input:
15 969740263 10 2 373621 969740262 2 946569 969740262 2 253224 969740262 2 664561 969740262 2 611912 969740262 2 204304 969740262 2 746434 969740262 2 336578 969740262 2 200784 969740262 2 557632 969740262 2 651211 969740262 2 559106 969740262 2 610198 969740262 2 799763 969740262 2 908557 969740262
output:
result:
Subtask #11:
score: 0
Skipped
Dependency #10:
0%
Subtask #12:
score: 0
Skipped
Dependency #7:
0%