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 ...
随机推荐
- Git-管理和撤销修改
一.管理修改 为什么说Git管理的是修改,而不是文件呢?我们还是做实验.第一步,对readme.txt做一个修改,比如加一行内容: Git is a distributed version contr ...
- drf-序列化器的理解
序列化器作用: 1.进行数据的校验 2.对数据对象进行转换 序列化: 模型类对象 -----> python字典 用于输出, 返回给前端使用 反序列化: 前端传送的数据 --- ...
- ss的使用配置(电脑、手机FQ)
注:FQ仅用于google查阅资料等,禁止违规违法行为 自己搭建ss服务 1.vps购买:https://www.alpharacks.com/holiday 按流程填完相关信息(Operating ...
- maven build的常用生命周期
常用的maven build goals: validate - validate the project is correct and all necessary information is av ...
- POJ 2661
#include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...
- Windows 安装Rabbitmq
Rabbitmq是基于erlang开发的消息队列,客户端支持主流的开发语言(java.C#.Python等). 环境:windows server 2012(x64) 1.下载安装 http://ww ...
- gvim配置相关
用 vundle 来管理 vim 插件(包含配置文件vimrc和gvimrc) gvim插件管理神器:vundle的安装与使用 Vim插件管理Vundle Linux 下VIM的配置 Vim配置系列( ...
- android的电话监听
android的电话监听 新建一个项目,结构图如下: PhoneService: package com.demo.tingdianhua; import android.app.Service; i ...
- Linux下升级Python到3.5.2版本
原文出处:https://www.cnblogs.com/tssc/p/7762998.html 本文主要介绍在Linux(CentOS)下将Python的版本升级为3.5.2的方法 众所周知,在20 ...
- Java 调用系统命令
ProcessBuilder import java.io.File; import java.io.IOException; import java.io.InputStream; import j ...