QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#291155#2023. Routing SchemesMoRanSky100 ✓6ms4236kbC++235.1kb2023-12-26 05:07:222023-12-26 05:07:22

Judging History

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

  • [2023-12-26 05:07:22]
  • 评测
  • 测评结果:100
  • 用时:6ms
  • 内存:4236kb
  • [2023-12-26 05:07:22]
  • 提交

answer

// Skyqwq
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long LL;

const int N = 205, P = 1e9 + 7;

int n, K, out[N], in[N], fact[N], infact[N], f[N], sum;

char s[N], g[N][N];

void inline clear() {
    for (int i = 1; i <= n; i++) out[i] = 0, in[i] = 0, f[i] = 0;
}

int inline power(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = (LL)res * a % P;
        a = (LL)a * a % P;
        b >>= 1;
    }
    return res;
}

void inline factPrework(int n) {
    fact[0] = infact[0] = 1;
    for (int i = 1; i <= n; i++) fact[i] = (LL)fact[i - 1] * i % P;
    infact[n] = power(fact[n], P - 2);
    for (int i = n - 1; i; i--) infact[i] = infact[i + 1] * (i + 1ll) % P;
}

int inline C(int a, int b) {
    if (a < b) return 0;
    return (LL)fact[a] * infact[b] % P * infact[a - b] % P;
}

int inline ask() {
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        int A = out[i], B = in[i], t = min(A, B);
        ans = (LL)ans * fact[t] % P;
    }
    return ans;
}

namespace S2{
    void inline work() {
        clear();
        for (int i = 1; i <= n; i++) {
            if (s[i] == 'S') in[i]++;
            else if (s[i] == 'R') out[i]++;
        }
        int S, T;
        for (int i = 1; i <= n; i++) 
            for (int j = 1; j <= n; j++)
                if (g[i][j] == '1') {
                    if (i < j) 
                        in[j]++, out[i]++;
                    else S = j, T = i;
                }
        //in[S]++, out[T]++;
        int ans = 1;
        for (int i = 1; i < S; i++) ans = (LL)ans * fact[in[i]] % P;
        for (int i = T; i <= n; i++) ans = (LL)ans * fact[out[i]] % P;
        f[S] = 1;
        for (int i = S; i < T; i++) {
            for (int j = i + 1; j <= T; j++) {
                if (g[i][j] == '1') {
                    int v = (LL)f[i] * fact[out[i] - 1] % P;
                    for (int k = i + 1; k < j; k++)
                        v = (LL)v * fact[out[k]] % P;
                    (f[j] += v) %= P;
                }
            }
        }
        ans = (LL)ans * f[T] % P;
        printf("%d\n", (sum - ans + P) % P);
    }
}

void inline add(int &x, int y) {
    (x += y) %= P;
}

namespace S3{
    int F[N][N], G[N][N];
    void inline work() {
        clear();
        for (int i = 1; i <= n; i++) {
            if (s[i] == 'S') in[i]++;
            else if (s[i] == 'R') out[i]++;
        }
        int S1 = -1, T1, S2, T2;
        for (int i = 1; i <= n; i++) 
            for (int j = 1; j <= n; j++)
                if (g[i][j] == '1') {
                    if (i < j) 
                        in[j]++, out[i]++;
                    else if (S1 == -1) S1 = j, T1 = i;
                    else S2 = j, T2 = i;
                }
        memset(F, 0, sizeof F);
        memset(G, 0, sizeof G);
        //cout << S1 << " ddd" << T1 << " )) " << " " << S2 << " " << T2 << endl;
        F[S1][S2] = 1;
        for (int i = 1; i <= n; i++) {
            memcpy(G, F, sizeof G);
            memset(F, 0, sizeof F);
            for (int u = 1; u <= n; u++) {
                for (int v = 1; v <= n; v++) {
                    if (!G[u][v]) continue;
                    //cout << u << " " << v << " -> " << G[u][v] << endl;
                    add(F[u][v], (LL)G[u][v] * fact[in[i]] % P);
                    if (u < i && g[u][i] == '1') {
                        add(F[i][v], (LL)G[u][v] * fact[in[i] - 1] % P);
                    }
                    if (v < i && g[v][i] == '1') {
                        add(F[u][i], (LL)G[u][v] * fact[in[i] - 1] % P);
                    }
                    if (u != v && u < i && v < i && g[u][i] == '1' && g[v][i] == '1') {
                        add(F[i][i], (LL)G[u][v] * fact[in[i] - 2] % P);
                    }
                }
            }
            //cout << i << " ____________\n";
        }
        int ans = 0;
        for (int u = 1; u <= n; u++) {
            for (int v = 1; v <= n; v++) {
                if (!F[u][v]) continue;
                    //cout << u << " " << v << " -> " << F[u][v] << endl;
                if (u != v && s[u] == 'R' && s[v] == 'R') add(ans, F[u][v]);
                if (u == T2 && s[v] == 'R') add(ans, F[u][v]);
                if (v == T1 && s[u] == 'R') add(ans, F[u][v]);
            }
        }
        printf("%d\n", ans);
    }
}

