QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#526427#950. Star Trekbachbeo200750 31ms26436kbC++172.6kb2024-08-21 15:42:452024-08-21 15:42:45

Judging History

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

  • [2024-08-21 15:42:45]
  • 评测
  • 测评结果:50
  • 用时:31ms
  • 内存:26436kb
  • [2024-08-21 15:42:45]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5+5;
const int mod = 1e9+7;

struct node{
    int c=0;
    array<array<int,2>,2> a;
    node(){a[0]=a[1]={0,0};}
    void add(){
        if(c) (a[1][0]+=1)%=mod,(a[1][1]+=1)%=mod;
        else (a[1][0]+=1)%=mod,(a[0][1]+=1)%=mod;
    }
}dp[maxn],sum[maxn];

int n;
ll D;
vector<int> edge[maxn];

void merge(node &x,node y){
    array<array<int,2>,2> a;
    a[0]=a[1]={0,0};
    for(int i=0;i<=1;i++){
        int ni=i|y.c;
        (a[ni][0]+=x.a[i][0])%=mod;
        (a[ni][1]+=x.a[i][1])%=mod;

        ni=x.c|i;
        (a[ni][0]+=y.a[i][0])%=mod;
        (a[ni][1]+=y.a[i][1])%=mod;
    }      
    x.c|=y.c,x.a=a;
}

node rev(node cur){
    cur.c^=1;
    swap(cur.a[0],cur.a[1]);
    return cur;
}

void dfs(int u,int p){
    for(int v:edge[u]){
        if(v==p) continue;
        dfs(v,u);
        merge(sum[u],rev(dp[v]));
    }
    dp[u]=sum[u];dp[u].add();
}

node val[maxn];
void redfs(int u,int p){
    merge(sum[u],rev(val[u]));sum[u].add();

    node cur;merge(cur,rev(val[u]));
    for(int v:edge[u]){
        if(v==p) continue;
        val[v]=cur;merge(cur,rev(dp[v]));
    }
    cur=node();
    reverse(edge[u].begin(),edge[u].end());
    for(int v:edge[u]){
        if(v==p) continue;
        merge(val[v],cur);
        merge(cur,rev(dp[v]));
        val[v].add();
        redfs(v,u);
    }
}

array<int,2> mul0(array<int,2> val,array<array<int,2>,2> ss){
    array<int,2> res={0,0};
    for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) (res[j]+=1LL*val[i]*ss[i][j]%mod)%=mod;
    return res;
}
array<array<int,2>,2> mul1(array<array<int,2>,2> val,array<array<int,2>,2> ss){
    array<array<int,2>,2> res={0,0};
    for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) for(int k=0;k<=1;k++) (res[i][j]+=1LL*val[i][k]*ss[k][j]%mod)%=mod;
    return res;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin >> n >> D;
    for(int i=1;i<n;i++){
        int u,v;cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dfs(1,0);
    val[1].c=1;
    redfs(1,0);
    array<array<int,2>,2> ss;
    ss[0]=ss[1]={0,0};
    for(int u=1;u<=n;u++){
        for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) (ss[j][i]+=sum[u].a[i][j])%=mod;
    }
    array<int,2> val={0,0};
    for(int u=1;u<=n;u++) val[sum[u].c]++;
    D--;
    while(D){
        if(D&1) val=mul0(val,ss);
        ss=mul1(ss,ss);D>>=1;
    }
    cout << (1LL*sum[1].a[1][0]*val[0]+1LL*sum[1].a[1][1]*val[1])%mod << '\n';
}

詳細信息

Subtask #1:

score: 0
Accepted

Test #1:

score: 0
Accepted
time: 2ms
memory: 11912kb

input:

3 1
1 2
2 3

output:

4

result:

ok single line: '4'

Subtask #2:

score: 7
Accepted

Test #3:

score: 7
Accepted
time: 3ms
memory: 11832kb

input:

2 100
1 2

output:

499445072

result:

ok single line: '499445072'

Test #4:

score: 7
Accepted
time: 0ms
memory: 11756kb

input:

2 12425
1 2

output:

346132770

result:

ok single line: '346132770'

Test #5:

score: 7
Accepted
time: 2ms
memory: 11888kb

input:

2 123535522316321
1 2

output:

133457048

result:

ok single line: '133457048'

Test #6:

score: 7
Accepted
time: 2ms
memory: 11820kb

input:

2 6162342352351236
1 2

output:

431657487

result:

ok single line: '431657487'

Test #7:

score: 7
Accepted
time: 0ms
memory: 11912kb

input:

2 1000000000000000000
1 2

output:

80065005

result:

ok single line: '80065005'

Subtask #3:

score: 8
Accepted

Test #8:

score: 8
Accepted
time: 0ms
memory: 11768kb

input:

