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# 视频播放
发表于: 2003-10-15 20:39:21 搞定了,嘿嘿!首先非常感谢zoujiaming在邮件中给我指了条路:用C#调用API搞定!!!使用的是mciSendString API函数主要参考了 ...
- c#基础 里氏转换
1.里氏转换1).子类可以赋值给父类2).如果父类中装的是子类对象,那么可以讲这个父类强转为子类对象. 2.子类对象可以调用父类中的成员,但是父类对象永远都只能调用自己的成员. //// 1.里氏转换 ...
- MoinMoin install in apache (win)
一:下载环境 xampp:http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.8.1/xampp-win32-1.8.1-VC9 ...
- java类及编写public类的基础点
1.一个java文件中只能有一个public类.且公共类名称必须与java文件名一致,否则会出现错误提示.与其他面向对象编程语言的一样,在利用java分析问题时,基本思路即为将问题的属性(静)与行为( ...
- zblog去除底部版权信息 “请勿修改或删除主题版权及作者信息”
场景:使用了免费模板,但底部带作者版权.删除版权信息的代码后访问前台弹窗:请勿修改或删除主题版权及作者信息... 1. 删除版权信息代码 使用notepad++搜索功能,搜索版权信息:如ABC,找到相 ...
- iOS 应用架构 (一)
摘要:iOS 客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答 iOS 应用架构中的种种问题,本文是其中的第一篇,主要讲架构设计的通识和方法论等,同时还讨论了大家关心 ...
- python中__file__
用__file__ 来获得脚本所在的路径,比如文件在/root下 cat tee #!/usr/bin/env pythonprint __file__ #得到相对路径tee ...
- 转:error LNK2005: ...already defined in MSVCRTD.lib
编译错误: Error1error LNK2005: _CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.l ...
- CentOS6.4安装JDK,卸载自带的OpenJDK
1.查看OpenJDK的安装包 $ rpm -qa |grep java java-1.6.0-openjdk-1.6.0.0-1.62.1.11.11.90.el6_4.x86_64 java-1. ...
- Java 变量及基本数据类型
1.Java变量 1.1 变量的概念 内存中开辟的一块存储空间,用于存放运算过程中需要用到的数据: 该区域有自己的名称(变量名)和类型(数据类型): 该区域的数据可以在同一类型范围内不断变化: 1) ...