This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
| ~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |