Codeforces Round #291 (Div. 2) E. Darth Vader and Tree 矩阵快速幂
来源:互联网 发布:淘宝小石头是正品吗 编辑:程序博客网 时间:2024/05/22 06:17
When Darth Vader gets bored, he sits down on the sofa, closes his eyes and thinks of an infinite rooted tree where each node has exactly n sons, at that for each node, the distance between it an its i-th left child equals to di. The Sith Lord loves counting the number of nodes in the tree that are at a distance at most x from the root. The distance is the sum of the lengths of edges on the path between nodes.
But he has got used to this activity and even grew bored of it. 'Why does he do that, then?' — you may ask. It's just that he feels superior knowing that only he can solve this problem.
Do you want to challenge Darth Vader himself? Count the required number of nodes. As the answer can be rather large, find it modulo109 + 7.
The first line contains two space-separated integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 109) — the number of children of each node and the distance from the root within the range of which you need to count the nodes.
The next line contains n space-separated integers di (1 ≤ di ≤ 100) — the length of the edge that connects each node with its i-th child.
Print a single number — the number of vertexes in the tree at distance from the root equal to at most x.
3 31 2 3
8
Pictures to the sample (the yellow color marks the nodes the distance to which is at most three)
题意是求x分解成子树(每条边为权值为f(i))的总个数,可以得到递推公式
dp[x] = dp[n-f(1)] + dp[n-f(2)]+....dp[n-f(i)] + 1;
可以发现f(i)最大为100,所以可 以得到公式dp[x] = sum(dp[x-i]) + 1( 1=< i <= 100),这样总的复杂度为101*101*101*log(n);就可得到解了!
ans为结果矩阵,one为乘数矩阵。
#define INF9000000000000000000#define EPS(double)1e-9#define mod1000000007#define PI3.14159265358979//*******************************************************************************/#endif#define N 101#define MOD 1000000007int n,x,pri[100050];long long matrix[N][N];void multimatrix(long long aa[][101],long long bb[][101],long long ans[][101],int len){ long long c[101][101]; FI(len){ FJ(len){ c[i][j] = 0; FK(len){ c[i][j] += aa[i][k] * bb[k][j]; c[i][j] %= mod; } } } FI(len){ FJ(len){ ans[i][j] = c[i][j]; } }}long long twoMulti(int x){ long long one[101][101],ans[101][101]; memset(one,0,sizeof(one)); memset(ans,0,sizeof(ans)); ans[0][0] = ans[0][1]= 1; FI(n) one[pri[i]][1]++; one[0][1] = one[0][0] = 1; for(int i=1;i<100;i++){ one[i][i+1] = 1; } while(x){ if(x&1){ multimatrix(ans ,one,ans,101); } multimatrix(one,one,one,101); x/=2; } return ans[0][1];}int main(){ while (S2(n,x) != EOF) { FI(n){ S(pri[i]); } cout<<twoMulti(x)<<endl; } return 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
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree 矩阵快速幂
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree(矩阵优化DP)
- codeforces 514E E. Darth Vader and Tree(矩阵快速幂 )
- codeforces 514E Darth Vader and Tree (dp+快速幂)
- 矩阵快速幂DP Darth Vader and Tree : CodeForces
- 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 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 #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo(矩阵快速幂)
- nginx+lua_nginx+GraphicsMagick缩略图+tfs获取原图+ngx_cache_purge
- 高效加载图片(下)
- iOS问题集锦
- OpenCV之十八 OpenCV仿射变换 & SURF特征点描述合辑
- 《软件开发者路线图:从学徒到高手》
- Codeforces Round #291 (Div. 2) E. Darth Vader and Tree 矩阵快速幂
- eclipse错误 timeout
- C#实现字符串,文件获取Md5加密校验方法
- 数据抽象
- windows 虚拟内存映射机制
- CSS3制作精美的iphone电话图标,不使用图片
- 【Xcode使用技巧】xcode 使用Code Snippet 添加自己常用的代码段
- 一分钟读懂MapReduce
- iPhone开发应用Sqlite使用手册