QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#340808#990. Colorful Componentsucup-team1209#AC ✓85ms4824kbC++203.2kb2024-02-29 12:34:482024-02-29 12:34:49

Judging History

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

  • [2024-02-29 12:34:49]
  • 评测
  • 测评结果:AC
  • 用时:85ms
  • 内存:4824kb
  • [2024-02-29 12:34:48]
  • 提交

answer

#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;

cs int N = 300 + 5; 

using ll = long long; 
cs int mod = 1e9 + 7; 
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int dec(int a, int b) { return a - b < 0 ? a - b + mod : a - b ; }
int mul(int a, int b) { return 1ll * a * b % mod; }
void Add(int &a, int b) { a = add(a, b);}
void Dec(int &a, int b){ a = dec(a, b); }
void Mul(int & a, int b) { a = mul(a, b); }
int ksm(int a, int b){
    int c = 1; 
    for(;b ;b >>= 1, Mul(a, a))
    if(b & 1) Mul(c, a);
    return c; 
}

int n, K; 
int c[N];

int pr[N];
int coef[N][N], co[N];
int dp[N][N];
int C[N][N];

int main() {
    #ifdef zqj
    freopen("1.in","r",stdin);
    #endif
    cin >> n >> K; 
    for(int i = 1; i <= n; i++){
        int x;
        cin >> x; 
        ++ c[x];
    }
    C[0][0] = 1; 
    for(int i = 1; i <= n; i++) C[i][0] = 1; 
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= i; j++) 
            C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);

    pr[1] = 1; 
    for(int i = 2; i <= n; i++) pr[i] = ksm(i, i - 2);
    coef[0][0] = 1; 
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= i; j++) {
            for(int k = 1; k <= min(i, K); k++) {
                Add(coef[i][j], mul(mul(C[i - 1][k - 1], mul(pr[k], k)), coef[i - k][j - 1]));
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= i; j++) {
            int cc = mul(coef[i][j], ksm(i, j - 2 + mod - 1));
            if(j & 1) Add(co[i], cc);
            else Dec(co[i], cc); 
        }
        Mul(co[i], i);
    }
    // for(int i = 1; i <= n; i++) cout << co[i] << ' '; cout << endl;
    dp[0][0] = 1; 
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= i; j++) {
            for(int k = 1; k <= i; k++)
                Add(dp[i][j], mul(dp[i - k][j - 1], mul(C[i - 1][k - 1], co[k]))); 
            // cerr << i << ' ' << j << ' ' << dp[i][j] << endl;
        }
    }
    // return 0; 
    vector <int> f(n + 1); f[0] = 1; 
    for(int i = 1; i <= n; i++) if(c[i]) {
        vector <int> g(n + 1);
        // cerr << i << ' ' << c[i] << endl;
        // for(int k = 1; k <= c[i]; k++) cerr << dp[c[i]][k] << ' '; cout << endl;
        for(int j = 0; j <= n; j++) if(f[j])
            for(int k = 1; k <= c[i]; k++)
                assert(j + k <= n), Add(g[j + k], mul(f[j], dp[c[i]][k]));
        f = g; 
        // for(int x : g) cout << x << ' '; cout << endl;
    }
    int ans = 0; 
    for(int i = 1; i <= n; i++) {
        int cc = ksm(n, mod - 1 + i - 2);
        Add(ans, mul(cc, f[i]));
    }
    cout << ans << '\n';
    return 0; 
}
/*

1 0 998244352 998244351 6 
1 1 1
2 1 0
2 2 1
3 1 998244344
3 2 0
3 3 1
4 1 998244225
4 2 998244317
4 3 0
4 4 1
5 1 3750
5 2 998243713
5 3 998244263
5 4 0
5 5 1
1 3
998244344 0 1 
0 998244344 0 1 0 0 
3 1
1 
0 0 998244344 0 1 0 
5 1
1 
0 0 0 998244344 0 1 
80

1 0 998244351 2 
1 1 1
2 1 0
2 2 1
3 1 998244335
3 2 0
3 3 1
4 1 128
4 2 998244281
4 3 0
4 4 1
1 3
998244335 0 1 
0 998244335 0 1 0 
2 1
1 
0 0 998244335 0 1 
998244351
*/

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3788kb

