UVa 10655 Contemplation! Algebra 矩阵快速幂
题意:
给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\)。
分析:
这种题目关键还是在于构造矩阵:
$\begin{bmatrix}
0 & 1 \
-(a+b) & ab
\end{bmatrix}
\begin{bmatrix}
a{n-1}+b{n-1}\
an+bn
\end{bmatrix}
\begin{bmatrix}
an+bn\
a{n+1}+b{n+1}
\end{bmatrix}$
注意不要遇到\(p,q\)都为\(0\)时就退出,因为测试数据中是有这种情况的。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
struct Matrix
{
LL a[2][2];
Matrix() { memset(a, 0, sizeof(a)); }
Matrix operator * (const Matrix& t) const {
Matrix ans;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
ans.a[i][j] += a[i][k] * t.a[k][j];
return ans;
}
};
Matrix Pow(Matrix a, LL p) {
Matrix ans;
ans.a[0][0] = ans.a[1][1] = 1;
while(p) {
if(p & 1) ans = ans * a;
a = a * a;
p >>= 1;
}
return ans;
}
LL p, q, n;
int main()
{
while(scanf("%lld%lld", &p, &q) == 2) {
if(p == 0 && q == 0) break;
scanf("%lld", &n);
if(n == 0) { printf("2\n"); continue; }
Matrix M;
M.a[0][1] = 1;
M.a[1][0] = -q;
M.a[1][1] = p;
M = Pow(M, n - 1);
LL ans = M.a[1][0] * 2 + M.a[1][1] * p;
printf("%lld\n", ans);
}
return 0;
}
UVa 10655 Contemplation! Algebra 矩阵快速幂的更多相关文章
- uva 10655 - Contemplation! Algebra(矩阵高速幂)
题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...
- Contemplation! Algebra(矩阵快速幂,uva10655)
Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...
- Contemplation! Algebra 矩阵快速幂
Given the value of a+b and ab you will have to find the value of a n + b n Input The input file cont ...
- UVA - 10870 Recurrences 【矩阵快速幂】
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...
- UVA - 10229 Modular Fibonacci 矩阵快速幂
Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...
- uva 10655 - Contemplation! Algebra
---恢复内容开始--- Given the value of a+b and ab you will have to find the value of an+bn 给出a+b和a*b的值,再给出n ...
- POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...
- uva 10518 - How Many Calls?(矩阵快速幂)
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...
- Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)
题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3); 求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...
随机推荐
- ribbon重试机制
我们使用Spring Cloud Ribbon实现客户端负载均衡的时候,通常都会利用@LoadBalanced来让RestTemplate具备客户端负载功能,从而实现面向服务名的接口访问. 下面的例子 ...
- 9、数值的整数次方------------>剑指offer系列
数值的整数次方 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 思路 这道题逻辑上很简单,但很容易出错 关键是要考虑全面,考虑到所有情况 ...
- Kendo UI 初始化 Data 属性
初始化 Data 属性 前面在介绍准备 Kendo UI 开发环境时我们使用 jQuery 的方法将一个 HTML 元素转换成一个 Kendo UI 控制项: $(“#datepicker”).ke ...
- 织梦dedecms手机版上下篇链接错误的解决方法
打开 \include\arc.archives.class.php 1. 找到 $this->PreNext['pre'] = "上一篇:<a href='$mlink'> ...
- JFinal视频教程-JFnal学院分享课
最近JFinal学院出了JFinal视频教程分享课,请笑纳~ 课程列表: 1.[JFinal版]微信小程序富文本渲染解决方案-html2wxml4J分享课 这个课程主要讲的是使用基于JFinal开发的 ...
- Got error 28 from storage engine的错误处理
早上例行检查数据库,发现Got error 28 from storage engine这个错误,天那,我的数据.心里哇凉....备份的时间还是很久以前.最近更新了不少,麻烦大了. 好在找到了解决方法 ...
- java面试题(杨晓峰)---第五讲String、StringBuffer、StringBuilder有什么区别?
线程 字符 操作频繁度 1 String (1)String的创建机制 由于String在java世界中使用过于频繁,java为了避免在一个系统中产生大量重复的String对象,引入了字符串常量池,其 ...
- CentOS 软RAID5
yum install -y mdadm np1回车 tfdw mdadm --create /dev/md5 --level=5 --raid-devices=3 /dev/sdb1 /dev/sd ...
- 得到本地机器的IP地址
实现效果: 知识运用: DNS类的GetHostByName //获取指定DNS主机名的DNS信息 public static IPHostEntry GetHostByName (string ...
- appium---AndroidSdk安装
AndroidSDK指的是Android专属的软件开发工具包,被软件开发工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集合.Android又是采用java语言进行开发 ...