#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
const ll MT = 2;
const ll N = 1e5 + 50;
const ll Mod = 1e9 + 7;
ll n, d;
struct shet{
int col = 0, s0 = 0, s1 = 0, t0 = 0, cnt = 1;
};
ll calcute(shet a){
if(a.t0 >= 2) return 0;
if(a.t0 == 1) return a.s0;
return a.s1 + 1;
}
shet add(shet a, shet b){
if(b.col){
a.s1 += b.cnt; a.cnt = calcute(a);
}
else{
a.t0++; a.s0 += b.cnt; a.col |= 1; a.cnt = calcute(a);
}
return a;
}
shet del(shet a, shet b){
if(b.col){
a.s1 -= b.cnt; a.cnt = calcute(a);
}
else{
a.t0--; a.s0 -= b.cnt; a.cnt = calcute(a);
if(!a.t0) a.col = 0;
}
return a;
}
shet val[N], rval[N];
vector<int> adj[N];
ll mod(ll a, ll b = Mod){
return (a%b+b)%b;
}
struct matrix{
ll n, m, a[MT][MT];
matrix(int n_, int m_, int x = 0){
n = n_; m = m_;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
a[i][j] = x;
}
matrix operator * (const matrix &t){
matrix r = matrix(n, t.m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
for(int k = 0; k < t.m; k++)
r.a[i][k] = mod(r.a[i][k] + mod(a[i][j] * t.a[j][k]));
return r;
}
};
matrix Pow(matrix a, ll b){
matrix ans = matrix(a.n, a.m);
for(int i = 0; i < MT; i++) ans.a[i][i] = 1;
for(; b; b /= 2, a = a * a){
if(b % 2) ans = ans * a;
}
return ans;
}
ll power(ll a, ll b, ll md = Mod){
ll ans = 1;
for(; b; b /= 2, a = a * a % md){
if(b % 2) ans = ans * a % md;
}
return ans;
}
void dfs(int v, int p = 0){
for(int u: adj[v]){
if(u == p) continue;
dfs(u, v);
val[v] = add(val[v], val[u]);
}
}
void cal(int v, int p = 0){
rval[v] = val[v];
for(int u: adj[v]){
if(u == p) continue;
val[v] = del(val[v], val[u]);
val[u] = add(val[u], val[v]);
cal(u, v);
val[u] = del(val[u], val[v]);
val[v] = add(val[v], val[u]);
}
}
int main(){
fast_io;
cin >> n >> d; d++;
int u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1); cal(1);
//for(int i = 1; i <= n; i++) cout << rval[i].col << sep << rval[i].cnt << sep << rval[i].t0 << endl;
ll sum0 = 0, sum1 = 0, cnt0 = 0;
for(int i = 1; i <= n; i++){
if(rval[i].col) sum1 = mod(sum1 + rval[i].cnt);
else sum0 = mod(sum0 + rval[i].cnt);
if(!rval[i].col) cnt0++;
}
matrix m = matrix(2, 2);
m.a[0][0] = mod(sum1 - sum0); m.a[0][1] = cnt0;
m.a[1][0] = 0; m.a[1][1] = n*n%Mod;
matrix ans = matrix(2, 1);
ans.a[0][0] = 0; ans.a[1][0] = 1;
ans = Pow(m, d-1) * ans;
ll res = mod(rval[1].cnt * ans.a[0][0]);
if(!rval[1].col) kill(res);
res = mod(power(n*n%Mod, d-1) - res);
kill(res);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2800 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2808 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2808 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2816 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
2 ms |
2732 KB |
Output is correct |
11 |
Correct |
2 ms |
2828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2808 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2816 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
2 ms |
2732 KB |
Output is correct |
11 |
Correct |
2 ms |
2828 KB |
Output is correct |
12 |
Correct |
62 ms |
14380 KB |
Output is correct |
13 |
Correct |
55 ms |
18576 KB |
Output is correct |
14 |
Correct |
48 ms |
11012 KB |
Output is correct |
15 |
Correct |
63 ms |
10956 KB |
Output is correct |
16 |
Correct |
51 ms |
11044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2808 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2816 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
2 ms |
2732 KB |
Output is correct |
11 |
Correct |
2 ms |
2828 KB |
Output is correct |
12 |
Correct |
2 ms |
2660 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2812 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2812 KB |
Output is correct |
20 |
Correct |
1 ms |
2624 KB |
Output is correct |
21 |
Correct |
2 ms |
2904 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2808 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2816 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
2 ms |
2732 KB |
Output is correct |
11 |
Correct |
2 ms |
2828 KB |
Output is correct |
12 |
Correct |
62 ms |
14380 KB |
Output is correct |
13 |
Correct |
55 ms |
18576 KB |
Output is correct |
14 |
Correct |
48 ms |
11012 KB |
Output is correct |
15 |
Correct |
63 ms |
10956 KB |
Output is correct |
16 |
Correct |
51 ms |
11044 KB |
Output is correct |
17 |
Correct |
2 ms |
2660 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2812 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2812 KB |
Output is correct |
25 |
Correct |
1 ms |
2624 KB |
Output is correct |
26 |
Correct |
2 ms |
2904 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
46 ms |
14468 KB |
Output is correct |
37 |
Correct |
56 ms |
18772 KB |
Output is correct |
38 |
Correct |
43 ms |
10848 KB |
Output is correct |
39 |
Correct |
58 ms |
11032 KB |
Output is correct |
40 |
Correct |
33 ms |
11020 KB |
Output is correct |
41 |
Correct |
56 ms |
16556 KB |
Output is correct |
42 |
Correct |
45 ms |
17236 KB |
Output is correct |
43 |
Correct |
23 ms |
9972 KB |
Output is correct |
44 |
Correct |
43 ms |
11044 KB |
Output is correct |
45 |
Correct |
55 ms |
10896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2800 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2808 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2816 KB |
Output is correct |
14 |
Correct |
2 ms |
2816 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2732 KB |
Output is correct |
18 |
Correct |
2 ms |
2828 KB |
Output is correct |
19 |
Correct |
62 ms |
14380 KB |
Output is correct |
20 |
Correct |
55 ms |
18576 KB |
Output is correct |
21 |
Correct |
48 ms |
11012 KB |
Output is correct |
22 |
Correct |
63 ms |
10956 KB |
Output is correct |
23 |
Correct |
51 ms |
11044 KB |
Output is correct |
24 |
Correct |
2 ms |
2660 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2812 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2812 KB |
Output is correct |
32 |
Correct |
1 ms |
2624 KB |
Output is correct |
33 |
Correct |
2 ms |
2904 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2652 KB |
Output is correct |
41 |
Correct |
2 ms |
2652 KB |
Output is correct |
42 |
Correct |
2 ms |
2652 KB |
Output is correct |
43 |
Correct |
46 ms |
14468 KB |
Output is correct |
44 |
Correct |
56 ms |
18772 KB |
Output is correct |
45 |
Correct |
43 ms |
10848 KB |
Output is correct |
46 |
Correct |
58 ms |
11032 KB |
Output is correct |
47 |
Correct |
33 ms |
11020 KB |
Output is correct |
48 |
Correct |
56 ms |
16556 KB |
Output is correct |
49 |
Correct |
45 ms |
17236 KB |
Output is correct |
50 |
Correct |
23 ms |
9972 KB |
Output is correct |
51 |
Correct |
43 ms |
11044 KB |
Output is correct |
52 |
Correct |
55 ms |
10896 KB |
Output is correct |
53 |
Correct |
68 ms |
18668 KB |
Output is correct |
54 |
Correct |
51 ms |
16984 KB |
Output is correct |
55 |
Correct |
32 ms |
9144 KB |
Output is correct |
56 |
Correct |
55 ms |
14428 KB |
Output is correct |
57 |
Correct |
50 ms |
11104 KB |
Output is correct |
58 |
Correct |
34 ms |
11136 KB |
Output is correct |
59 |
Correct |
34 ms |
10836 KB |
Output is correct |
60 |
Correct |
34 ms |
10844 KB |
Output is correct |