input:

5 3
1
1
3
1
5

output:

125

result:

ok "125"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3608kb

input:

4 2
2
1
1
1

output:

7

result:

ok "7"

Test #3:

score: 0
Accepted
time: 35ms
memory: 4652kb

input:

300 16
2
2
2
4
5
1
5
3
5
4
2
1
4
4
4
5
2
1
5
4
3
4
5
3
5
5
1
3
1
1
2
5
5
3
3
2
5
2
3
2
2
4
2
2
2
4
4
2
2
4
1
3
3
4
1
3
3
4
3
4
3
5
5
4
3
3
1
2
1
2
5
2
2
4
3
3
5
3
2
4
3
5
1
4
5
5
2
3
2
3
4
4
5
5
5
5
4
5
3
2
4
4
4
3
5
3
1
1
3
5
5
4
5
2
5
5
5
2
2
2
3
1
5
4
1
4
3
5
1
4
4
2
5
2
2
4
5
3
4
3
3
4
2
5
1
1
3...

output:

540253743

result:

ok "540253743"

Test #4:

score: 0
Accepted
time: 39ms
memory: 4820kb

input:

300 20
2
7
4
5
1
10
3
10
9
2
6
4
9
4
5
2
6
10
9
8
4
10
8
5
5
1
3
1
1
7
5
5
3
8
7
10
2
3
2
7
4
7
7
7
9
9
2
7
9
6
3
3
4
1
8
8
4
3
4
8
5
10
9
3
3
6
7
6
7
10
7
2
9
3
3
10
3
7
4
8
10
1
4
5
10
2
3
7
3
4
9
5
10
5
10
9
5
3
2
9
4
4
3
10
3
6
1
8
5
10
4
5
7
10
5
5
7
2
7
3
1
5
4
1
9
3
5
1
9
4
7
10
2
2
4
10
8
9
...

output:

616258392

result:

ok "616258392"

Test #5:

score: 0
Accepted
time: 32ms
memory: 4656kb

input:

300 1
124
25
131
70
63
150
139
62
46
94
9
34
25
102
66
120
139
28
134
120
98
135
95
21
43
71
11
87
45
15
33
58
37
70
12
63
132
47
104
97
67
17
9
119
72
87
29
96
53
103
34
71
58
78
34
3
94
78
115
60
139
43
63
46
127
146
37
60
37
12
59
23
43
120
53
107
54
68
70
21
94
125
10
22
143
117
133
64
129
55
14...

output:

450350134

result:

ok "450350134"

Test #6:

score: 0
Accepted
time: 32ms
memory: 4664kb

input:

300 2
131
70
63
150
139
62
46
94
9
34
25
102
66
120
139
28
134
120
98
135
95
21
43
71
11
87
45
15
33
58
37
70
12
63
132
47
104
97
67
17
9
119
72
87
29
96
53
103
34
71
58
78
34
3
94
78
115
60
139
43
63
46
127
146
37
60
37
12
59
23
43
120
53
107
54
68
70
21
94
125
10
22
143
117
133
64
129
55
140
75
10...

output:

942808207

result:

ok "942808207"

Test #7:

score: 0
Accepted
time: 58ms
memory: 4664kb

input:

300 150
1
2
2
2
1
2
1
2
2
2
1
2
2
2
2
1
1
1
1
1
1
1
1
1
1
2
1
2
2
1
2
1
2
1
1
1
1
1
2
1
1
2
1
1
2
1
2
2
2
1
2
2
1
2
1
1
1
2
1
2
1
2
1
2
1
1
1
2
1
1
2
2
2
1
2
1
2
2
1
1
1
2
1
1
2
1
2
1
1
1
2
1
2
2
1
2
1
2
1
2
1
2
2
1
1
2
1
1
1
2
1
1
1
1
2
1
1
1
1
1
1
2
1
2
2
2
2
2
2
1
1
1
2
2
1
1
1
1
1
2
1
1
2
1
1
1
...

