QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#186805 | #4910. Numbers | hos_lyric# | 28 | 120ms | 8332kb | C++14 | 8.5kb | 2023-09-24 11:59:38 | 2024-07-04 02:08:16 |
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;
// singular ==> return false;
// solution: a[j][n]
bool solveEq(vector<vector<Mint>> &a) {
const int n = a.size();
for (int h = 0; h < n; ++h) {
for (int i = h; i < n; ++i) if (a[i][h]) {
swap(a[h], a[i]);
break;
}
if (!a[h][h]) return false;
const Mint s = a[h][h].inv();
for (int j = h + 1; j <= n; ++j) a[h][j] *= s;
for (int i = h + 1; i < n; ++i) {
for (int j = h + 1; j <= n; ++j) a[i][j] -= a[i][h] * a[h][j];
}
}
for (int i = n; --i >= 0; ) {
for (int j = i + 1; j < n; ++j) a[i][n] -= a[i][j] * a[j][n];
}
return true;
}
int N, MO, I;
vector<int> L;
vector<Mint> P, D;
int M;
vector<int> LL;
namespace sub3 {
Mint run() {
vector<vector<int>> graph(M, vector<int>(N));
for (int u = 0; u < M; ++u) {
for (int i = 0; i < N; ++i) {
graph[u][i] = (u / LL[i] % L[i] == L[i] - 1) ? (u - LL[i] * (L[i] - 1)) : (u + LL[i]);
}
}
Mint ans = 1;
for (int t = 1; t < M; ++t) {
vector<vector<Mint>> a(M, vector<Mint>(M + 1, 0));
a[t][M] = 1;
for (int u = 0; u < M; ++u) {
a[u][u] = 1;
if (u != 0 && u != t) {
for (int i = 0; i < N; ++i) {
a[u][graph[u][i]] -= P[i];
}
}
}
const bool res = solveEq(a);
assert(res);
Mint sum = 0;
for (int i = 0; i < N; ++i) {
sum += P[i] * a[graph[0][i]][M];
}
ans += sum;
}
return ans;
}
} // sub3
namespace sub4 {
#define N do_not_use_N
#define M do_not_use_M
Mint f[1025][32][33];
Mint run() {
if (L[0] < L[1]) {
swap(L[0], L[1]);
swap(P[0], P[1]);
}
Mint ans = 1;
for (int tx = 0; tx < L[0]; ++tx) for (int ty = 0; ty < L[1]; ++ty) if (tx || ty) {
for (int x = 0; x <= L[0]; ++x) for (int y = 0; y < L[1]; ++y) {
fill(f[x][y], f[x][y] + (L[1] + 1), 0);
}
for (int y = 0; y < L[1]; ++y) {
f[L[0]][y][y] = 1;
}
for (int x = L[0]; --x >= 0; ) {
if (x == 0 || x == tx) {
const int y0 = (x == 0) ? 0 : ty;
for (int y = y0; ; ) {
if (x == 0 && y == 0) {
f[x][y][L[1]] = 0;
} else if (x == tx && y == ty) {
f[x][y][L[1]] = 1;
} else {
const int yy = (y + 1) % L[1];
for (int z = 0; z <= L[1]; ++z) {
f[x][y][z] = P[0] * f[x + 1][y][z] + P[1] * f[x][yy][z];
}
}
if (y == 0) y = L[1];
if (--y == y0) break;
}
} else {
{
Mint prob = P[0] / (1 - P[1].pow(L[1]));
for (int yy = 0; yy < L[1]; ++yy) {
for (int z = 0; z <= L[1]; ++z) {
f[x][0][z] += prob * f[x + 1][yy][z];
}
prob *= P[1];
}
}
for (int y = L[1]; --y >= 1; ) {
const int yy = (y + 1) % L[1];
for (int z = 0; z <= L[1]; ++z) {
f[x][y][z] = P[0] * f[x + 1][y][z] + P[1] * f[x][yy][z];
}
}
}
}
vector<vector<Mint>> a(L[1], vector<Mint>(L[1] + 1, 0));
for (int y = 0; y < L[1]; ++y) {
a[y][y] += 1;
for (int z = 0; z < L[1]; ++z) {
a[y][z] -= f[0][y][z];
}
a[y][L[1]] += f[0][y][L[1]];
}
const bool res = solveEq(a);
assert(res);
auto calc = [&](int x, int y) -> Mint {
Mint ret = 0;
for (int z = 0; z < L[1]; ++z) {
ret += f[x][y][z] * a[z][L[1]];
}
ret += f[x][y][L[1]];
return ret;
};
Mint sum = 0;
sum += P[0] * calc(1, 0);
sum += P[1] * calc(0, 1);
ans += sum;
}
return ans;
}
#undef N
#undef M
} // sub4
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;
Mint ans;
if (I == 1) {
ans = M;
} else if (I == 2 || I == 3) {
ans = sub3::run();
} else if (I == 4) {
ans = sub4::run();
}
printf("%u\n", ans.x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 2
Accepted
Test #1:
score: 2
Accepted
time: 2ms
memory: 8104kb
input:
1 1040016149 1 114514 86782 975423317
output:
114514
result:
ok 1 number(s): "114514"
Subtask #2:
score: 8
Accepted
Test #2:
score: 8
Accepted
time: 2ms
memory: 8044kb
input:
1 917829557 2 7 409960 84299716
output:
7
result:
ok 1 number(s): "7"
Test #3:
score: 0
Accepted
time: 0ms
memory: 8036kb
input:
2 1021037011 2 3 673845 456586624 2 557323 1021037010
output:
325765596
result:
ok 1 number(s): "325765596"
Test #4:
score: 0
Accepted
time: 2ms
memory: 7972kb
input:
2 974672641 2 2 919159 974672640 4 945246 788001635
output:
206340059
result:
ok 1 number(s): "206340059"
Test #5:
score: 0
Accepted
time: 2ms
memory: 8312kb
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: 9ms
memory: 8332kb
input:
2 1040469361 3 3 607396 156553896 20 622587 835710357
output:
212836966
result:
ok 1 number(s): "212836966"
Test #7:
score: 0
Accepted
time: 7ms
memory: 8328kb
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: 16ms
memory: 8060kb
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: 24ms
memory: 8004kb
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: 25ms
memory: 8044kb
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: 29ms
memory: 8064kb
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: 30ms
memory: 8004kb
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: 33ms
memory: 8056kb
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: 47ms
memory: 8032kb
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: 54ms
memory: 8040kb
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: 45ms
memory: 8028kb
input:
2 998244353 4 4 61786 911660635 238 287234 493901365
output:
223055892
result:
ok 1 number(s): "223055892"
Test #17:
score: 0
Accepted
time: 34ms
memory: 8124kb
input:
2 998244353 4 7 25813 683624219 112 96355 961521397
output:
97474170
result:
ok 1 number(s): "97474170"
Test #18:
score: 0
Accepted
time: 49ms
memory: 8024kb
input:
2 998244353 4 56 87114 727469702 14 24912 983690962
output:
592417090
result:
ok 1 number(s): "592417090"
Test #19:
score: 0
Accepted
time: 120ms
memory: 8124kb
input:
2 998244353 4 32 147776 617152567 28 775643 859007132
output:
566596649
result:
ok 1 number(s): "566596649"
Test #20:
score: 0
Accepted
time: 78ms
memory: 8280kb
input:
2 998244353 4 17 545281 464157011 56 816599 3898319
output:
469481867
result:
ok 1 number(s): "469481867"
Subtask #5:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 0ms
memory: 8320kb
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:
0
result:
wrong answer 1st numbers differ - expected: '72258463', found: '0'
Subtask #6:
score: 0
Skipped
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #6:
0%
Subtask #8:
score: 0
Wrong Answer
Dependency #4:
100%
Accepted
Test #51:
score: 0
Wrong Answer
time: 2ms
memory: 8248kb
input:
2 998244353 8 229376 553453 626702417 2 148397 998244352
output:
0
result:
wrong answer 1st numbers differ - expected: '942359197', found: '0'
Subtask #9:
score: 0
Skipped
Dependency #8:
0%
Subtask #10:
score: 0
Skipped
Dependency #5:
0%
Subtask #11:
score: 0
Skipped
Dependency #10:
0%
Subtask #12:
score: 0
Skipped
Dependency #7:
0%