codeforces 514E E. Darth Vader and Tree(矩阵快速幂 )
来源:互联网 发布:淘宝网购物女装外套妮子风衣 编辑:程序博客网 时间:2024/05/05 10:19
题目链接:
codeforces 514E
题目大意:
给出一个每个节点有n个孩子的多叉树,父亲到第i个孩子有固定的长度,问到根节点的距离不超过x的节点的数目。
题目分析:
- 当x<=100时,
dp[i]=∑j=1100dp[i−j]⋅num[j] ans=∑i=0xdp[i] - 因为是线性递推,所以可以利用矩阵加速,根据转移方程构造出转移的矩阵即可。
AC代码:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")#define LL __int64#define pi acos(-1.0)const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;LL a[102], dp[102], sum[102];struct Matrix { LL ma[102][102];} init,res;Matrix Mult(Matrix x, Matrix y){ Matrix tmp; for(int i=0; i<101; i++) { for(int j=0; j<101; j++) { tmp.ma[i][j]=0; for(int k=0; k<101; k++) { tmp.ma[i][j]+=x.ma[i][k]*y.ma[k][j]; if(tmp.ma[i][j]>=mod) tmp.ma[i][j]%=mod; } } } return tmp;}Matrix Pow(Matrix x, int k){ Matrix tmp; int i, j; for(i=0; i<101; i++) for(j=0; j<101; j++) tmp.ma[i][j]=(i==j); while(k) { if(k&1) tmp=Mult(tmp,x); x=Mult(x,x); k>>=1; } return tmp;}int main(){ int n, x, i, j, y; while(scanf("%d%d",&n,&x)!=EOF) { memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); for(i=0; i<n; i++) { scanf("%d",&y); a[y]++; } dp[0]=1; sum[0]=0; for(i=1; i<=100; i++) { for(j=1; j<=i; j++) { dp[i]+=dp[i-j]*a[j]; if(dp[i]>=mod) dp[i]%=mod; } sum[i]=sum[i-1]+dp[i]; if(sum[i]>=mod) sum[i]%=mod; } if(x<=100) { printf("%I64d\n",(sum[x]+1)%mod); continue ; } memset(init.ma,0,sizeof(init.ma)); for(i=0; i<100; i++) { init.ma[0][i]=init.ma[100][i]=a[i+1]; } for(i=1; i<100; i++) { init.ma[i][i-1]=1; } init.ma[100][100]=1; res=Pow(init,x-100); LL ans=(sum[100]*res.ma[100][100])%mod; for(i=1; i<=100; i++) { ans+=dp[i]*res.ma[100][100-i]; if(ans>=mod) ans%=mod; } printf("%I64d\n",(ans+1)%mod); } return 0;}
0 0
- codeforces 514E 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+矩阵快速幂优化】
- 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 E. Darth Vader and Tree(矩阵应用)
- 矩阵快速幂DP Darth Vader and Tree : CodeForces
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree(矩阵优化DP)
- 【 Codeforces 514E 】Darth Vader and Tree - DP 矩乘转移
- Codeforces 514E 矩阵快速幂
- codeforces #250E The Child and Binary Tree 快速傅里叶变换
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(dp + 矩阵快速幂)
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
- 优先级队列
- E-UTRA channel bandwidths per operating band (36.101)
- C# abstract类 和 interface 的 区别
- android中设置控件背景的3种方式
- uva 1447 - Malfatti Circles(几何+二分)
- codeforces 514E E. Darth Vader and Tree(矩阵快速幂 )
- A multiplication game
- 在项目中遇到的 maven 插件常用的配置
- 分布式在线数据库学习 && 资料汇总
- vim的一些操作
- Install JDK for linux
- MongoDB简介安装
- rust sugar
- RStudio启动报错(fatal error)