QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#291091 | #4045. 排列 | MoRanSky | 100 ✓ | 213ms | 75676kb | C++23 | 3.4kb | 2023-12-26 04:48:48 | 2023-12-26 04:48:48 |
Judging History
answer
// Skyqwq
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef pair<int, int> PII;
template <typename T> void inline read(T &x) {
x = 0; char s = getchar(); int f = 1;
while (s < '0' || s > '9') { if (s == '-') { f = -1; } s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
template <typename T> bool inline chkMax(T &x, T y) { return y > x ? x = y, 1 : 0; }
template <typename T> bool inline chkMin(T &x, T y) { return y < x ? x = y, 1 : 0; }
const int P = 1e9 + 7, N = 5e5 + 5;
int n, a[N], f[N], sz[N], c[N], d[N], t, inv[N];
int find(int x) {
return x == f[x] ? x : f[x] = find(f[x]);
}
void inline merge(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
if (sz[x] > sz[y]) swap(x, y);
f[x] = y, sz[y] += sz[x];
}
vector<int> g[N];
vector<PII> e[N];
bool st[N];
int pr[N], tot, p0[N];
vector<PII> fc[N];
void inline clr() {
t = 0;
for (int i = 1; i <= n; i++) c[i] = d[i] = 0, g[i].clear();
}
int now, cnt[N];
int inline get(int x) {
int mx = 1;
for (int v: g[x]) cnt[v]++;
for (PII v: e[x]) cnt[v.fi] += v.se;
for (int v: g[x])
if (cnt[v]) chkMax(mx, v), cnt[v] = 0;
for (PII v: e[x])
if (cnt[v.fi]) chkMax(mx, v.fi), cnt[v.fi] = 0;
return mx;
}
void inline chg(int x, int d) {
for (PII o: fc[x]) {
now = 1ll * now * inv[get(o.fi)] % P;
e[o.fi].pb(mp(o.se, d));
now = 1ll * now * get(o.fi) % P;
}
}
void inline del(int x) {
for (PII o: fc[x])
e[o.fi].clear();
}
void inline add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void inline work() {
now = 1;
for (int i = 1; i <= n; i++) {
if (g[i].size()) {
sort(g[i].begin(), g[i].end(), greater<int>() );
while (g[i].size() > 3) g[i].pop_back();
now = 1ll * now * g[i][0] % P;
}
}
int ans = 0;
for (int i = 1; i <= t; i++) {
int u = d[i];
if (c[u] > 1) {
int la = now;
chg(u + u, 1);
chg(u, -1);
chg(u, -1);
del(u + u); del(u);
add(ans, 1ll * now * c[u] % P * (c[u] - 1) % P * u % P * u % P);
now = la;
}
for (int j = i + 1; j <= t; j++) {
int v = d[j];
int la = now;
chg(u + v, 1), chg(u, -1), chg(v, -1);
add(ans, 2ll * now * c[u] % P * c[v] % P * u % P * v % P);
del(u + v), del(u), del(v);
now = la;
}
}
printf("%d\n", ans);
}
void inline apd(int x) {
for (PII o: fc[x])
g[o.fi].pb(o.se);
}
void inline prw(int n) {
p0[1] = 1;
inv[1] = 1;
for (int i = 2; i <= n; i++) {
inv[i] = ((LL)P - P / i) * inv[P % i] % P;
}
for (int i = 2; i <= n; i++) {
if (!st[i]) pr[++tot] = i, p0[i] = i;
for (int j = 1; pr[j] * i <= n; j++) {
st[i * pr[j]] = 1;
p0[i * pr[j]] = pr[j];
if (i % pr[j] == 0) break;
}
}
for (int i = 1; i <= n; i++) {
int x = i;
while (x != 1) {
int p = p0[x], v = 1;
while (x % p == 0) v *= p, x /= p;
fc[i].pb(mp(p, v));
}
}
}
int main() {
//freopen("perm.in", "r", stdin);
//freopen("perm.out", "w", stdout);
prw(5e5);
int T; read(T);
while (T--) {
read(n);
for (int i = 1; i <= n; i++) read(a[i]), f[i] = i, sz[i] = 1;
for (int i = 1; i <= n; i++) merge(i, a[i]);
for (int i = 1; i <= n; i++) {
if (find(i) == i) {
c[sz[i]]++;
apd(sz[i]);
}
}
for (int i = 1; i <= n; i++)
if (c[i]) d[++t] = i;
work();
clr();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 51ms
memory: 73076kb
input:
5 98005 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10...
output:
0 0 0 0 0
result:
ok 5 number(s): "0 0 0 0 0"
Test #2:
score: 10
Accepted
time: 52ms
memory: 74168kb
input:
5 96008 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10...
output:
0 0 0 0 0
result:
ok 5 number(s): "0 0 0 0 0"
Test #3:
score: 10
Accepted
time: 49ms
memory: 74236kb
input:
5 35 18 10 30 13 20 32 33 34 27 28 9 6 3 8 1 26 4 2 17 25 7 12 31 5 24 35 11 15 29 23 19 16 21 22 14 35 31 6 24 28 25 20 18 17 30 12 4 15 11 8 22 19 29 34 5 27 33 13 14 7 21 9 35 2 16 10 1 26 32 23 3 35 14 32 27 2 4 21 9 8 24 15 11 33 22 6 19 1 31 26 28 23 16 3 18 25 10 35 17 20 34 30 29 7 13 12 5 3...
output:
264720 4620 171700 43512 118560
result:
ok 5 number(s): "264720 4620 171700 43512 118560"
Test #4:
score: 10
Accepted
time: 30ms
memory: 74272kb
input:
5 197 185 145 76 108 112 8 83 176 171 141 162 38 102 136 122 152 166 11 1 135 130 42 87 69 113 115 138 37 30 110 164 137 82 43 183 14 64 99 49 51 153 182 146 59 24 197 40 60 2 117 128 16 191 28 196 179 97 161 192 57 32 85 58 175 13 89 187 54 75 168 103 68 62 180 100 70 53 140 111 23 50 116 159 12 94...
output:
4238652 353540880 230360760 352397593 962380654
result:
ok 5 number(s): "4238652 353540880 230360760 352397593 962380654"
Test #5:
score: 10
Accepted
time: 45ms
memory: 74828kb
input:
5 2458 1924 1946 1771 473 1896 756 722 87 85 245 1408 605 1410 313 495 127 213 915 964 1358 1599 2356 1509 1820 844 1299 809 1525 64 1356 1067 857 1865 1637 519 2012 338 1516 1949 309 2259 182 921 73 1862 1014 1541 2125 1608 1416 930 2338 981 1193 1858 901 2392 277 2288 68 1253 28 365 760 1811 2127 ...
output:
978052529 242234656 117060060 927865953 609414491
result:
ok 5 number(s): "978052529 242234656 117060060 927865953 609414491"
Test #6:
score: 10
Accepted
time: 63ms
memory: 75632kb
input:
5 99185 26839 61121 45992 14403 65124 24864 24199 33689 29856 380 33507 43200 19519 25796 37723 6125 75456 95552 69200 20 41718 90058 1095 17598 26502 48916 17135 71629 47605 87901 81017 8068 294 5119 6766 13238 94076 81234 66485 97356 8495 7138 67357 78520 22079 28469 62820 10519 29849 85393 94754 ...
output:
30625137 354057431 257356181 679385499 743281292
result:
ok 5 number(s): "30625137 354057431 257356181 679385499 743281292"
Test #7:
score: 10
Accepted
time: 51ms
memory: 74956kb
input:
5 97719 54330 7287 42502 46748 89696 26766 73785 45779 78745 77009 12843 68346 70268 86363 9338 37991 8250 42092 65636 42830 94460 20829 70115 51051 42361 42787 40552 28200 31627 39620 62229 5783 63051 71265 23966 55290 91270 15488 51871 9502 27171 8529 18288 96742 2458 91419 92000 90167 41768 64543...
output:
557314000 693919687 209770156 961373841 829047726
result:
ok 5 number(s): "557314000 693919687 209770156 961373841 829047726"
Test #8:
score: 10
Accepted
time: 78ms
memory: 75676kb
input:
5 98247 5710 49877 43452 94001 96873 64582 12275 27479 10333 15298 40636 16574 45960 24260 22082 52701 48364 72346 40713 39476 96997 18048 73992 61216 33184 17676 17070 52531 51967 94266 8416 14666 54237 24907 7626 5477 15042 60115 6627 43141 86307 20699 88929 51763 78632 31451 77977 60138 54936 181...
output:
53218185 842910290 715332616 936936023 802603566
result:
ok 5 number(s): "53218185 842910290 715332616 936936023 802603566"
Test #9:
score: 10
Accepted
time: 204ms
memory: 75372kb
input:
5 478810 212740 317409 24031 371981 433384 170780 64048 152611 228849 320502 138270 124681 107152 366209 195005 386784 25774 409661 283786 33634 385169 225292 244770 159952 51564 417197 308732 216201 439365 95461 275072 103286 175283 107672 361963 178996 73879 117794 111044 215371 301840 447509 4055...
output:
432340839 538243127 736567741 789874152 81799729
result:
ok 5 number(s): "432340839 538243127 736567741 789874152 81799729"
Test #10:
score: 10
Accepted
time: 213ms
memory: 75112kb
input:
5 495437 15542 448191 294627 367556 66964 387494 217813 228809 59154 222553 220684 195206 161876 108475 341708 249137 200490 465901 171655 463006 292802 300999 366933 308286 396686 54874 133267 369496 478958 401677 469352 472285 110117 5694 408189 446558 384897 110566 281732 348029 92707 278977 4789...
output:
255314907 598285290 204270639 686469720 457337304
result:
ok 5 number(s): "255314907 598285290 204270639 686469720 457337304"
Extra Test:
score: 0
Extra Test Passed