int main() {
    factPrework(200);
    int T; scanf("%d", &T);
    while (T--) {
        scanf("%d%d%s", &n, &K, s + 1);
        for (int i = 1; i <= n; i++) {
            scanf("%s", g[i] + 1);
            if (s[i] == 'S') in[i]++;
            else if (s[i] == 'R') out[i]++;
        }
        for (int i = 1; i <= n; i++) 
            for (int j = 1; j <= n; j++)
                if (g[i][j] == '1') {
                        in[j]++, out[i]++;
                }
        sum = ask();
        if (K == 0) {
            printf("%d\n", sum);
        } else if (K == 1) {
            S2::work();
        } else {
            S3::work();
        }
        clear();
    }
    return 0;
}

詳細信息

Test #1:

score: 4.16667
Accepted
time: 1ms
memory: 3588kb

input:

2

8 0
SS....RR
00100000
00100000
00011000
00000100
00000100
00000011
00000000
00000000

13 0
SSS.RRRSS.RR.
0001000000000
0001000000000
0001000000000
0000111000000
0000000000000
0000000000000
0000000000000
0000000001000
0000000001000
0000000000110
0000000000000
0000000000000
0000000000000

output:

4
12

result:

ok 2 lines

Test #2:

score: 4.16667
Accepted
time: 1ms
memory: 3984kb

input:

2

5 1
SS.RR
00101
00100
10010
00000
00000

6 2
S....R
001000
000100
010001
000010
001000
000000

output:

3
1

result:

ok 2 lines

Test #3:

score: 4.16667
Accepted
time: 1ms
memory: 4160kb

input:

5

3 2
RS.
010
101
100

4 2
.R.S
0100
0010
1000
0100

4 2
.SR.
0000
0011
0100
0010

5 2
.SSRR
01000
10101
01010
00000
00000

6 2
SS..RR
001010
000010
000010
000010
100101
000000

output:

2
1
2
6
24

result:

ok 5 lines

Test #4:

score: 4.16667
Accepted
time: 1ms
memory: 3980kb

input:

20

6 2
SS..RR
000001
001010
000110
000010
010001
001000

6 1
.SS.RR
000000
000010
000011
000000
000001
001000

6 0
.SR.SR
000000
001000
000000
000000
000001
000000

6 2
SS..RR
011100
000110
000010
100001
100000
000000

6 1
SS.RR.
000110
001000
100000
000000
000000
000000

6 0
.SS.RR
000000
001000
0...

output:

24
5
1
24
2
4
12
6
2
14
8
2
8
10
1
6
5
2
46
12

result:

ok 20 lines

Test #5:

score: 4.16667
Accepted
time: 0ms
memory: 4032kb

input:

20

6 2
.SSR.R
000100
001000
000111
001000
000001
100000

6 1
.S.SRR
010000
000110
000000
100010
000001
000000

6 0
S.RSR.
010000
001000
000000
000010
000000
000000

6 2
..SSRR
000000
000010
000100
010011
000001
000100

6 1
S.SR.R
010000
000110
000100
010000
000001
000000

6 0
SRS.R.
010000
000000
0...

output:

16
6
1
16
3
1
52
3
1
48
8
1
28
12
2
50
3
2
26
66

result:

ok 20 lines

Test #6:

score: 4.16667
Accepted
time: 1ms
memory: 3672kb

input:

20

100 0
SSSS..S.S.SR.SSSSSS.SSR..SRS.SS.RSSSSRSSSSSRRRRSRRRRS...R.SRSRRS..S..RRSR...R.RRSRRRRRR.RRR....RRRR.
0000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000
0000100000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

598434032
1327104
576
884736
96
237273118
1536
32
4
27648
8
4608
1536
605079681
41472
6
556485058
3972517
439853547
1

result:

ok 20 lines

Test #7:

score: 4.16667
Accepted
time: 1ms
memory: 3660kb

input:

19

100 0
S.SSSSSSSSSSSSRS.SRSSSSSS.SRSRSSSSSSSR..RSRSSRSSSSRS.SRRRSRSRRSR.RRRRRRRRRRRRS.RRRRSRSRRRRRRSRRRRRRR
0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0010000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

