QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#717805#7509. 01treehcywoiAC ✓38ms27608kbC++236.0kb2024-11-06 18:59:332024-11-06 18:59:36

Judging History

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

  • [2024-11-06 18:59:36]
  • 评测
  • 测评结果:AC
  • 用时:38ms
  • 内存:27608kb
  • [2024-11-06 18:59:33]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

template<class T>
T qmi(T a, i64 b) {
  T res = 1;
  for (; b; b /= 2, a *= a) {
    if (b % 2) {
      res *= a;
    }
  }
  return res;
}

i64 mul(i64 a, i64 b, i64 p) {
  i64 res = a * b - i64(1.L * a * b / p) * p;
  res %= p;
  if (res < 0) {
    res += p;
  }
  return res;
}

template<int P>
struct modint {
  int x;
  constexpr modint() : x{} {}
  constexpr modint(i64 x) : x{norm(x % getmod())} {}

  static int mod;
  constexpr static int getmod() {
    if (P > 0) {
      return P;
    } else {
      return mod;
    }
  }
  constexpr static void setmod(int m) {
    mod = m;
  }
  constexpr int norm(int x) const {
    if (x < 0) {
      x += getmod();
    }
    if (x >= getmod()) {
      x -= getmod();
    }
    return x;
  }
  constexpr int val() const {
    return x;
  }
  explicit constexpr operator int() const {
    return x;
  }
  constexpr modint operator-() const {
    modint res;
    res.x = norm(getmod() - x);
    return res;
  }
  constexpr modint inv() const {
    assert(x != 0);
    return qmi(*this, getmod() - 2);
  }
  constexpr modint &operator*= (modint v) & {
    x = 1LL * x * v.x % getmod();
    return *this;
  }
  constexpr modint &operator+= (modint v) & {
    x = norm(x + v.x);
    return *this;
  }
  constexpr modint &operator-= (modint v) & {
    x = norm(x - v.x);
    return *this;
  }
  constexpr modint &operator/= (modint v) & {
    return *this *= v.inv();
  }
  friend constexpr modint operator- (modint a, modint b) {
    modint res = a;
    res -= b;
    return res;
  }
  friend constexpr modint operator+ (modint a, modint b) {
    modint res = a;
    res += b;
    return res;
  }
  friend constexpr modint operator* (modint a, modint b) {
    modint res = a;
    res *= b;
    return res;
  }
  friend constexpr modint operator/ (modint a, modint b) {
    modint res = a;
    res /= b;
    return res;
  }
  friend constexpr std::istream &operator>> (std::istream& is, modint& a) {
    i64 v;
    is >> v;
    a = modint(v);
    return is;
  }
  friend constexpr std::ostream &operator<< (std::ostream& os, const modint& a) {
    return os << a.val();
  }
  friend constexpr bool operator== (modint a, modint b) {
    return a.val() == b.val();
  }
  friend constexpr bool operator!= (modint a, modint b) {
    return a.val() != b.val();
  }
};

constexpr int P = 1E9 + 7;
using mint = modint<P>;

struct Comb {
  int n;
  std::vector<mint> fact;
  std::vector<mint> invefact;
  std::vector<mint> inve;

  Comb() : n{0}, fact{1}, invefact{1}, inve{0} {}
  Comb(int n) : Comb() {
    init(n);
  }
  
  void init(int m) {
    if (m <= n) return;
    fact.resize(m + 1);
    invefact.resize(m + 1);
    inve.resize(m + 1);
    
    for (int i = n + 1; i <= m; i++) {
      fact[i] = fact[i - 1] * i;
    }
    invefact[m] = fact[m].inv();
    for (int i = m; i > n; i--) {
      invefact[i - 1] = invefact[i] * i;
      inve[i] = invefact[i] * fact[i - 1];
    }
    n = m;
  }
  
