#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int mod=1e9+7;
int add(int x,int y){x+=y;return x>=mod?x-mod:x;}
int sub(int x,int y){x-=y;return x<0?x+mod:x;}
int mul(int x,int y){return (ll)x*y%mod;}
void ckadd(int&x,int y){x=add(x,y);}
void cksub(int&x,int y){x=sub(x,y);}
void ckmul(int&x,int y){x=mul(x,y);}
int powmod(int x,ll k){
int ans=1;
for(;k;k>>=1,ckmul(x,x))if(k&1)ckmul(ans,x);
return ans;
}
const int N=100050;
int n;
ll d;
vector<int> E[N];
struct info{
int x,y,z;
info(int a=0,int b=0,int c=0):x(a),y(b),z(c){}
info operator + (info b){return info(add(x,b.x),add(y,b.y),add(z,b.z));}
info operator * (info b){return info(mul(x,b.x),add(mul(x,b.y),mul(b.x,y)),add(mul(x,b.z),mul(b.x,z)));}
int get(int w,int l){return add(x,add(mul(w,y),mul(l,z)));}
void p(){printf("%i %i %i\n",x,y,z);}
};
info operator += (info&a,info b){return a=a+b;}
info operator *= (info&a,info b){return a=a*b;}
struct DP{
info a[2][2];
void cl(){for(int i=0;i<2;++i)for(int j=0;j<2;++j)a[i][j]=info();}
DP(){}
void id(){a[0][0]=info(1);}
};
DP operator + (DP a,DP b){
DP ans;
for(int i=0;i<2;++i)
for(int j=0;j<2;++j)
for(int k=0;k<2;++k)
for(int l=0;l<2;++l){
if(i+k<2){
ans.a[i|k][j|l]+=a.a[i][j]*b.a[k][l];
}
}
return ans;
}
DP operator += (DP&a,DP b){return a=a+b;}
DP sw(DP a){
DP b=a;
for(int i=0;i<2;++i)swap(b.a[i][0],b.a[i][1]);
return b;
}
DP dw[N],idx;
void DW(int u,int p){
dw[u]=idx;
for(int v:E[u])if(v!=p){
DW(v,u);
dw[u]+=sw(dw[v]);
}
}
DP dp[N],up[N];
void UP(int u,int p){
dp[u]=sw(up[u])+dw[u];
DP tmp=idx;
for(int v:E[u])if(v!=p){
up[v]=tmp;
tmp+=sw(dw[v]);
}
tmp=sw(up[u]);
reverse(E[u].begin(),E[u].end());
for(int v:E[u])if(v!=p){
up[v]+=tmp;
UP(v,u);
tmp+=sw(dw[v]);
}
}
#define matrix vector<vector<int>>
matrix mul(matrix a,matrix b){
int n=a.size();
matrix ans(n,vector<int>(n,0));
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
for(int k=0;k<n;++k)
ckadd(ans[i][j],mul(a[i][k],b[k][j]));
return ans;
}
matrix pow(matrix x,ll k){
int n=x.size();
matrix ans(n,vector<int>(n,0));
for(int i=0;i<n;i++)ans[i][i]=1;
for(;k;k>>=1,x=mul(x,x))if(k&1)ans=mul(ans,x);
return ans;
}
vector<int> mul(matrix a,vector<int> b){
int n=a.size();
vector<int> ans(n,0);
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
ckadd(ans[i],mul(a[i][j],b[j]));
return ans;
}
int main(){
//freopen("input1.txt","r",stdin);
scanf("%i %lld",&n,&d);
for(int i=1,u,v;i<n;i++)scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
idx.id();
idx.a[1][0]=info(0,1,0);
idx.a[1][1]=info(0,0,1);
DW(1,0);
up[1].id();
up[1]=sw(up[1]);
UP(1,0);
info sum_win=info(),sum_lose=info();
for(int i=1;i<=n;i++){
sum_win+=dp[i].a[1][1];
sum_lose+=dp[i].a[1][0];
//printf("%i\n",i);
//dw[i].a[1][1].p();
//dw[i].a[1][0].p();
}
//printf("sum\n");
//sum_win.p();
//sum_lose.p();
int w=0,l=0;
for(int i=1;i<=n;i++){
ckadd(w,dp[i].a[0][1].x);
ckadd(l,dp[i].a[0][0].x);
}
matrix base(3,vector<int>(3,0));
base[0][0]=sum_lose.z;
base[0][1]=sum_lose.y;
base[0][2]=sum_lose.x;
base[1][0]=sum_win.z;
base[1][1]=sum_win.y;
base[1][2]=sum_win.x;
base[2][2]=1;
vector<int> v={l,w,1};
base=pow(base,d-1);
/*for(int i=1;i<d;i++){
tie(w,l)=make_pair(sum_win.get(w,l),sum_lose.get(w,l));
}*/
v=mul(base,v);
l=v[0];
w=v[1];
int ans=dp[1].a[1][1].get(w,l);
printf("%i\n",ans);
return 0;
}
Compilation message
startrek.cpp: In function 'int main()':
startrek.cpp:106:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
106 | scanf("%i %lld",&n,&d);
| ~~~~~^~~~~~~~~~~~~~~~~
startrek.cpp:107:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
107 | for(int i=1,u,v;i<n;i++)scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
13 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
12 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16744 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16744 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
7 |
Correct |
13 ms |
17024 KB |
Output is correct |
8 |
Correct |
13 ms |
17152 KB |
Output is correct |
9 |
Correct |
14 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
11 |
Correct |
13 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16744 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
7 |
Correct |
13 ms |
17024 KB |
Output is correct |
8 |
Correct |
13 ms |
17152 KB |
Output is correct |
9 |
Correct |
14 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
11 |
Correct |
13 ms |
16768 KB |
Output is correct |
12 |
Correct |
267 ms |
35832 KB |
Output is correct |
13 |
Correct |
285 ms |
54368 KB |
Output is correct |
14 |
Correct |
228 ms |
20136 KB |
Output is correct |
15 |
Correct |
232 ms |
19960 KB |
Output is correct |
16 |
Correct |
241 ms |
19960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16744 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
7 |
Correct |
13 ms |
17024 KB |
Output is correct |
8 |
Correct |
13 ms |
17152 KB |
Output is correct |
9 |
Correct |
14 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
11 |
Correct |
13 ms |
16768 KB |
Output is correct |
12 |
Correct |
11 ms |
16768 KB |
Output is correct |
13 |
Correct |
13 ms |
16768 KB |
Output is correct |
14 |
Correct |
11 ms |
16768 KB |
Output is correct |
15 |
Correct |
11 ms |
16768 KB |
Output is correct |
16 |
Correct |
11 ms |
16772 KB |
Output is correct |
17 |
Correct |
11 ms |
16768 KB |
Output is correct |
18 |
Correct |
11 ms |
16768 KB |
Output is correct |
19 |
Correct |
11 ms |
16768 KB |
Output is correct |
20 |
Correct |
11 ms |
16768 KB |
Output is correct |
21 |
Correct |
13 ms |
17024 KB |
Output is correct |
22 |
Correct |
14 ms |
17152 KB |
Output is correct |
23 |
Correct |
13 ms |
16768 KB |
Output is correct |
24 |
Correct |
13 ms |
16896 KB |
Output is correct |
25 |
Correct |
13 ms |
16768 KB |
Output is correct |
26 |
Correct |
13 ms |
16896 KB |
Output is correct |
27 |
Correct |
13 ms |
17280 KB |
Output is correct |
28 |
Correct |
12 ms |
16768 KB |
Output is correct |
29 |
Correct |
13 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16744 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
11 ms |
16768 KB |
Output is correct |
7 |
Correct |
13 ms |
17024 KB |
Output is correct |
8 |
Correct |
13 ms |
17152 KB |
Output is correct |
9 |
Correct |
14 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
11 |
Correct |
13 ms |
16768 KB |
Output is correct |
12 |
Correct |
267 ms |
35832 KB |
Output is correct |
13 |
Correct |
285 ms |
54368 KB |
Output is correct |
14 |
Correct |
228 ms |
20136 KB |
Output is correct |
15 |
Correct |
232 ms |
19960 KB |
Output is correct |
16 |
Correct |
241 ms |
19960 KB |
Output is correct |
17 |
Correct |
11 ms |
16768 KB |
Output is correct |
18 |
Correct |
13 ms |
16768 KB |
Output is correct |
19 |
Correct |
11 ms |
16768 KB |
Output is correct |
20 |
Correct |
11 ms |
16768 KB |
Output is correct |
21 |
Correct |
11 ms |
16772 KB |
Output is correct |
22 |
Correct |
11 ms |
16768 KB |
Output is correct |
23 |
Correct |
11 ms |
16768 KB |
Output is correct |
24 |
Correct |
11 ms |
16768 KB |
Output is correct |
25 |
Correct |
11 ms |
16768 KB |
Output is correct |
26 |
Correct |
13 ms |
17024 KB |
Output is correct |
27 |
Correct |
14 ms |
17152 KB |
Output is correct |
28 |
Correct |
13 ms |
16768 KB |
Output is correct |
29 |
Correct |
13 ms |
16896 KB |
Output is correct |
30 |
Correct |
13 ms |
16768 KB |
Output is correct |
31 |
Correct |
13 ms |
16896 KB |
Output is correct |
32 |
Correct |
13 ms |
17280 KB |
Output is correct |
33 |
Correct |
12 ms |
16768 KB |
Output is correct |
34 |
Correct |
13 ms |
16768 KB |
Output is correct |
35 |
Correct |
13 ms |
16768 KB |
Output is correct |
36 |
Correct |
255 ms |
35708 KB |
Output is correct |
37 |
Correct |
273 ms |
54264 KB |
Output is correct |
38 |
Correct |
230 ms |
20140 KB |
Output is correct |
39 |
Correct |
238 ms |
19996 KB |
Output is correct |
40 |
Correct |
241 ms |
20088 KB |
Output is correct |
41 |
Correct |
265 ms |
45304 KB |
Output is correct |
42 |
Correct |
248 ms |
51064 KB |
Output is correct |
43 |
Correct |
200 ms |
19828 KB |
Output is correct |
44 |
Correct |
237 ms |
20088 KB |
Output is correct |
45 |
Correct |
230 ms |
19960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16768 KB |
Output is correct |
2 |
Correct |
13 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
12 ms |
16768 KB |
Output is correct |
7 |
Correct |
11 ms |
16768 KB |
Output is correct |
8 |
Correct |
11 ms |
16768 KB |
Output is correct |
9 |
Correct |
11 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16768 KB |
Output is correct |
11 |
Correct |
11 ms |
16744 KB |
Output is correct |
12 |
Correct |
11 ms |
16768 KB |
Output is correct |
13 |
Correct |
11 ms |
16768 KB |
Output is correct |
14 |
Correct |
13 ms |
17024 KB |
Output is correct |
15 |
Correct |
13 ms |
17152 KB |
Output is correct |
16 |
Correct |
14 ms |
16768 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
13 ms |
16768 KB |
Output is correct |
19 |
Correct |
267 ms |
35832 KB |
Output is correct |
20 |
Correct |
285 ms |
54368 KB |
Output is correct |
21 |
Correct |
228 ms |
20136 KB |
Output is correct |
22 |
Correct |
232 ms |
19960 KB |
Output is correct |
23 |
Correct |
241 ms |
19960 KB |
Output is correct |
24 |
Correct |
11 ms |
16768 KB |
Output is correct |
25 |
Correct |
13 ms |
16768 KB |
Output is correct |
26 |
Correct |
11 ms |
16768 KB |
Output is correct |
27 |
Correct |
11 ms |
16768 KB |
Output is correct |
28 |
Correct |
11 ms |
16772 KB |
Output is correct |
29 |
Correct |
11 ms |
16768 KB |
Output is correct |
30 |
Correct |
11 ms |
16768 KB |
Output is correct |
31 |
Correct |
11 ms |
16768 KB |
Output is correct |
32 |
Correct |
11 ms |
16768 KB |
Output is correct |
33 |
Correct |
13 ms |
17024 KB |
Output is correct |
34 |
Correct |
14 ms |
17152 KB |
Output is correct |
35 |
Correct |
13 ms |
16768 KB |
Output is correct |
36 |
Correct |
13 ms |
16896 KB |
Output is correct |
37 |
Correct |
13 ms |
16768 KB |
Output is correct |
38 |
Correct |
13 ms |
16896 KB |
Output is correct |
39 |
Correct |
13 ms |
17280 KB |
Output is correct |
40 |
Correct |
12 ms |
16768 KB |
Output is correct |
41 |
Correct |
13 ms |
16768 KB |
Output is correct |
42 |
Correct |
13 ms |
16768 KB |
Output is correct |
43 |
Correct |
255 ms |
35708 KB |
Output is correct |
44 |
Correct |
273 ms |
54264 KB |
Output is correct |
45 |
Correct |
230 ms |
20140 KB |
Output is correct |
46 |
Correct |
238 ms |
19996 KB |
Output is correct |
47 |
Correct |
241 ms |
20088 KB |
Output is correct |
48 |
Correct |
265 ms |
45304 KB |
Output is correct |
49 |
Correct |
248 ms |
51064 KB |
Output is correct |
50 |
Correct |
200 ms |
19828 KB |
Output is correct |
51 |
Correct |
237 ms |
20088 KB |
Output is correct |
52 |
Correct |
230 ms |
19960 KB |
Output is correct |
53 |
Correct |
267 ms |
54332 KB |
Output is correct |
54 |
Correct |
265 ms |
47224 KB |
Output is correct |
55 |
Correct |
180 ms |
19516 KB |
Output is correct |
56 |
Correct |
254 ms |
35704 KB |
Output is correct |
57 |
Correct |
238 ms |
19960 KB |
Output is correct |
58 |
Correct |
234 ms |
20088 KB |
Output is correct |
59 |
Correct |
237 ms |
19960 KB |
Output is correct |
60 |
Correct |
231 ms |
20088 KB |
Output is correct |