62130475
55296
48
64
4
27648
55296
573308928
477885230
8
95551488
2
2654208
644491483
4
933474442
7962624
112970109
254803968

result:

ok 19 lines

Test #8:

score: 4.16667
Accepted
time: 1ms
memory: 3892kb

input:

19

100 1
SSSSSRSSSSS.RSSSRSSSSSSSSSSSSSRS.RSSSSSSSSSS.SSRSSRSSSRRRRRRSRRRRRRSSRR.RRRRRRRRRRRRRRRRRR..RRRRRRRR
0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0010100000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

689691380
448688154
160
3
1952
30710784
10423296
11776
648576
3
83607552
434872188
126074880
411720392
40
188006400
405930966
85733880
96576

result:

ok 19 lines

Test #9:

score: 4.16667
Accepted
time: 1ms
memory: 3680kb

input:

19

100 1
SSSSSSSSSR.SSSSSSSSRSSSSSRSSSRSSSSSSSRSSSSSSRRSSRSSSRRSSSRRRRRRR.RRRRRRRR.S.RRRRSRRRRRRRRRRRRRRRRRRR
0000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000100000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

90137953
54934394
901518419
178034057
565248
893842573
3
34176
847977096
33696
834388724
204037824
265403599
40
207872
2356128
3984
38
209610728

result:

ok 19 lines

Test #10:

score: 4.16667
Accepted
time: 1ms
memory: 3752kb

input:

20

100 1
SSSS.SSS.SRSSSRSSSSSSS.SSSRRSSSSSSSSSRS.SRSS.S.SSSS.R.SRRSSRSRRR.RRRRRRRRRRRRRRRRRRRRSRRRRR.RRRR..RR
0000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000100000000000000000000000000...

output:

503432759
594772364
544942080
36000
302334554
1728
130970091
330786317
570862472
1152
1536
980377003
816
36
741497200
3483648
238016597
2808
1280
534354874

result:

ok 20 lines

Test #11:

score: 4.16667
Accepted
time: 1ms
memory: 3736kb

input:

20

100 1
S.SSS.SSSSRSSSSSS.RS..SSS.SSSRR.SSSRSSSSS.SSS.SR.SRRS.R.RRRRRR..RRRRRSS.RRRRRSRRR...RSRRSSRRRRRRRRRR
0000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

118409422
384
848468894
271953138
80
68832
120
638
44
946471695
46752
489537139
3
276480
68
650455698
2
9953280
16
6

result:

ok 20 lines

Test #12:

score: 4.16667
Accepted
time: 1ms
memory: 3676kb

input:

2

100 1
SSSRSS.SSSSSSSSSSSSSSRSSRSSSSSSSRSSSRSSR.SRRS.SSRSS.RSRRR.SS.RSRRRRRRRSR.RSRRSRRRRRRRRRR.RRRRRRRRRRR
0010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000001000000000000000000000000000000...

output:

840967271
265512588

result:

ok 2 lines

Test #13:

score: 4.16667
Accepted
time: 5ms
memory: 4188kb

input:

19

100 2
SSSSSSSSRSSSSSSSSSSSSSSSSRSRSSSSSRSRRSRRSSSSRSSRRSSSRRSSSRRRSRRSRRRSRRSRRSRRRRRRRRRRSRRRRRRRRRRRRRRR
0000000100000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000
0001000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

288382526
586884853
620640127
126
840
50688
577482031
217976832
802287906
218
6
86158509
111559680
3072
18
993526011
3696
808254173
25505280

result:

ok 19 lines

Test #14:

score: 4.16667
Accepted
time: 5ms
memory: 3996kb

input:

18

100 2
S.SSSSSS.SRSSSSSSSSSS.SSS.SS.SSSSS.RSSRRR.SRR.SSR.RR.RSSRRSRRSSRRR.RS.RS.RRRRRRRR.RRRR.RR...RRR..RRR
0000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

697195692
618898048
172800
27157804
55406592
128480256
1631616
1121472
873122334
141419520
920
10
271362863
22176
808300753
55172453
1871424
14

result:

ok 18 lines

Test #15:

score: 4.16667
Accepted
time: 6ms
memory: 4236kb

input:

20

100 2
SSSS.S.SS.SS.SSS.SRSS.R.SSSSSS..SSSS.RRSSSRS..SS...RS.RS.RSRRRRRR.SRRRRR.SRRRRRRR..RR...RSRRRRRRRR..
0100000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000
0011000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

482364841
46
30301483
886937551
533292954
477411877
14616576
161792
438755707
282222897
1329024
1162656
655637728
152098340
564
186654173
2736
85561990
273888
6768

result:

ok 20 lines

Test #16:

score: 4.16667
Accepted
time: 5ms
memory: 4184kb

