QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#186702 | #4910. Numbers | hos_lyric# | 20 | 53ms | 4108kb | C++14 | 6.3kb | 2023-09-24 10:21:08 | 2024-07-04 02:08:14 |
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, M, I;
vector<int> L;
vector<Mint> P, D;
int U;
vector<int> LL;
namespace sub3 {
Mint run() {
vector<vector<int>> graph(U, vector<int>(N));
for (int u = 0; u < U; ++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 < U; ++t) {
vector<vector<Mint>> a(U, vector<Mint>(U + 1, 0));
a[t][U] = 1;
for (int u = 0; u < U; ++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]][U];
}
ans += sum;
}
return ans;
}
} // sub3
int main() {
for (; ~scanf("%d%d%d", &N, &M, &I); ) {
Mint::setM(M);
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];
}
U = LL[N];
cerr<<"N = "<<N<<", I = "<<I<<", L = "<<L<<", U = "<<U<<endl;
Mint ans;
if (I == 1) {
ans = U;
} else if (I == 2 || I == 3) {
ans = sub3::run();
}
printf("%u\n", ans.x);
}
return 0;
}
詳細信息
Subtask #1:
score: 2
Accepted
Test #1:
score: 2
Accepted
time: 1ms
memory: 3876kb
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: 0ms
memory: 3804kb
input:
1 917829557 2 7 409960 84299716
output:
7
result:
ok 1 number(s): "7"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3804kb
input:
2 1021037011 2 3 673845 456586624 2 557323 1021037010
output:
325765596
result:
ok 1 number(s): "325765596"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3876kb
input:
2 974672641 2 2 919159 974672640 4 945246 788001635
output:
206340059
result:
ok 1 number(s): "206340059"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3792kb
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: 8ms
memory: 4096kb
input:
2 1040469361 3 3 607396 156553896 20 622587 835710357
output:
212836966
result:
ok 1 number(s): "212836966"
Test #7:
score: 0
Accepted
time: 10ms
memory: 3904kb
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: 3820kb
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: 19ms
memory: 3816kb
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: 24ms
memory: 3896kb
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: 28ms
memory: 3820kb
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: 33ms
memory: 4108kb
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: 35ms
memory: 3828kb
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: 3832kb
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: 53ms
memory: 3856kb
input:
2 1023351421 3 20 337665 403345072 5 40276 480359844
output:
1002751099
result:
ok 1 number(s): "1002751099"
Subtask #4:
score: 0
Wrong Answer
Test #16:
score: 0
Wrong Answer
time: 0ms
memory: 4092kb
input:
2 998244353 4 4 61786 911660635 238 287234 493901365
output:
0
result:
wrong answer 1st numbers differ - expected: '223055892', found: '0'
Subtask #5:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 0ms
memory: 3796kb
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:
0%
Subtask #7:
score: 0
Skipped
Dependency #6:
0%
Subtask #8:
score: 0
Skipped
Dependency #4:
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%