【 Codeforces 514E 】Darth Vader and Tree - DP 矩乘转移
来源:互联网 发布:mac写日记的软件 编辑:程序博客网 时间:2024/05/19 01:12
题面比较文艺。
大意:给一棵树,这个树有无限个节点。对于每个点,都有n个儿子,第i个儿子与这个点的距离为
首先可以列出一个DP:
似乎不是那么好做。但是注意到,
令
有伴随矩阵
这个是显然的。左半部分是移位用的,倒数第二列用来计算新的f值,最后一列用新的f值加上s得到新的s值。
这样答案就是
当然前面要DP预处理一下,然后判断一下,再跑矩乘。
#include <bits/stdc++.h>#define rep(i,a,b) for(int i = a , _ = b ; i <= _ ; i ++)#define per(i,a,b) for(int i = a , _ = b ; i >= _ ; i --)#define For(i,a,b) for(int i = a , _ = b ; i < _ ; i ++)inline int rd() { char c = getchar(); while (!isdigit(c)) c = getchar() ; int x = c - '0'; while (isdigit(c = getchar())) x = x * 10 + c - '0'; return x;}const int mod = 1000000007;typedef long long ll;inline int mul(int a , int b) { return (ll) a * b % mod; }struct Matrix { int a[101][101]; Matrix() { memset(a , 0 , sizeof a) ; } friend Matrix operator*(Matrix&A , Matrix&B) { Matrix C = Matrix(); For (i , 0 , 101) For (j , 0 , 101) For (k , 0 , 101) (C.a[i][j] += mul(A.a[i][k] , B.a[k][j])) %= mod; return C; }}A , B , T;int n , x , cnt[101] , f[101];void input() { n = rd() , x = rd(); rep (i , 1 , n) cnt[rd()] ++;}void init() { f[0] = 1; rep (i , 1 , 100) rep (j , 1 , i) (f[i] += mul(f[i - j] , cnt[j])) %= mod;}void solve() { init(); if (x <= 100) { int ans = 0; rep (i , 0 , x) (ans += f[i]) %= mod; printf("%d\n" , ans); return; } For (i , 0 , 100) A.a[0][i] = f[i + 1] , (A.a[0][100] += f[i + 1]) %= mod; For (i , 0 , 99 ) B.a[i + 1][i] = 1; B.a[100][100] = 1; For (i , 0 , 100) B.a[i][99] = B.a[i][100] = cnt[100 - i]; For (i , 0 , 101) T.a[i][i] = 1; for (x -= 100;x;x >>= 1) { if (x & 1) T = T * B; B = B * B; } A = A * T; printf("%d\n" , (A.a[0][100] + 1) % mod);}int main() { #ifndef ONLINE_JUDGE freopen("data.txt" , "r" , stdin); #endif input(); solve(); return 0;}
0 0
- 【 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+矩阵快速幂优化】
- Codeforces 514E 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(矩阵优化DP)
- codeforces 514E E. Darth Vader and Tree(矩阵快速幂 )
- codeforces 514E E. Darth Vader and Tree(矩阵应用)
- 矩阵快速幂DP Darth Vader and Tree : CodeForces
- 【矩阵快速幂】 Codeforces Round #291 (Div. 2) E. Darth Vader and Tree
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree 矩阵快速幂
- 【codeforces 734E】Anton and Tree【缩点+DP】
- Codeforces 864E Round#436E :记录转移的DP
- codeforces 23E Tree (树形dp)
- Codeforces 463E Caisa and Tree(暴力)
- 问题:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Dri
- 验证纯数字,以及数字位数
- LA 3644 (并查集)
- Android笔记 - Binder之数据结构(一)
- test
- 【 Codeforces 514E 】Darth Vader and Tree - DP 矩乘转移
- 国内的pythoner强烈建议使用豆瓣的pypi源
- JavaScript-获得当前时间
- pyhton序列化
- php数据结构算法示例
- Android内存溢出 (oom)实战
- 菜鸟猿大战Java之集合框架系列(四)
- CentOS7编译安装apache httpd及问题详解
- Ceph 多节点quick部署