QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#591534 | #4045. 排列 | WTR2007 | 100 ✓ | 173ms | 38344kb | C++20 | 3.6kb | 2024-09-26 16:20:07 | 2024-09-26 16:20:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int N = 1000050;
int a[N], c[N], now[N], p[N], inv[N], mx[3][N];
bool vis[2][N], r[N];
vector<pair<int, int>> e[N];
vector<int> Prime, q;
inline int read() {
int w = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
w = (w << 1) + (w << 3) + ch - 48;
ch = getchar();
}
return w;
}
inline int Pow(int a, int b) {
int ans = 1;
for (; b; b >>= 1, a *= a) if (b & 1) ans *= a;
return ans;
}
inline void Solve() {
int n, lcm = 1, ans = 0;
n = read();
for (int i = 1; i <= n; i++) a[i] = read(), r[i] = 0;
for (int i = 1; i <= n; i++) {
if (r[i]) continue;
int sz = 1; r[i] = 1;
for (int p = a[i]; p != i; p = a[p]) sz++, r[p] = 1;
q.push_back(sz); c[sz]++;
}
sort(q.begin(), q.end());
q.erase(unique(q.begin(), q.end()), q.end());
if ((int)q.size() == 1 && c[q[0]] == 1) {
for (auto x : q) c[x] = 0;
vector<int>().swap(q);
printf("0\n");
return ;
}
for (auto t : q) {
int opt = t;
while (opt > 1) {
int P = p[opt], s = 0;
while (opt % P == 0) opt /= P, s++;
e[t].push_back({P, s});
for (int j = 0; j < c[t]; j++)
for (int cs = s, i = 0; i <= 2; i++)
if (mx[i][P] < cs) swap(mx[i][P], cs);
}
}
for (auto P : Prime) {
lcm = 1ll * lcm * Pow(P, mx[0][P]) % MOD;
now[P] = mx[0][P];
}
auto Delete = [&](int x, int t) {
if (!vis[0][x] && mx[0][x] == t) vis[0][x] = 1;
else if (!vis[1][x] && mx[1][x] == t) vis[1][x] = 1;
};
for (auto i : q) {
for (auto j : q) {
if (i == j && c[i] == 1) continue;
int res = lcm, opt = i + j;
vector<int> h;
for (auto [x, y] : e[i]) Delete(x, y), h.push_back(x);
for (auto [x, y] : e[j]) Delete(x, y), h.push_back(x);
sort(h.begin(), h.end());
h.erase(unique(h.begin(), h.end()), h.end());
for (auto P : h) {
now[P] = 0;
if (!vis[0][P]) now[P] = mx[0][P];
else if (!vis[1][P]) now[P] = mx[1][P];
else now[P] = mx[2][P];
res = 1ll * res * inv[Pow(P, mx[0][P] - now[P])] % MOD;
}
while (opt > 1) {
int P = p[opt], s = 0;
while (opt % P == 0) opt /= P, s++;
if (s > now[P]) res = 1ll * res * Pow(P, s - now[P]) % MOD;
}
if (i == j) ans += 1ll * res * c[i] % MOD * (c[i] - 1) % MOD * i % MOD * i % MOD;
else ans += 1ll * res * c[i] % MOD * c[j] % MOD * i % MOD * j % MOD;
ans %= MOD;
for (auto P : h) {
vis[0][P] = vis[1][P] = 0;
now[P] = mx[0][P];
}
}
}
printf("%d\n", ans);
for (auto x : q)
c[x] = 0, vector<pair<int, int>>().swap(e[x]);
for (auto P : Prime) mx[0][P] = mx[1][P] = mx[2][P] = 0;
vector<int>().swap(q);
}
signed main() {
int T;
T = read(); inv[1] = 1;
for (int i = 2; i <= N - 5; i++) {
inv[i] = MOD - 1ll * inv[MOD % i] * (MOD / i) % MOD;
if (!p[i]) p[i] = i, Prime.push_back(i);
for (auto P : Prime) {
if (i * P > N - 5) break;
p[i * P] = P;
if (i % P == 0) break;
}
}
while (T--) Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 21ms
memory: 20484kb
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: 14ms
memory: 20480kb
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: 7ms
memory: 33144kb
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: 12ms
memory: 32260kb
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: 11ms
memory: 32688kb
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: 27ms
memory: 31868kb
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: 25ms
memory: 32716kb
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: 36ms
memory: 33064kb
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: 152ms
memory: 38344kb
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: 173ms
memory: 35764kb
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