100 1
100 44
82 15
56 50
24 18
91 98
43 75
17 94
70 91
36 8
47 80
32 31
22 50
37 27
38 16
86 48
73 84
63 51
33 30
27 68
21 67
67 91
9 78
26 86
39 54
83 95
60 56
100 47
11 24
16 23
57 91
51 39
34 72
25 73
76 67
98 48
4 1
53 36
15 4
40 37
81 33
95 93
84 66
77 39
29 89
58 40
55 35
93 38
89 39
18 64
96 ...

output:

9956

result:

ok single line: '9956'

Test #9:

score: 8
Accepted
time: 2ms
memory: 11788kb

input:

100 1
38 46
78 7
28 95
47 28
1 14
4 90
9 8
83 56
14 3
94 13
80 17
81 75
6 79
16 15
56 37
39 25
60 84
40 48
87 45
29 60
27 5
58 41
36 38
88 92
10 49
34 42
75 94
89 18
70 16
68 67
37 58
30 80
92 20
44 32
17 52
97 59
86 97
5 89
85 40
11 100
21 22
91 33
98 29
57 99
3 11
65 51
100 98
96 19
7 6
74 55
23 3...

output:

10000

result:

ok single line: '10000'

Test #10:

score: 8
Accepted
time: 2ms
memory: 11764kb

input:

99 1
23 1
59 24
79 1
98 1
69 31
26 57
88 1
20 1
38 46
72 91
2 1
38 1
23 30
50 82
90 1
99 21
47 28
7 6
34 42
10 49
39 1
90 44
13 4
50 1
73 1
33 1
80 17
88 92
77 1
16 15
89 18
60 84
52 1
16 1
77 35
63 70
69 1
98 29
76 71
10 1
74 55
2 65
80 1
39 25
79 83
56 1
85 1
72 1
66 68
96 19
14 1
32 1
22 1
87 1
8...

output:

2500

result:

ok single line: '2500'

Test #11:

score: 8
Accepted
time: 2ms
memory: 11764kb

input:

100 1
4 52
85 13
74 45
62 53
95 71
56 100
23 73
78 54
95 33
24 64
85 100
99 13
67 82
14 79
35 67
37 28
78 17
97 39
57 35
23 87
98 29
90 87
50 76
93 16
17 52
29 90
26 40
24 71
2 33
77 27
3 89
55 96
34 94
41 72
24 86
30 69
9 53
9 91
48 93
4 7
95 44
19 66
74 58
30 7
97 10
49 81
31 76
76 79
2 96
2 83
65...

output:

9406

result:

ok single line: '9406'

Test #12:

score: 8
Accepted
time: 2ms
memory: 11836kb

input:

100 1
95 20
74 77
35 76
54 13
55 90
6 83
86 77
71 33
39 41
83 76
81 15
31 87
53 49
40 12
1 4
24 80
58 57
30 67
26 19
38 100
98 72
59 17
34 17
23 36
53 28
5 3
75 78
53 93
55 45
69 16
9 96
23 41
5 34
82 96
48 16
42 2
70 42
38 32
5 12
8 62
20 85
59 99
14 99
14 91
1 36
69 88
24 92
63 28
27 87
31 52
60 7...

output:

1908

result:

ok single line: '1908'

Subtask #4:

score: 15
Accepted

Dependency #3:

100%
Accepted

Test #13:

score: 15
Accepted
time: 0ms
memory: 12008kb

input:

1000 1
590 532
937 732
981 268
717 86
297 53
391 627
392 975
840 440
1 892
926 878
727 310
376 301
417 460
686 736
180 66
489 772
899 892
314 245
284 454
810 379
92 257
776 12
16 449
267 504
197 354
517 272
213 302
534 96
923 495
761 390
212 511
407 54
868 41
658 471
106 231
134 651
121 141
279 829
...

output:

894

result:

ok single line: '894'

Test #14:

score: 15
Accepted
time: 0ms
memory: 12120kb

input:

1000 1
58 610
202 970
260 447
465 668
990 955
709 346
587 234
126 660
819 802
68 872
762 207
551 449
209 415
687 299
219 401
433 951
253 112
799 808
449 348
235 39
57 128
191 819
112 663
149 817
944 741
913 105
133 197
834 801
532 987
622 755
460 745
655 576
603 277
238 393
647 467
195 57
461 116
88...

output:

1000000

result:

ok single line: '1000000'

Test #15:

score: 15
Accepted
time: 2ms
memory: 11868kb

input:

999 1
898 709
633 908
551 1
726 1
16 1
174 1
449 642
975 1
923 1
914 1
429 1
158 1
383 1
500 315
512 1
54 938
657 62
904 1
358 1
413 811
605 89
867 1
708 1
715 1
857 1
630 1
636 202
914 195
662 296
409 1
919 1
198 1
413 1
481 767
163 1
667 1
539 1
6 1
772 1
610 947
404 596
431 1
672 704
928 1
860 13...

output:

250000

result:

ok single line: '250000'

Test #16:

score: 15
Accepted
time: 0ms
memory: 11820kb

input:

