QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#641906#950. Star TrekElegia❄️50 1ms4128kbC++145.4kb2024-10-15 03:14:292024-10-15 03:14:29

Judging History

This is the latest submission verdict.

  • [2024-10-15 03:14:29]
  • Judged
  • Verdict: 50
  • Time: 1ms
  • Memory: 4128kb
  • [2024-10-15 03:14:29]
  • Submitted

answer

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

const int P = 1000000007;

int norm(int x) { return x >= P ? (x - P) : x; }

void add(int &x, int y) { if ((x += y) >= P) x -= P; }

void sub(int &x, int y) { if ((x -= y) < 0) x += P; }

void exGcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exGcd(b, a % b, y, x);
  y -= a / b * x;
}

int inv(int a) {
  int x, y;
  exGcd(a, P, x, y);
  return norm(x + P);
}

struct Info {
  int x[2][2];

  Info() { memset(x, 0, sizeof(x)); }

  Info(const Info &info) { memcpy(x, info.x, sizeof(x)); }

  Info operator+(const Info &rhs) const {
    Info ret = Info();
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j)
        ret.x[i][j] = norm(x[i][j] + rhs.x[i][j]);
    return ret;
  }

  Info operator*(const Info &rhs) const {
    Info ret = Info();
    for (int i = 0; i < 2; ++i)
      for (int j = 0; i + j < 2; ++j)
        for (int k = 0; k < 2; ++k)
          for (int t = 0; t < 2; ++t)
            add(ret.x[i + j][k | t], x[i][k] * (ll) rhs.x[j][t] % P);
    return ret;
  }

  void swp() {
    for (int i = 0; i < 2; ++i) swap(x[i][0], x[i][1]);
  }
};

Info id() {
  Info ret = Info();
  ret.x[0][0] = 1;
  return ret;
}

struct Mat {
  int x[2][2];

  Mat() { memset(x, 0, sizeof(x)); }

  Mat(const Mat &mat) { memcpy(x, mat.x, sizeof(x)); }

  Mat operator*(const Mat& rhs) const {
    Mat ret = Mat();
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j)
        for (int k = 0; k < 2; ++k)
          ret.x[i][k] = (ret.x[i][k] + x[i][j] * (ll)rhs.x[j][k]) % P;
    return ret;
  }
};

Mat matExp(const Mat& mat, ll d) {
  if (d == 1) return mat;
  Mat half = matExp(mat * mat, d >> 1);
  if (d & 1) half = half * mat;
  return half;
}

int main() {
#ifdef ELEGIA
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  ll d;
  cin >> n >> d;

  vector<vector<int>> g(n);
  for (int rep = 1; rep < n; ++rep) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  vector<vector<int>> tree(n);

  vector<int> prt(n, -1);
  function<void(int)> predfs = [&](int u) {
    for (int v : g[u])
      if (v != prt[u]) {
        prt[v] = u;
        tree[u].push_back(v);
        predfs(v);
      }
  };
  predfs(0);

  auto getDP = [&](int x, int y) {
    vector<Info> dt(n), td(n), ret(n);
    Info ad = Info();
    ad.x[0][0] = 1;
    ad.x[1][0] = x;
    ad.x[1][1] = y;

    function<void(int)> downtop = [&](int u) {
      dt[u] = ad;
      for (int v : tree[u])
        dt[u] = dt[u] * (downtop(v), dt[v]);
      dt[u].swp();
    };
    downtop(0);

    td[0] = id();
    function<void(int)> topdown = [&](int u) {
      if (tree[u].empty())
        return;
      {
        vector<Info> pre(tree[u].size()), suf(tree[u].size());
        Info cur = id();
        for (int i = 0; i < tree[u].size(); ++i) {
          pre[i] = cur;
          cur = cur * dt[tree[u][i]];
        }
        cur = id();
        for (int i = (int) tree[u].size() - 1; i >= 0; --i) {
          suf[i] = cur;
          cur = cur * dt[tree[u][i]];
        }
        for (int i = 0; i < tree[u].size(); ++i) {
          td[tree[u][i]] = td[u] * pre[i] * suf[i] * ad;
          td[tree[u][i]].swp();
          td[tree[u][i]] = td[tree[u][i]];
        }
      }
      for (int v : tree[u])
        topdown(v);
    };
    topdown(0);

    for (int u = 0; u < n; ++u) {
      ret[u] = ad;
      for (int v : g[u])
        if (v != prt[u])
          ret[u] = ret[u] * dt[v];
        else
          ret[u] = ret[u] * td[u];
      ret[u].swp();
    }

    return ret;
  };

  auto trans0 = getDP(1, 0);
  auto trans1 = getDP(0, 1);

  Mat transMat = Mat();
  int row0 = 0, row1 = 0;
  for (int i = 0; i < n; ++i) {
    add(row0, trans0[i].x[0][0]);
    add(row1, trans0[i].x[0][1]);
    add(transMat.x[0][0], trans0[i].x[1][0]);
    add(transMat.x[0][1], trans0[i].x[1][1]);
    add(transMat.x[1][0], trans1[i].x[1][0]);
    add(transMat.x[1][1], trans1[i].x[1][1]);
  }
  int fin0 = row0, fin1 = row1;
  if (d > 1) {
    Mat ex = matExp(transMat, d - 1);
    fin0 = (row0 * (ll) ex.x[0][0] + row1 * (ll) ex.x[1][0]) % P;
    fin1 = (row0 * (ll) ex.x[0][1] + row1 * (ll) ex.x[1][1]) % P;
  }

  int ans = getDP(fin0, fin1)[0].x[1][0];
  cout << ans << '\n';

#ifdef ELEGIA
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Accepted

Test #1:

score: 0
Accepted
time: 0ms
memory: 3772kb

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: 0ms
memory: 3620kb

input:

2 100
1 2

output:

499445072

result:

ok single line: '499445072'

Test #4:

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

input:

2 12425
1 2

output:

346132770

result:

ok single line: '346132770'

Test #5:

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

input:

2 123535522316321
1 2

output:

133457048

result:

ok single line: '133457048'

Test #6:

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

input:

2 6162342352351236
1 2

output:

431657487

result:

ok single line: '431657487'

Test #7:

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

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: 3640kb

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: 0ms
memory: 3652kb

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: 0ms
memory: 3568kb

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: 0ms
memory: 3564kb

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: 0ms
memory: 3860kb

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: 1ms
memory: 3920kb

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: 1ms
memory: 4100kb

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: 1ms
memory: 3788kb

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: 1ms
memory: 4028kb

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: 1ms
memory: 4040kb

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: 0
Memory Limit Exceeded

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:


Subtask #6:

score: 20
Accepted

Dependency #4:

100%
Accepted

Test #23:

score: 20
Accepted
time: 1ms
memory: 4128kb

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: 1ms
memory: 4100kb

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: 1ms
memory: 3708kb

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: 0ms
memory: 3740kb

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: 1ms
memory: 3984kb

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%