QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#290687 | #6161. 作业题 | MoRanSky | 100 ✓ | 126ms | 4540kb | C++23 | 2.1kb | 2023-12-25 06:27:10 | 2023-12-25 06:27:11 |
Judging History
answer
// Skyqwq
#include <iostream>
#include <cstdio>
#include <cstring>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> Poly;
const int N = 31, M = N * N, P = 998244353, S = 153000;
int n, m, U[M], V[M], W[M], f[S], mx, ans;
Poly a[N][N];
int inline power(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (LL) res * a % P;
a = (LL)a * a % P;
b >>= 1;
}
return res;
}
Poly operator + (Poly a, Poly b) {
return (Poly) { (a.x + b.x) % P, (a.y + b.y) % P };
}
Poly operator - (Poly a, Poly b) {
return (Poly) { (a.x - b.x + P) % P, (a.y - b.y + P) % P };
}
Poly operator * (Poly a, Poly b) {
return (Poly) { (LL)a.x * b.x % P, ((LL)a.x * b.y + (LL)a.y * b.x) % P };
}
Poly inline inv(Poly a) {
int I = power(a.x, P - 2);
return (Poly) { I, (P - (LL)a.y * I % P * I % P) % P };
}
Poly operator / (Poly a, Poly b) {
return a * inv(b);
}
void inline add(int u, int v, int w) {
Poly t = (Poly) { 1, w };
a[u][u] = a[u][u] + t, a[v][v] = a[v][v] + t;
a[u][v] = a[u][v] - t, a[v][u] = a[v][u] - t;
}
int inline det() {
int f = 1;
Poly res = (Poly) { 1, 0 };
for (int i = 1; i <= n; i++) {
if (!a[i][i].x) {
for (int j = i + 1; j <= n; j++) {
if (a[j][i].x) {
f *= -1;
swap(a[i], a[j]);
break;
}
}
}
if (!a[i][i].x && !a[i][i].y) return 0;
for (int j = i + 1; j <= n; j++) {
Poly t = a[j][i] / a[i][i];
for (int k = i; k <= n; k++)
a[j][k] = a[j][k] - a[i][k] * t;
}
res = res * a[i][i];
}
return (res.y * f + P) % P;
}
int inline calc(int k) {
memset(a, 0, sizeof a);
int cnt = 0;
for (int i = 1; i <= m; i++)
if (W[i] % k == 0) cnt++, add(U[i], V[i], W[i]);
if (cnt < n - 1) return 0;
return det();
}
int main() {
scanf("%d%d", &n, &m); --n;
for (int i = 1; i <= m; i++) scanf("%d%d%d", U + i, V + i, W + i), mx = max(mx, W[i]);
for (int i = 1; i <= mx; i++) f[i] = calc(i);
for (int i = mx; i; i--) {
for (int j = 2 * i; j <= mx; j += i) f[i] = (f[i] - f[j] + P) % P;
ans = (ans + (LL)i * f[i]) % P;
}
printf("%d\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 14ms
memory: 4428kb
input:
5 10 1 2 113400 1 3 131040 1 4 131040 1 5 126000 2 3 131040 2 4 90720 2 5 138600 3 4 110880 3 5 110880 4 5 95760
output:
549298858
result:
ok 1 number(s): "549298858"
Test #2:
score: 10
Accepted
time: 16ms
memory: 4472kb
input:
6 15 1 2 110880 1 3 141120 1 4 141120 1 5 131040 1 6 151200 2 3 137280 2 4 105840 2 5 138600 2 6 150480 3 4 138600 3 5 131040 3 6 137280 4 5 85680 4 6 151200 5 6 131040
output:
627752274
result:
ok 1 number(s): "627752274"
Test #3:
score: 10
Accepted
time: 20ms
memory: 4500kb
input:
25 24 1 2 147840 1 3 98280 2 4 120960 3 5 128520 5 6 128520 6 7 143640 1 8 138600 7 9 131040 3 10 147840 10 11 120120 5 12 120120 4 13 131040 8 14 151200 13 15 151200 5 16 110880 11 17 120960 6 18 151200 6 19 147840 5 20 147840 1 21 120960 14 22 110880 13 23 120120 18 24 98280 1 25 131040
output:
623404094
result:
ok 1 number(s): "623404094"
Test #4:
score: 10
Accepted
time: 22ms
memory: 4436kb
input:
28 28 1 2 128520 2 3 98280 1 4 138600 4 5 98280 2 6 98280 1 7 131040 3 8 138600 2 9 147840 6 10 120120 1 11 120960 7 12 138600 6 13 147840 3 14 120960 6 15 120960 9 16 120120 4 17 143640 3 18 128520 15 19 147840 9 20 120120 6 21 120960 20 22 147840 10 23 131040 5 24 138600 7 25 143640 5 26 120960 4 ...
output:
640377458
result:
ok 1 number(s): "640377458"
Test #5:
score: 10
Accepted
time: 24ms
memory: 4540kb
input:
30 30 1 2 110880 1 3 120960 3 4 131040 3 5 120120 3 6 138600 5 7 98280 3 8 120960 5 9 128520 1 10 128520 2 11 138600 5 12 98280 10 13 98280 11 14 143640 3 15 120960 10 16 98280 16 17 138600 5 18 138600 13 19 147840 18 20 120960 13 21 131040 15 22 151200 22 23 131040 2 24 128520 6 25 120960 20 26 138...
output:
309797364
result:
ok 1 number(s): "309797364"
Test #6:
score: 10
Accepted
time: 21ms
memory: 4432kb
input:
25 25 1 2 131040 2 3 120120 2 4 98280 3 5 110880 5 6 120120 4 7 120960 1 8 128520 4 9 110880 3 10 147840 7 11 120960 4 12 120960 2 13 138600 8 14 120960 1 15 128520 8 16 131040 10 17 110880 14 18 98280 9 19 131040 7 20 147840 8 21 138600 14 22 138600 5 23 151200 7 24 131040 13 25 143640 12 1 143640
output:
302382070
result:
ok 1 number(s): "302382070"
Test #7:
score: 10
Accepted
time: 111ms
memory: 4540kb
input:
28 376 1 2 152501 1 3 152501 1 4 152501 1 5 152501 1 6 152501 1 7 152501 1 8 152501 1 9 152501 1 10 152501 1 11 152501 1 12 152501 1 13 152501 1 14 152501 1 15 152501 1 16 152501 1 17 152501 1 18 152501 1 19 152501 1 20 152501 1 21 152501 1 22 152501 1 23 152501 1 24 152501 1 25 152501 1 26 152501 1...
output:
493255263
result:
ok 1 number(s): "493255263"
Test #8:
score: 10
Accepted
time: 126ms
memory: 4512kb
input:
30 431 1 2 152501 1 3 152501 1 4 152501 1 5 152501 1 6 152501 1 7 152501 1 8 152501 1 9 152501 1 10 152501 1 11 152501 1 12 152501 1 13 152501 1 14 152501 1 15 152501 1 16 152501 1 17 152501 1 18 152501 1 19 152501 1 20 152501 1 21 152501 1 22 152501 1 23 152501 1 24 152501 1 25 152501 1 26 152501 1...
output:
441996120
result:
ok 1 number(s): "441996120"
Test #9:
score: 10
Accepted
time: 110ms
memory: 4512kb
input:
28 372 1 2 152501 1 3 152501 1 4 152501 1 5 152501 1 6 152501 1 7 152501 1 8 152501 1 9 152501 1 10 152501 1 11 152501 1 12 152501 1 13 152501 1 14 152501 1 15 152501 1 16 152501 1 17 152501 1 18 152501 1 19 152501 1 20 152501 1 21 152501 1 22 152501 1 23 152501 1 24 152501 1 25 152501 1 26 152501 1...
output:
179882346
result:
ok 1 number(s): "179882346"
Test #10:
score: 10
Accepted
time: 126ms
memory: 4436kb
input:
30 432 1 2 152501 1 3 152501 1 4 152501 1 5 152501 1 6 152501 1 7 152501 1 8 152501 1 9 152501 1 10 152501 1 11 152501 1 12 152501 1 13 152501 1 14 152501 1 15 152501 1 16 152501 1 17 152501 1 18 152501 1 19 152501 1 20 152501 1 21 152501 1 22 152501 1 23 152501 1 24 152501 1 25 152501 1 26 152501 1...
output:
45748263
result:
ok 1 number(s): "45748263"
Extra Test:
score: 0
Extra Test Passed