  mint fac(int m) {
    if (m > n) init(2 * m);
    return fact[m];
  }
  mint invfac(int m) {
    if (m > n) init(2 * m);
    return invefact[m];
  }
  mint inv(int m) {
    if (m > n) init(2 * m);
    return inve[m];
  }
  mint binom(int n, int m) {
    if (n < m || m < 0) return 0;
    return fac(n) * invfac(m) * invfac(n - m);
  }
} comb;

struct Mo {
  int X, Y;
  int ix, iy;
  mint ans;

  Mo(int A, int B) {
    X = A, Y = B;
    ix = 0, iy = 0;
    ans = comb.binom(X, Y);
  }

  void move(int x, int y) {
    while (iy < y) {
      iy++;
      ans += comb.binom(ix, iy) * comb.binom(X - ix, Y - iy);
    }
    while (iy > y) {
      ans -= comb.binom(ix, iy) * comb.binom(X - ix, Y - iy);
      iy--;
    }
    while (ix < x) {
      ans -= comb.binom(ix, iy) * comb.binom(X - 1 - ix, Y - 1 - iy);
      ix++;
      if (ix == 0) {
        ans = iy >= 0 ? comb.binom(X, Y) : 0;
      }
    }
    while (ix > x) {
      ix--;
      if (ix < 0) {
        ans = 0;
      } else {
        ans += comb.binom(ix, iy) * comb.binom(X - 1 - ix, Y - 1 - iy);
      }
    }
  }
};