output:

169425341

result:

ok "169425341"

Test #8:

score: 0
Accepted
time: 66ms
memory: 4664kb

input:

300 290
2
2
1
2
1
2
2
2
1
2
2
2
2
1
1
1
1
1
1
1
1
1
1
2
1
2
2
1
2
1
2
1
1
1
1
1
2
1
1
2
1
1
2
1
2
2
2
1
2
2
1
2
1
1
1
2
1
2
1
2
1
2
1
1
1
2
1
1
2
2
2
1
2
1
2
2
1
1
1
2
1
1
2
1
2
1
1
1
2
1
2
2
1
2
1
2
1
2
1
2
2
1
1
2
1
1
1
2
1
1
1
1
2
1
1
1
1
1
1
2
1
2
2
2
2
2
2
1
1
1
2
2
1
1
1
1
1
2
1
1
2
1
1
1
1
2
...

output:

394671363

result:

ok "394671363"

Test #9:

score: 0
Accepted
time: 85ms
memory: 4660kb

input:

300 290
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
...

output:

0

result:

ok "0"

Test #10:

score: 0
Accepted
time: 47ms
memory: 4824kb

input:

300 80
1
3
3
3
1
1
2
3
2
3
2
3
1
2
2
3
3
3
3
1
1
1
3
3
3
2
2
1
1
2
3
2
3
3
2
3
2
1
1
2
1
3
1
3
1
3
1
3
1
1
3
1
1
2
1
3
1
3
2
2
1
3
2
2
3
2
1
3
1
2
1
1
2
3
1
1
3
1
2
3
1
1
1
1
1
2
1
1
2
1
3
2
2
2
1
3
1
2
3
3
1
3
3
1
3
1
2
2
2
2
1
1
2
1
1
1
1
2
1
3
2
2
1
1
1
3
1
3
1
1
3
1
2
2
1
3
1
1
3
1
2
3
2
2
1
3
2...

output:

428950103

result:

ok "428950103"

Test #11:

score: 0
Accepted
time: 37ms
memory: 4652kb

input:

300 50
3
3
1
1
2
3
2
3
2
3
1
2
2
3
3
3
3
1
1
1
3
3
3
2
2
1
1
2
3
2
3
3
2
3
2
1
1
2
1
3
1
3
1
3
1
3
1
1
3
1
1
2
1
3
1
3
2
2
1
3
2
2
3
2
1
3
1
2
1
1
2
3
1
1
3
1
2
3
1
1
1
1
1
2
1
1
2
1
3
2
2
2
1
3
1
2
3
3
1
3
3
1
3
1
2
2
2
2
1
1
2
1
1
1
1
2
1
3
2
2
1
1
1
3
1
3
1
1
3
1
2
2
1
3
1
1
3
1
2
3
2
2
1
3
2
2
3...

output:

283683661

result:

ok "283683661"

Test #12:

score: 0
Accepted
time: 42ms
memory: 4656kb

input:

300 50
1
1
2
3
2
3
2
3
1
2
2
3
3
3
3
1
1
1
3
3
3
2
2
1
1
2
3
2
3
3
2
3
2
1
1
2
1
3
1
3
1
3
1
3
1
1
3
1
1
2
1
3
1
3
2
2
1
3
2
2
3
2
1
3
1
2
1
1
2
3
1
1
3
1
2
3
1
1
1
1
1
2
1
1
2
1
3
2
2
2
1
3
1
2
3
3
1
3
3
1
3
1
2
2
2
2
1
1
2
1
1
1
1
2
1
3
2
2
1
1
1
3
1
3
1
1
3
1
2
2
1
3
1
1
3
1
2
3
2
2
1
3
2
2
3
2
1...

output:

280919911

result:

ok "280919911"

Test #13:

score: 0
Accepted
time: 70ms
memory: 4600kb

input:

300 300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
...

output:

394671363

result:

ok "394671363"

Test #14:

score: 0
Accepted
time: 66ms
memory: 4668kb

input:

300 299
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
...

output:

0

result:

ok "0"