UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#185266#31. MSTnodgd100527ms5604kbC++2.5kb2023-09-26 15:06:552023-09-26 15:06:56

answer

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 40 + 5;
const int MAX_N2 = 800 + 5;
const int P = 1000000007;

int N, M, w[MAX_N];
int pm[MAX_N2][MAX_N2];

struct State {
    int m, cnt[MAX_N];
    State() {
        clear();
    }
    void clear() {
        m = 0;
        for (int i = 1; i <= N; i ++) cnt[i] = 0;
    }
    inline bool operator < (const State &b) const {
        if (m != b.m) {
            return m < b.m;
        }
        for (int i = 1; i <= N; i ++) {
            if (cnt[i] != b.cnt[i]) {
                return cnt[i] < b.cnt[i];
            }
        }
        return false;
    }
    inline bool operator == (const State &b) const {
        if (m != b.m) {
            return false;
        }
        for (int i = 1; i <= N; i ++) {
            if (cnt[i] != b.cnt[i]) {
                return false;
            }
        }
        return true;
    }
};

int main() {
    scanf("%d", &N);
    M = N * (N - 1) >> 1;
    for (int i = 1; i < N; i ++) {
        scanf("%d", &w[i]);
    }
    w[N] = M + 1;
    for (int i = 0; i <= M; i ++) {
        pm[i][0] = 1;
        for (int j = 1; j <= i; j ++) {
            pm[i][j] = 1ll * pm[i][j - 1] * (i - j + 1) % P;
        }
    }
    map<State, int> f;
    queue<State> q;
    State s, ss;
    s.m = 1, s.cnt[1] = N;
    f[s] = 1, q.push(s);
    while (!q.empty()) {
        s = q.front();
        q.pop();
        int fs = f[s];
        f.erase(s);
        int e = 0;
        for (int i = 1; i <= N; i ++) {
            e += (i * (i - 1) >> 1) * s.cnt[i];
        }
        e -= w[s.m] - 1;
        if (e < 0) continue;
        if (s.m == N) {
            printf("%d\n", fs);
            break;
        }
        for (int i = 1; i <= N; i ++) {
            if (!s.cnt[i]) continue;
            for (int j = i + (s.cnt[i] == 1); j <= N; j ++) {
                if (!s.cnt[j]) continue;
                int tk = i * j * (i == j ? s.cnt[i] * (s.cnt[i] - 1) >> 1 : s.cnt[i] * s.cnt[j]);
                int tp = pm[e + i * j - 1][w[s.m + 1] - w[s.m] - 1];
                int tf = 1ll * fs * tk % P * tp % P;
                ss = s;
                ss.cnt[i] --;
                ss.cnt[j] --;
                ss.cnt[i + j] ++;
                ss.m ++;
                if (!f.count(ss)) {
                    f[ss] = 0;
                    q.push(ss);
                }
                int &fss = f[ss];
                fss += tf, fss -= fss >= P ? P : 0;
            }
        }
    }
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 1260kb

input:

4
1 2 3

output:

576

result:

ok single line: '576'

Test #2:

score: 5
Accepted
time: 0ms
memory: 1260kb

input:

4
1 2 4

output:

144

result:

ok single line: '144'

Test #3:

score: 5
Accepted
time: 0ms
memory: 1276kb

input:

5
1 2 3 4

output:

2160000

result:

ok single line: '2160000'

Test #4:

score: 5
Accepted
time: 0ms
memory: 1276kb

input:

5
1 2 3 6

output:

276480

result:

ok single line: '276480'

Test #5:

score: 5
Accepted
time: 0ms
memory: 1304kb

input:

6
1 2 3 4 5

output:

350972052

result:

ok single line: '350972052'

Test #6:

score: 5
Accepted
time: 0ms
memory: 1292kb

input:

6
1 2 4 7 11

output:

12441600

result:

ok single line: '12441600'

Test #7:

score: 5
Accepted
time: 0ms
memory: 1324kb

input:

7
1 2 3 4 5 6

output:

373181939

result:

ok single line: '373181939'

Test #8:

score: 5
Accepted
time: 0ms
memory: 1324kb

input:

7
1 2 3 6 7 10

output:

655119719

result:

ok single line: '655119719'

Test #9:

score: 5
Accepted
time: 0ms
memory: 1608kb

input:

15
1 2 3 4 5 6 7 8 9 10 11 12 13 14

output:

778980097

result:

ok single line: '778980097'

Test #10:

score: 5
Accepted
time: 0ms
memory: 1612kb

input:

15
1 2 3 7 8 10 11 13 15 18 21 25 27 30

output:

72110308

result:

ok single line: '72110308'

Test #11:

score: 5
Accepted
time: 3ms
memory: 1912kb

input:

20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

output:

521592645

result:

ok single line: '521592645'

Test #12:

score: 5
Accepted
time: 0ms
memory: 1888kb

input:

20
1 2 3 5 7 9 11 16 21 26 28 32 37 41 42 47 48 50 55

output:

185224996

result:

ok single line: '185224996'

Test #13:

score: 5
Accepted
time: 3ms
memory: 2340kb

input:

25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

output:

83240183

result:

ok single line: '83240183'

Test #14:

score: 5
Accepted
time: 5ms
memory: 2328kb

input:

25
1 2 4 7 10 13 15 16 17 18 21 25 27 31 36 40 42 43 47 52 56 60 63 64

output:

649967979

result:

ok single line: '649967979'

Test #15:

score: 5
Accepted
time: 21ms
memory: 2908kb

input:

30
1 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

output:

587585839

result:

ok single line: '587585839'

Test #16:

score: 5
Accepted
time: 16ms
memory: 2908kb

input:

30
1 2 3 5 10 12 16 20 24 27 28 29 31 32 36 41 44 45 49 51 54 58 62 66 68 70 74 79 84

output:

219192987

result:

ok single line: '219192987'

Test #17:

score: 5
Accepted
time: 59ms
memory: 3900kb

input:

35
1 2 3 5 8 9 12 13 14 15 18 21 25 30 33 36 38 41 43 45 49 54 57 59 64 69 70 74 78 80 81 83 87 89

output:

758370424

result:

ok single line: '758370424'

Test #18:

score: 5
Accepted
time: 48ms
memory: 3828kb

input:

35
1 2 4 7 9 13 15 19 23 28 32 36 40 41 43 47 49 50 55 56 61 64 67 69 72 75 76 78 80 85 90 93 95 98

output:

509074597

result:

ok single line: '509074597'

Test #19:

score: 5
Accepted
time: 184ms
memory: 5508kb

input:

40
1 2 4 6 8 12 15 16 19 21 26 28 31 35 36 38 41 43 47 50 55 57 58 63 65 69 74 76 78 83 85 90 91 92 ...

output:

124674859

result:

ok single line: '124674859'

Test #20:

score: 5
Accepted
time: 188ms
memory: 5604kb

input:

40
1 2 4 5 7 12 13 16 18 23 27 28 32 35 39 42 43 45 47 50 53 54 57 59 61 66 70 72 77 82 87 91 96 98 ...

output:

186165705

result:

ok single line: '186165705'