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 ...
随机推荐
- C# 实现本地化日志管理
1.新建一个类库解决方案 CommnoLog 2.新建两个文件夹 2.1FileUtil.cs 代码如下 public static class FileUtil { /// <summary ...
- java中循环的不同终止方式
1.break:直接强行跳出当前循环,不再执行剩余代码.但在多重循环的情况下,若break在内层循环中,则仅仅终止了内层循环,外循环照常执行. 2.continue:仅仅终止此次循环. 3.retur ...
- 《超实用的Node.js代码段》连载三:Node.js深受欢迎的六大原因
<超实用的Node.js代码段>连载一:获取Buffer对象字节长度 <超实用的Node.js代码段>连载二:正确拼接Buffer Node.js是一种后起的优秀服务器编程语言 ...
- 如何在github中的readme.md加入项目截图
1. 先在之前的本地项目文件夹里创建一个存放截图的文件夹.(如img文件夹) 2. 将新增的内容通过github desktop上传到github中 3. 在github中立马能看到刚刚上传的图片,打 ...
- Oracle创建用户、表(1)
Oracle创建用户.表(1) 1. 连接 C:\Users\LEI>sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on ...
- JMeter进行压力测试
一.jmeter的安装 1.从 http://jmeter.apache.org/download_jmeter.cgi 下载jmeter(图1正中间的apache-jmeter-2.13.tg ...
- javascript日期函数
时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.除开JavaScript中的时间对象外,在VbScript中也有许多的时间对象,而且非常好用.下面还是按照我们 ...
- stixel 理解
在车辆所处平面建立极坐标占位网格(polar occupancy grid),将视差图所代表的三维世界(3D world) 正交投影到该平面中. occupancy:每个网格被赋予一个占位数,代表了该 ...
- mac层到ath9k层,ath9k层到硬件层
如上图,整个 mac 层分成两个部分——UMAC 和 LMAC.LMAC 分成 MAC 下半部分和硬件抽象层. 硬件抽象层和ath9k层的连接 在hw.h中的函数struct ath_hw_ops() ...
- bash编程之循环控制:
bash编程之循环控制: for varName in LIST; do 循环体 done while CONDITION; do 循环体 done until CONDITION; do 循 ...