Submission #1156977

#TimeUsernameProblemLanguageResultExecution timeMemory
1156977RSAMSDStar Trek (CEOI20_startrek)C++20
0 / 100
8 ms2632 KiB
#include<bits/stdc++.h> using namespace std; const int f = 1e5+10; const int mod = 1e9+7; vector<int> g[f]; long long dp[f]; bool B[f]; struct matric{ long long id[5][5]; matric(){ for(int i =0;i<5;i++){ for(int j =0;j<5;j++){ id[i][j] =0; } } } }; long long powmod(long long a,long long p){ long long ans = 1; while(p>0){ if(p&1)ans = ans*a%mod; p/=2;a = a*a%mod; } return ans; } matric makemat(long long x,long long n){ matric a; a.id[0][0] = x; a.id[1][0] = 1; a.id[0][1] = 0; a.id[1][1] = powmod(n,2); return a; } matric zarb(matric a,matric b){ matric ans; for(int i =0;i<2;i++){ for(int j =0;j<2;j++){ ans.id[i][j] =0; for(int h =0;h<2;h++){ ans.id[i][j]+=a.id[i][h]*b.id[h][j]%mod; ans.id[i][j]%mod; } } } return ans; } matric powmat(matric a,long long p){ matric ans = a;p--; while(p>0){ if(p&1)ans = zarb(ans,a); p/=2;a = zarb(a,a); } return ans; } int dfs(int v,int par){ int cnt =0; int much =0; int meny = 0; for(int u:g[v]){ if(u==par)continue; cnt+=dfs(u,v); much+=!B[u]; } if(much==0){ B[v] = 0; for(int u:g[v]){ if(u==par)continue; meny+=dp[u]; } dp[v] = meny; // cout<<v<<'\n'; } else if (much==1){ B[v] = 1; for(int u:g[v]){ if(u==par)continue; if(!B[u])meny+=dp[u]; } dp[v] = meny; } else{ B[v] = 1; dp[v] = 0; } cnt+=!B[v]; dp[v]+=!B[v]; return cnt; } int main(){ int n,d;cin>>n>>d; for(int i =0;i<n-1;i++){ int l,r;cin>>l>>r; g[l].push_back(r); g[r].push_back(l); } long long mI =0,mH = 0,I =0,hI =0; for(int i =n;i>=1;i--){ hI = dfs(i,0); if(B[i]){ mH+=dp[i]; } else { mI+=dp[i]; I++; // cout<<i<<'\n'; } mI%=mod;mH%=mod; } matric base; base.id[0][0] = I; base.id[0][1] = I*n%mod; base.id[1][0] = base.id[1][1] = 0; long long gI,fI; if(d>1){ matric mp = powmat(makemat(mI-mH,n),d-1); base = zarb(base,mp); } gI = base.id[0][0]; fI = powmod(powmod(n,2),d-1)-gI; fI+=mod;fI%=mod; if(!B[1]){ cout<<gI*dp[1]%mod; } else cout<<((fI*n%mod)+(gI*(hI-dp[1])%mod))%mod; // cout<<gI<<" "<<fI<<" "<<I; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...