Codeforces Round #291 (Div. 2) E. Darth Vader and Tree(矩阵优化DP)
来源:互联网 发布:mac如何进行文件管理 编辑:程序博客网 时间:2024/05/19 01:12
题意:给你一颗树,有无限个节点,每个节点有n个儿子,据他第i个儿子的距离为di,问这棵树有多少个点离根的距
离不超过x。
思路:
f[i]表示路径长度恰为i的能到的点个数,首先可以列出一个DP:
似乎不是那么好做。但是注意到,
构造矩阵:
代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 105;const int mod = 1e9+7;ll n, x, cnt[maxn];ll dp[maxn];struct node{ ll s[maxn][maxn];};node mul(node a, node b){ node t; memset(t.s, 0, sizeof(t.s)); for(int i = 1; i <= 101; i++) for(int j = 1; j <= 101; j++) for(int k = 1; k <= 101; k++) t.s[i][j] = (t.s[i][j]+a.s[i][k]*b.s[k][j])%mod; return t;}node mt_pow(node a, int q){ node res; memset(res.s, 0, sizeof(res.s)); for(int i = 1; i <= 101; i++) res.s[i][i] = 1; while(q) { if(q%2) res = mul(res, a); a = mul(a, a); q /= 2; } return res;}int main(void){ while(cin >> n >> x) { memset(cnt, 0, sizeof(cnt)); memset(dp, 0, sizeof(dp)); for(int i = 1; i <= n; i++) { int x; scanf("%d", &x); cnt[x]++; } dp[0] = 1; for(int i = 1; i < maxn; i++) for(int j = 1; j <= i; j++) dp[i] = (dp[i]+cnt[j]*dp[i-j]%mod)%mod; ll sum = 0; if(x <= 100) { for(int i = 0; i <= x; i++) sum = (sum+dp[i])%mod; printf("%I64d\n", sum); continue; } for(int i = 0; i <= 100; i++) sum = (sum+dp[i])%mod; node base; memset(base.s, 0, sizeof(base.s)); for(int i = 1; i <= 99; i++) base.s[i][i+1] = 1; for(int i = 1; i <= 100; i++) base.s[100][i] = base.s[101][i] = cnt[101-i]; base.s[101][101] = 1; node ans = mt_pow(base, x-100); ll res = 0; for(int i = 1; i <= 100; i++) res = (res+ans.s[101][i]*dp[i]%mod)%mod; res = (res+ans.s[101][101]*sum%mod)%mod; printf("%I64d\n", res); } return 0;}
阅读全文
1 0
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree(矩阵优化DP)
- Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)
- Codeforces Round #291 (Div. 2)E. Darth Vader and Tree——dp+矩阵快速幂
- 【矩阵快速幂】 Codeforces Round #291 (Div. 2) E. Darth Vader and Tree
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree 矩阵快速幂
- Codeforces 514E Darth Vader and Tree【Dp+矩阵快速幂优化】
- Codeforces 514E Darth Vader and Tree【Dp+矩阵快速幂优化】
- codeforces 514E Darth Vader and Tree (dp+快速幂)
- Codeforces 514E Darth Vader and Tree DP + 矩阵快速幂
- Codeforces 514E. Darth Vader and Tree DP+矩阵快速幂
- 矩阵快速幂DP Darth Vader and Tree : CodeForces
- codeforces 514E E. Darth Vader and Tree(矩阵快速幂 )
- codeforces 514E E. Darth Vader and Tree(矩阵应用)
- 【 Codeforces 514E 】Darth Vader and Tree - DP 矩乘转移
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(矩阵优化DP)
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(矩阵优化DP)★
- Codeforces Round #291 (Div. 2)E(DP+矩阵)
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(dp + 矩阵快速幂)
- openStreetMap ,Leaflet and XGeocoding V2之初步使用
- 手机隐私泄漏都因为干了这些事!快查查这些事情你做过没
- J涂色问题
- My First Blog
- 矩阵数据类型
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree(矩阵优化DP)
- maven-springmvc 整合配置
- jquery-ui sortable 排序
- ASP.NET控件Web CAD SDK发布v12版本,支持DWG 2018
- ListView的多条目加载
- SDL2入门教程(02_Getting an Image on the Screen)
- Chrome学习笔记(一):线程模型,消息循环
- dll->mdb
- 1013. 数素数 (20)