Power of Matrix 等比数列求和 矩阵版!
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 49
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-;
//矩阵快速幂
int n, k;
struct Mat
{
int a[MAXN][MAXN];
Mat()
{
memset(a, , sizeof(a));
}
Mat operator *(const Mat& rhs)
{
Mat ret;
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
if (a[i][j])
{
for (int t = ; t < n; t++)
{
ret.a[i][t] = (ret.a[i][t] + a[i][j] * rhs.a[j][t])%;
}
}
}
}
return ret;
}
Mat operator +(const Mat& rhs)
{
Mat ret;
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
ret.a[i][j] += (a[i][j] + rhs.a[i][j])%;
}
}
return ret;
}
};
Mat e;
Mat fpow(const Mat& m, int b)
{
Mat ans, tmp = m;
for (int i = ; i < n; i++)
ans.a[i][i] = ;
while (b != )
{
if (b & )
ans = tmp*ans;
tmp = tmp * tmp;
b >>= ;
}
return ans;
}
Mat sum(const Mat& m, int k)
{
if (k == ) return m;
else if (k % == )
{
return (e + fpow(m, k / )) * sum(m, k / );
}
else if (k % == )
{
Mat tmp = fpow(m, k / + );
return (e + tmp)*sum(m, k / ) + tmp;
}
} int main()
{
while (scanf("%d%d", &n,&k), n)
{
for (int i = ; i < n; i++)
e.a[i][i] = ;
Mat M;
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
scanf("%d", &M.a[i][j]);
M.a[i][j] %= ;
}
}
M = sum(M, k);
for (int i = ; i < n; i++)
{
printf("%d", M.a[i][]);
for (int j = ; j < n; j++)
{
printf("% d", M.a[i][j]);
}
printf("\n");
}
printf("\n");
}
}
Power of Matrix 等比数列求和 矩阵版!的更多相关文章
- Power of Matrix(uva11149+矩阵快速幂)
Power of Matrix Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- ZOJ-3774 Power of Fibonacci——等比数列求和&&等价替换
题目 求 $\displaystyle \sum_{i=1}^n F_i^k$,($1 \leq n\leq 10^{18},1 \leq k\leq 10^5$),答案对 $10^9+9$ 取模. ...
- UVA 11149 - Power of Matrix(矩阵乘法)
UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞.∑kiAi=(1+Ak/2)∑k/2iAi,不断二分就可以 代码: ...
- UVa 11149 Power of Matrix(倍增法、矩阵快速幂)
题目链接: 传送门 Power of Matrix Time Limit: 3000MS Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...
- luogu1397 [NOI2013]矩阵游戏 (等比数列求和)
一个比较显然的等比数列求和,但有一点问题就是n和m巨大.. 考虑到他们是在幂次上出现,所以可以模上P-1(费马小定理) 但是a或c等于1的时候,不能用等比数列求和公式,这时候就要乘n和m,又要变成模P ...
- SPOJ AMR10E Stocks Prediction --二分求和+矩阵快速幂
题意:给一个递推式S(n) = a1*S(n-1)+...+aR*S(n-R),要求S(k)+S(2k)+...+S(nk)的值. 分析:看到n的大小和递推式,容易想到矩阵快速幂.但是如何转化呢? 首 ...
- UVA 11149 Power of Matrix 快速幂
题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...
- POJ 1845 (约数和+二分等比数列求和)
题目链接: http://poj.org/problem?id=1845 题目大意:A^B的所有约数和,mod 9901. 解题思路: ①整数唯一分解定理: 一个整数A一定能被分成:A=(P1^K1) ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
随机推荐
- [读书笔记3]《C语言嵌入式系统编程修炼》
第五章 性能优化 5.1 使用宏定义 在C语言中,宏是产生内嵌代码的唯一方法.对于嵌入式系统而言,为了能达到性能要求,宏是一种很好的代替函数的方法. 写一个"标准"宏MIN ...
- 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game
题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...
- 题解报告:hdu 1312 Red and Black(简单dfs)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- ACM_闹钟人生(水题)
闹钟人生 Time Limit: 2000/1000ms (Java/Others) Problem Description: 已知一个时钟一开始指向0点,顺时针走了n个小时,求它最终所指向的数字(时 ...
- EditText(8)EditText中drawableRight图片的点击事件
参考: http://stackoverflow.com/questions/3554377/handling-click-events-on-a-drawable-within-an-edittex ...
- ibatis入门教程一
这几天研究ibatis玩,参考一篇贴子进行安装配置:蓝雪森林 选择这个帖子来跟随配置是因为这个帖子看着比较干净,但是我仍旧在配置得过程中出现了好几个问题,所以我决定在这个帖子的基础上将更多细节加上,做 ...
- js易混API汇总
一:slice()方法 ————————————http://www.w3school.com.cn/jsref/jsref_slice_string.asp ———————————————————— ...
- CSS 潜藏着的BFC
在写样式时,往往是添加了一个样式,又或者是修改了某个属性,就达到了我们的预期. 而BFC就潜藏在其中,当你修改样式时,一不小心就能触发它而毫无察觉,因此没有意识到BFC的神奇之处. 什么是BFC(Bl ...
- Dreamweaver启动出错--Designer.xml错误
Designer.xml错误导致Dreamweaver CS4无法启动 xml parsing fatal error:Invalid document structure,line:1, file: ...
- SqlBulkCopy实现大批量数据导入
//自增列重新生成:SqlBulkCopy bc = new SqlBulkCopy(conn) //自增列保留原值:SqlBulkCopy bc = new SqlBulkCopy(conn,Sql ...