QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#626602#4045. 排列propane100 ✓432ms43108kbC++204.0kb2024-10-10 10:40:132024-10-10 10:40:14

Judging History

你现在查看的是最新测评结果

  • [2024-10-10 10:40:14]
  • 评测
  • 测评结果:100
  • 用时:432ms
  • 内存:43108kb
  • [2024-10-10 10:40:13]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<array>
#include<set>
#include<algorithm>
using namespace std;
using LL = long long;
const int mod = 1e9 + 7;

void add(int &a, int b){
    a += b;
    if (a >= mod) a -= mod;
}

int mul(int a, int b){
    return 1LL * a * b % mod;
}

const int maxn = 5e5 + 5;
vector<int> factor[maxn];
int inv[maxn];

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    inv[1] = 1;
    for(int i = 2; i < maxn; i++){
        inv[i] = mul(mod - mod / i, inv[mod % i]);
    }

    for(int i = 2; i < maxn; i++){
        if (factor[i].empty()){
            for(int j = i; j < maxn; j += i){
                factor[j].push_back(i);
            }
        }
    }

    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        vector<int> p(n + 1);
        for(int i = 1; i <= n; i++) cin >> p[i];
        vector<bool> v(n + 1);
        vector<int> cnt(n + 1);
        vector<array<int, 3> > mx(n + 1);
        for(int i = 1; i <= n; i++){
            if (!v[i]){
                int len = 0;
                for(int j = i; !v[j]; j = p[j]){
                    len += 1;
                    v[j] = true;
                }
                cnt[len] += 1;
                int t = len;
                for(auto x : factor[len]){
                    int c = 0;
                    while(t % x == 0) t /= x, c += 1;
                    for(int j = 0; j < 3; j++){
                        if (c > mx[x][j]){
                            swap(c, mx[x][j]);
                        }
                    }
                }
            }
        }
        vector<int> cand;
        for(int i = 1; i <= n; i++){
            if (cnt[i]){
                cand.push_back(i);
            }
        }
        int lcm = 1;
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < mx[i][0]; j++){
                lcm = mul(lcm, i);
            }
        }
        int ans = 0;
        for(int i = 0; i < cand.size(); i++){
            for(int j = i; j < cand.size(); j++){
                int x = cand[i], y = cand[j];
                int tot = 0;
                if (x == y) tot = mul(mul(cnt[x], cnt[x] - 1), mul(x, x));
                else tot = mul(2, mul(mul(cnt[x], x), mul(cnt[y], y)));

                if (tot == 0) continue;

                auto p = factor[x];
                p.insert(p.end(), factor[y].begin(), factor[y].end());
                p.insert(p.end(), factor[x + y].begin(), factor[x + y].end());
                sort(p.begin(), p.end());
                p.erase(unique(p.begin(), p.end()), p.end());
                
                int z = x + y;

                int nlcm = lcm;

                for(auto u : p){
                    multiset<int> s(mx[u].begin(), mx[u].end());
                    multiset<int>::iterator it;
                    for(auto v : {x, y}){
                        int c = 0;
                        while(v % u == 0) v /= u, c += 1;
                        it = s.find(c);
                        if (it != s.end()) s.erase(it);
                    }
                    {
                        int c = 0;
                        while(z % u == 0) z /= u, c += 1;
                        s.insert(c);
                    }
                    int cur = *s.rbegin();
                    if (cur < mx[u][0]){
                        for(int i = 0; i < mx[u][0] - cur; i++){
                            nlcm = mul(nlcm, inv[u]);
                        }
                    }
                    else{
                        for(int i = 0; i < cur - mx[u][0]; i++){
                            nlcm = mul(nlcm, u);
                        }
                    }
                }
                add(ans, mul(nlcm, tot));
            }
        }
        cout << ans << '\n';
    }

}

詳細信息


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 93ms
memory: 35308kb

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: 91ms
memory: 35072kb

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: 56ms
memory: 33140kb

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: 63ms
memory: 33184kb

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: 63ms
memory: 33508kb

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: 95ms
memory: 35144kb

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: 89ms
memory: 35108kb

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: 127ms
memory: 35056kb

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: 381ms
memory: 43108kb

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: 432ms
memory: 42984kb

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