1000 1
353 968
882 603
408 912
464 286
621 982
170 403
627 689
839 505
537 42
324 218
746 361
494 302
730 814
131 298
351 893
78 588
471 614
9 20
145 747
143 517
41 615
872 940
887 35
178 320
583 860
858 249
639 750
253 786
881 696
148 8
110 509
863 3
18 108
647 239
643 811
945 417
37 908
163 662
25...

output:

945660

result:

ok single line: '945660'

Test #17:

score: 15
Accepted
time: 2ms
memory: 11820kb

input:

1000 1
367 834
279 720
809 973
383 949
857 465
289 899
867 999
178 80
868 71
990 389
574 224
92 636
38 511
274 434
699 885
589 56
366 515
725 265
122 57
202 793
501 392
956 969
369 530
194 386
771 831
531 55
188 715
154 65
378 899
495 380
761 245
379 139
279 880
135 685
856 278
662 44
388 561
884 50...

output:

229218

result:

ok single line: '229218'

Subtask #5:

score: 0
Memory Limit Exceeded

Dependency #4:

100%
Accepted

Test #18:

score: 15
Accepted
time: 31ms
memory: 26436kb

input:

100000 1
21514 53675
37786 38522
4396 7815
78120 79656
46355 66053
87509 75662
61087 75391
48706 14696
55281 66363
94957 59080
55881 38209
56517 30881
58945 24126
55348 78766
45451 54549
54584 75049
12286 26851
71081 12894
61371 56312
26258 96611
20523 65809
72400 59703
21036 10373
86191 93888
49201...

output:

865183633

result:

ok single line: '865183633'

Test #19:

score: 0
Memory Limit Exceeded

input:

100000 1
94573 87510
30875 28537
57172 56489
45416 39729
14454 53978
56562 8437
4388 99595
14042 77604
93527 96359
69846 75190
61713 60538
26804 4270
61741 38005
6689 37583
65189 89680
53664 48743
95059 83939
75719 84787
69097 10278
24867 49894
71392 33508
53616 56387
25638 42234
3962 49009
78298 91...

output:

999999937

result:


Subtask #6:

score: 20
Accepted

Dependency #4:

100%
Accepted

Test #23:

score: 20
Accepted
time: 0ms
memory: 12004kb

input:

1000 82512
284 847
888 463
380 399
478 833
653 686
785 476
397 183
424 397
837 654
313 745
450 783
811 340
884 240
802 936
154 70
678 609
943 700
789 795
771 679
326 212
836 531
473 132
552 607
224 976
918 141
648 977
391 493
249 683
152 824
160 745
756 80
952 6
5 355
901 138
506 114
190 613
230 269...

output:

20021886

result:

ok single line: '20021886'

Test #24:

score: 20
Accepted
time: 2ms
memory: 12172kb

input:

1000 91421
284 648
217 135
44 541
47 992
648 138
568 252
862 726
406 196
788 768
15 591
17 900
666 881
83 210
209 731
494 710
262 355
777 764
214 425
546 506
205 281
18 956
966 143
430 787
259 29
66 516
400 780
390 993
723 12
342 539
130 125
733 363
878 450
582 594
851 673
757 583
339 595
886 407
40...

output:

391828247

result:

ok single line: '391828247'

Test #25:

score: 20
Accepted
time: 0ms
memory: 11800kb

input:

801 100000
130 1
601 436
235 1
705 35
500 1
69 445
290 764
182 1
27 1
125 628
590 344
633 1
775 162
437 1
668 1
500 424
685 145
406 1
82 1
59 359
509 1
409 1
177 1
96 1
176 1
398 1
255 1
457 657
149 506
251 488
157 1
13 253
557 1
91 183
421 662
185 1
609 1
486 1
164 1
705 1
217 111
224 51
156 1
158 ...

output:

190470151

result:

ok single line: '190470151'

Test #26:

score: 20
Accepted
time: 2ms
memory: 11956kb

input:

999 83259
201 220
110 39
462 561
500 309
954 666
140 728
280 850
761 235
646 146
201 902
827 659
714 55
410 671
161 854
159 511
115 363
102 216
464 64
793 254
282 153
431 324
875 532
494 242
713 32
698 388
842 744
818 509
498 495
108 495
950 268
891 114
475 800
745 569
698 748
12 539
812 303
115 376...

output:

894338086

result:

ok single line: '894338086'

Test #27:

score: 20
Accepted
time: 0ms
memory: 11876kb

input:

1000 56399
690 165
546 741
745 357
704 754
371 313
423 912
947 759
604 166
760 922
153 414
295 511
811 629
828 387
974 779
392 137
869 706
877 914
553 763
628 790
462 180
681 419
397 527
897 870
974 521
363 912
392 514
975 54
998 514
769 711
704 77
173 64
425 410
621 892
858 415
124 884
867 652
908 ...

output:

510610377

result:

ok single line: '510610377'

Subtask #7:

score: 0
Skipped

Dependency #5:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

0%