input:

19

100 2
SSSSSSSSSSSRSSRSSSSSS.SSSSSSSSSSRSSRR.SSSSSRSSSRRSRRS.SS.RRRRRRRRS.RRSSRRRRRRR.RRSRRRRRRRRRRRRRRRRRR
0000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000
0010000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

799345293
1963584
47921184
31728
503502938
11
43414557
809484678
1180
23520
746898789
4
60692372
1170
480
620165360
825252916
17808249
604800

result:

ok 19 lines

Test #17:

score: 4.16667
Accepted
time: 5ms
memory: 4188kb

input:

20

100 2
.S...SS.SSSSS.S..S..SSSS.S.SS.S.SSRRSRS.RRSR.SR.R.S....RR.R....RR.RR.RRSR.R.S..RRS..RRR.R....R...R.R
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000010000000000000000000000000000000000000000000000000000000...

output:

469006517
29270016
392037224
20
206208
591411456
4106592
9288
342033913
25524
375795687
42
25837056
619991390
139649673
264130863
328
14
4473792
975901937

result:

ok 20 lines

Test #18:

score: 4.16667
Accepted
time: 5ms
memory: 4224kb

input:

19

100 2
.S.S.SS......SS.S..SS.S.S.SS..S...R.SSR.S.SSS.SSS....R.RRRR.....RR.S.R.RRRRRSR.R.RS....R.RR..RRR.R.R
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000100000000000000000000000000000000000000...

output:

682103332
275071534
109983559
76
542216858
578
271872
233280000
251873280
5568
1376
320175888
118034867
170201088
6
4680
17664
823374152
12

result:

ok 19 lines

Test #19:

score: 4.16667
Accepted
time: 2ms
memory: 4232kb

input:

19

100 2
.SSSSS.S..SSSSSSSSSSS.SSSSSS.SSSRSRRSSSR.SSSSSRRRR.SRR.RS.SR...R.RRRR.RRRRR..RRSRRRSSRRRR.RRRRRRRRRR
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000010000000000000000000000000000000000000000000000000000000...

output:

167077449
103022667
991359642
318642285
385920
94
791047322
3408
809988073
134016
384
129408
20160
752574023
775966050
5200
624375289
85
547938585

result:

ok 19 lines

Test #20:

score: 4.16667
Accepted
time: 5ms
memory: 3992kb

input:

19

100 2
SS..SSSS.S.SRSS.SSSR....SS.SS..SS..SRSSRSSRSS.R.RS...S...RSRS.RR.RRRR.RRRSR.RSR.R..SR.R.RRRR.RR..RRR
0000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000010000000000000000000000000000000000000000000000...

output:

624070116
108
697115986
31078372
30912
9114624
643870277
4640
29520
1008
336780288
785664
896
63698746
130914290
942108125
110351390
402689352
906235839

result:

ok 19 lines

Test #21:

score: 4.16667
Accepted
time: 5ms
memory: 4184kb

input:

19

100 2
S...S..S....S.SSS...SSS.S.RSS..R..SR..SSS...R...S...SSS..R.S.SS.S.R.RRR..R..RRRR.S.RRRRR.R..RRRR..RR
0000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

184450660
732005041
480605689
4096
568512
974032746
117460824
278893961
8
1359360
6965760
227367936
529817832
401344
510698770
20483328
17
1291680
861821825

result:

ok 19 lines

Test #22:

score: 4.16667
Accepted
time: 3ms
memory: 4228kb

input:

2

100 2
SRSSSS.SSSSSSSSSRSSSSSSSSRSSSSSRRSSRSSRSRRRS..RSSSSSRSRSRSSRRSSR.RRRSRRRSRRRR.RRRRRRRSRRRRRRRRR.RRRR
0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

968420002
389389747

result:

ok 2 lines

Test #23:

score: 4.16667
Accepted
time: 2ms
memory: 3988kb

input:

2

100 2
SSSSSSSSSSSRSSSSSRSSSSSSSS.SSSSS.SSSSSSSRSSRSRSSSRRRRRRRRR..RRRSRRRR.RRRRRRRSSRRRRR.RSR.RRRRRRR.RRRR
0011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000100000000000000000000000000000000000000000000000...

output:

303488821
183480277

result:

ok 2 lines

Test #24:

score: 4.16667
Accepted
time: 4ms
memory: 4224kb

input:

2

100 2
S.SSSS.SSSSRSSSSSSSSRSSSSSSS.S.RSRSSSRRSSRSS.SSR.RRRRSRRSS.SRRSSRS.RRRRRS.RRRRRR.RR.RRR.R.RRRRRRRRR.
0010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

709758665
79666468

result:

ok 2 lines