题意:

给出\(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 矩阵快速幂的更多相关文章

  1. uva 10655 - Contemplation! Algebra(矩阵高速幂)

    题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...

  2. Contemplation! Algebra(矩阵快速幂,uva10655)

    Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...

  3. 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 ...

  4. UVA - 10870 Recurrences 【矩阵快速幂】

    题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...

  5. UVA - 10229 Modular Fibonacci 矩阵快速幂

                                 Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...

  6. 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 ...

  7. 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) ...

  8. uva 10518 - How Many Calls?(矩阵快速幂)

    题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...

  9. 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 ...

随机推荐

  1. 开源分布式Job系统,调度与业务分离-如何创建周期性的HttpJob任务

    项目介绍: Hangfire:是一个开源的job调度系统,支持分布式JOB!! Hangfire.HttpJob 是我针对Hangfire开发的一个组件,该组件和Hangfire本身是独立的.可以独立 ...

  2. 常见的JedisConnectionException 异常

    最近在使用redis出现以下的异常: 1.redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectExcept ...

  3. js页面可视区域懒加载

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 今天测试发现qwebsocket有个bug

    发现命令使用时间久了就会丢失mask,mask设置成0,而websocket协议要求客户端给服务器传东西必须带mask=1,发现确实有个bug,代码如下: 文件:src/websockets/qweb ...

  5. 为什么HDFS的副本数通常选择3?

    HDFS采用一种称为机架感知的策略来改进数据的可靠性.可用性和网络带宽的利用率. 在大多数情况下,HDFS的副本系数是3,HDFS的存放策略是一个副本存放在本地机架节点上,另一个副本存放在同一机架的另 ...

  6. GoAccess自动分割Nginx日志

    GoAccess 是一款开源的网站日志实时分析工具.GoAccess 的工作方式很容易理解,就是读取和解析 Apache/Nginx/Lighttpd 的访问日志文件 access log,然后以更友 ...

  7. mysql-练级查询

    mysql的链接查询中主要有五大类链接查询 1.内连接查询 1.1:等值链接查询:指使用等号"="比较两个表的连接列的值,相当于两表执行笛卡尔后,取两表连结列值相等的记录. SEL ...

  8. python 基础之格式化输出

    字符占位符%s #_cvvh:"chenxi" #date: 2019/6/24 print ('chhjg') # 格式化输出 name = input("Name:& ...

  9. 2004: C语言实验——数日子(数组)

    2004: C语言实验——数日子 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 213  Solved: 111[Submit][Status][Web ...

  10. SD Card Formatter for Mac Download

    https://www.sdcard.org/downloads/formatter_4/eula_mac/ SDFormatter Mac版是一款Mac OS平台上的sd卡修复工具,SDFormat ...