LinghtOJ1132 Summing up Powers
来源:互联网 发布:淘宝新手入门教程视频 编辑:程序博客网 时间:2024/05/18 12:32
Given N and K, you have to find
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains two integers N (1 ≤ N ≤
Output
For each case, print the case number and the result.
Sample Input
3
3 1
4 2
3 3
Sample Output
Case 1: 6
Case 2: 30
Case 3: 36
题意就是求开始的那个式子。
不难看出递推式:
但是这样依旧没解决问题,我们要化出来的递推式中的元素应该要乘上1个相同的元素,然后才能构造成矩阵.也就是说比如
所以如果
那么我们把上面的递推公式转化:
那么进行
而
…
所以我们就可以构造矩阵:
这样乘出来之后就可以得到
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;typedef unsigned int uint;typedef long long LL;//计算组合数uint c[52][52];void get_c(){ for(int i = 0; i <= 50; ++i) for(int j = 0; j <= 50; ++j) { if(!j || i==j)c[i][j] = 1; else c[i][j] = c[i-1][j]+c[i-1][j-1]; }}LL n;int k;int Size;struct matrix{ uint a[52][52]={{0}}; matrix operator * (const matrix b)const { matrix ans; for(int i = 0; i < Size; ++i) for(int k = 0; k < Size; ++k)if(a[i][k]) for(int j = 0; j < Size; ++j) { ans.a[i][j] += a[i][k]*b.a[k][j]; } return ans; }};uint F(LL n){ matrix ans,base; for(int j = 0; j <= k+1; ++j)ans.a[0][j] = 1; base.a[0][0] = 1; for(int i = 1; i <= k+1; ++i)base.a[i][0] = c[k][i-1]; for(int j = 1; j <= k+1; ++j) for(int i = 1; i <= j; ++i)base.a[i][j] = c[j-1][i-1]; n--; while(n) { if(n & 1LL)ans = ans*base; base = base*base; n >>= 1LL; } return ans.a[0][0];}int main(){ int t; get_c(); scanf("%d",&t); int ca = 0; while(t--) { scanf("%lld%d",&n,&k); Size = k+2; printf("Case %d: %u\n",++ca,F(n)); } return 0;}
对
- LinghtOJ1132 Summing up Powers
- Summing up Powers LightOJ
- Summing up Powers LightOJ
- LightOJ - 1132 Summing up Powers
- Light oj 1132 - Summing up Powers
- light oj 1132 Summing up Powers
- 矩阵快速幂lightoj1132 - Summing up Powers
- Light OJ 1132 Summing up Powers 矩阵快速幂
- LightOJ - 1132 Summing up Powers 矩阵快速幂
- LightOJ 1132 - Summing up Powers 矩阵快速幂+排列组合
- [矩阵快速幂] LightOJ 1132 - Summing up Powers
- [矩阵快速幂] LightOJ 1142 - Summing up Powers (II)
- Light oj 1132 Summing up Powers (矩阵快速幂)
- Lightoj 1132 Summing up Powers(矩阵快速幂)
- LightOJ 1132 Summing up Powers(矩阵快速幂+二项式定理)
- LightOJ1132---Summing up Powers (矩阵快速幂+二项式定理(简单推理))
- 【矩阵快速幂】LightOJ_1132_Summing up Powers
- uva_112_Tree Summing
- 【最短路】基础
- Vue开发环境搭建及热更新
- Eclipse 配置第一个Spring Framework项目 with Maven
- 一天一道leecode 1
- Linux 之 shell 比较运算符
- LinghtOJ1132 Summing up Powers
- brew install rabbitmq
- TCP/IP 三次握手协议与四次放手协议
- Python纪实:用爬虫明确求职路上的技能树(一)
- Poj 3259 Wormholes ( SPFA
- Hadoop(四)----流程
- ssm之Struts(拦截器)
- 自适应6大调整
- oracle笔记