void solve() {
  int n;
  std::cin >> n;

  std::vector<std::vector<int>> adj(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    std::cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  std::string a, b;
  std::cin >> a >> b;

  std::vector<int> s1(n), t1(n), sq(n), tq(n);

  auto dfs = [&](auto self, int x, int p, int d) -> void {
    if (a[x] == '?') {
      sq[x]++;
    } else if (a[x] == d + '0') {
      s1[x]++;
    }
    if (b[x] == '?') {
      tq[x]++;
    } else if (b[x] == d + '0') {
      t1[x]++;
    }

    for (auto y : adj[x]) {
      if (y == p) {
        continue;
      }
      self(self, y, x, d ^ 1);
      sq[x] += sq[y];
      s1[x] += s1[y];
      tq[x] += tq[y];
      t1[x] += t1[y];
    }
  };
  dfs(dfs, 0, -1, 0);

  int X = sq[0] + tq[0];
  int Y = sq[0] + s1[0] - t1[0];

  std::vector<std::array<int, 2>> ask;
  for (int i = 0; i < n; i++) {
    int x = sq[i] + tq[i];
    int y = sq[i] + s1[i] - t1[i];
    ask.push_back({x, y});
  }

  constexpr int B = 710;
  std::sort(ask.begin(), ask.end(),
    [&](std::array<int, 2> i, std::array<int, 2> j) {
      if (i[0] / B != j[0] / B) {
        return i[0] < j[0];
      }
      return i[1] < j[1];
    });

  mint ans = 0;
  int ix = 0, iy = 0;
  Mo m1(X, Y), m2(X - 1, Y - 1);
  for (auto [x, y] : ask) {
    m1.move(x, y);
    m2.move(x - 1, y - 1);

    mint G = m1.ans, H = m2.ans;
    ans += 2 * (y * G - x * H) + x * comb.binom(X - 1, Y - 1) - y * comb.binom(X, Y);
  }

  std::cout << ans << "\n";
}

int main() {
  // freopen("opt.in", "r", stdin);
  // freopen("opt.out", "w", stdout);
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int t;
  std::cin >> t;

  while (t--) {
    solve();
  }

  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3476kb

input:

3
2
1 2
00
11
3
1 2
2 3
???
???
3
1 2
2 3
??1
0?0

output:

1
16
1

result:

ok 3 number(s): "1 16 1"

Test #2:

score: 0
Accepted
time: 4ms
memory: 3556kb

input:

1000
23
1 2
1 3
1 4
2 5
5 6
4 7
3 8
4 9
8 10
8 11
8 12
1 13
7 14
10 15
7 16
7 17
5 18
18 19
12 20
9 21
21 22
6 23
00?10?0000??1?00111?010
011??1?10?01?110?0??101
6
1 2
1 3
1 4
4 5
3 6
000?10
1???01
25
1 2
2 3
2 4
4 5
5 6
2 7
4 8
5 9
7 10
8 11
11 12
5 13
11 14
3 15
6 16
14 17
1 18
4 19
6 20
4 21
5 22...

output:

53545
24
11724
2228
162
14
711
28
550
1680
52
2
13
988
9480
2342
626416
0
71780
1
88
39207
19448
4
37395
9602
3253496
38057200
1066
3
303732
1608
281022
11718
170
78
15
1219376
29364
9092
7
0
2
7014
1942448
170371
75845
167217
16554
64
904
564290
14822
1127090
1758504
1227646
14833316
14954550
36055...

result:

ok 1000 numbers

Test #3:

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

input:

1
3000
1 2
2 3
2 4
1 5
3 6
2 7
5 8
8 9
9 10
10 11
2 12
11 13
7 14
11 15
7 16
13 17
8 18
1 19
11 20
10 21
18 22
7 23
7 24
15 25
23 26
24 27
14 28
15 29
25 30
16 31
6 32
10 33
3 34
30 35
16 36
9 37
36 38
28 39
26 40
33 41
1 42
11 43
20 44
23 45
14 46
31 47
41 48
11 49
48 50
45 51
6 52
10 53
32 54
38 5...

output:

924036898

result:

ok 1 number(s): "924036898"

Test #4:

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

input:

1
3000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26 53
27 54
2...

output:

429465738

result:

ok 1 number(s): "429465738"

Test #5:

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

input:

1
3000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 5...

output:

650625747

result:

ok 1 number(s): "650625747"

Test #6:

score: 0
Accepted
time: 38ms
memory: 12040kb

input:

1
100000
1 2
1 3
3 4
1 5
2 6
6 7
2 8
1 9
6 10
1 11
3 12
11 13
5 14
9 15
12 16
6 17
13 18
13 19
14 20
7 21
14 22
21 23
12 24
17 25
14 26
3 27
4 28
8 29
22 30
12 31
6 32
30 33
4 34
15 35
12 36
3 37
20 38
7 39
37 40
5 41
13 42
34 43
9 44
27 45
39 46
43 47
3 48
17 49
36 50
12 51
1 52
45 53
35 54
15 55
2...

output:

143309878

result:

ok 1 number(s): "143309878"

Test #7:

score: 0
Accepted
time: 24ms
memory: 13520kb

input:

1
100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26 53
27 54...

output:

724432513

result:

ok 1 number(s): "724432513"

Test #8:

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

input:

1
100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52...

output:

164448583

result:

ok 1 number(s): "164448583"

Test #9:

score: 0
Accepted
time: 26ms
memory: 13656kb

input:

1
100000
1 2
1 3
1 4
4 5
2 6
5 7
3 8
5 9
9 10
6 11
4 12
6 13
11 14
1 15
6 16
15 17
6 18
6 19
8 20
11 21
1 22
11 23
20 24
9 25
2 26
5 27
14 28
7 29
28 30
10 31
7 32
17 33
2 34
24 35
14 36
10 37
2 38
5 39
39 40
14 41
35 42
25 43
33 44
11 45
41 46
1 47
33 48
36 49
25 50
20 51
13 52
4 53
46 54
45 55
21 ...

output:

159386883

result:

ok 1 number(s): "159386883"

Test #10:

score: 0
Accepted
time: 19ms
memory: 13648kb

input:

1
100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26 53
27 54...

output:

752214506

result:

ok 1 number(s): "752214506"

Test #11:

score: 0
Accepted
time: 23ms
memory: 27608kb

input:

1
100000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52...

output:

419022702

result:

ok 1 number(s): "419022702"