LOJ1070(SummerTrainingDay05-B 矩阵快速幂)
Algebraic Problem
Given the value of a+b and ab you will have to find the value of an+bn. a and bnot necessarily have to be real numbers.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains three non-negative integers, p, q and n. Here p denotes the value of a+b and q denotes the value of ab. Each number in the input file fits in a signed 32-bit integer. There will be no such input so that you have to find the value of 00.
Output
For each test case, print the case number and (an+bn) modulo 264.
Sample Input
2
10 16 2
7 12 3
Sample Output
Case 1: 68
Case 2: 91
令Sn = an+bn ,则有递推公式Sn = p×Sn-1 - q×Sn-2 ,构造矩阵[[p, 1], [-q, 0]],初始矩阵为[S1, S0]。
//2017-08-05
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll unsigned long long using namespace std; ll p, q;
const int N = ; struct Matrix{
ll a[N][N];
int r, c;
}ori, res; void init(){
memset(res.a, , sizeof(res.a));
res.r = ; res.c = ;
res.a[][] = p;
res.a[][] = ;
ori.r = ; ori.c = ;//构造矩阵
ori.a[][] = p;
ori.a[][] = ;
ori.a[][] = -q;
ori.a[][] = ;
} Matrix multi(Matrix x, Matrix y)//矩阵乘法
{
Matrix z;
memset(z.a, , sizeof(z.a));
z.r = x.r, z.c = y.c;
for(int i = ; i <= x.r; i++){
for(int k = ; k <= x.c; k++)//加速优化
{
if(x.a[i][k] == ) continue;
for(int j = ; j<= y.c; j++)
z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]));
}
}
return z;
} void Matrix_pow(int n)//矩阵快速幂
{
while(n){
if(n & )
res = multi(res, ori);
ori = multi(ori, ori);
n >>= ;
}
printf("%llu\n", res.a[][]);
} int main(){
int T, n;
scanf("%d", &T);
int kase = ;
while(T--){
scanf("%lld%lld%d", &p, &q, &n);
init();
printf("Case %d: ", ++kase);
if(n == )printf("2\n");
else if(n == )printf("%llu\n", p);
else Matrix_pow(n-);
} return ;
}
LOJ1070(SummerTrainingDay05-B 矩阵快速幂)的更多相关文章
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 51nod 1126 矩阵快速幂 水
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
- hdu2604(递推,矩阵快速幂)
题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
随机推荐
- Linux Shell脚本编程基础(11)
实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核,不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序.Shel编程语言具有普通编程 ...
- 初识node.js(通过npm下载项目依赖的包的过程)
一.初识node.js 简单的说Node.js 就是运行在服务器端的JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事 ...
- mac终端常用命令
1.du #查看文件目录大小 示例:查看DataCenter目录下所有文件/文件夹的大小 everSeeker:DataCenter pingping$ -h .9G ./Books 1.2M ./C ...
- C#6.0语言规范(十八) 不安全代码
前面章节中定义的核心C#语言与C和C ++的区别在于它省略了作为数据类型的指针.相反,C#提供了引用和创建由垃圾收集器管理的对象的能力.这种设计与其他功能相结合,使C#成为比C或C ++更安全的语言. ...
- Swift 里 Array (一)内存结构
public struct Array<Element>: _DestructorSafeContainer { #if _runtime(_ObjC) @usableFromInline ...
- POJ 2545
#include<iostream> #include<stdio.h> #include<iomanip> #define MAXN 100000 using n ...
- docker学习实践之路[第五站]mysql镜像应用
拉取mysql镜像 docker pull mysql:5.6 #拉取mysql .6版本的镜像 运行mysql镜像 docker run --name some-mysql --restart=al ...
- Android 开发工具类 37_ ContactInfoProvider
Android 手机中的联系人信息保存在 data\data\com.android.providers.contacts\databases\contacts2.db 中.主要有 raw_cont ...
- CentOS安装PHP7+Nginx+MySQL
本文属于动手搭建PHP开发环境的一部分,更多点击链接查看. 本文以centos6为例.命令部分均省略sudo命令. 安装PHP 下载 http://cn2.php.net/distributions/ ...
- Sublime Text shift+ctrl 妙用
1 :按住shift+ctrl然后按←或→可快速选中一行中的某一部分,相当于双击鼠标选中. 当你想在代码末尾加注释的话,这个方法很好用 输入文字 -> 光标移到文字末尾 -> 按住shif ...