题目连接:uva 10655 - Contemplation! Algebra

题目大意:输入非负整数,p。q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数。

解题思路:定义f(n)=an+bn,则有f(n)∗(a+b)=(an+bn)∗(a+b)=an+1+abn+ban+bn+1=f(n+1)+abf(n−1),
所以f(n+1)=(a+b)f(n)−abf(n−1),用矩阵高速幂求解。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxsize = 100;
typedef long long ll;
typedef long long type; struct Mat {
int r, l;
type arr[maxsize][maxsize]; Mat (int r = 0, int l = 0) {
set(r, l);
memset(arr, 0, sizeof(arr));
} void set (int r, int l) {
this->r = r;
this->l = l;
} Mat operator * (const Mat& u) {
Mat ret(r, u.l);
for (int k = 0; k < l; k++) {
for (int i = 0; i < r; i++)
for (int j = 0; j < u.l; j++)
ret.arr[i][j] = (ret.arr[i][j] + arr[i][k] * u.arr[k][j]);
}
return ret;
}
}; void put (Mat x) {
for (int i = 0; i < x.r; i++) {
for (int j = 0; j < x.l; j++)
printf("%lld ", x.arr[i][j]);
printf("\n");
}
} Mat pow_mat (Mat ans, Mat x, ll n) { while (n) {
if (n&1)
ans = x * ans;
x = x * x;
n >>= 1;
}
return ans;
} int main () {
ll p, q, n;
while (scanf("%lld%lld%lld", &p, &q, &n) == 3 && p + q + n) {
Mat x(2, 2);
x.arr[0][1] = 1;
x.arr[1][0] = -q;
x.arr[1][1] = p; Mat ans(2, 1);
ans.arr[0][0] = 2;
ans.arr[1][0] = p; if (n > 1) {
ans = pow_mat(ans, x, n-1);
printf("%lld\n", ans.arr[1][0]);
} else
printf("%lld\n", ans.arr[n][0]);
}
return 0;
}

uva 10655 - Contemplation! Algebra(矩阵高速幂)的更多相关文章

  1. UVa 10655 Contemplation! Algebra 矩阵快速幂

    题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...

  2. UVA 11551 - Experienced Endeavour(矩阵高速幂)

    UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...

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

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

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

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

  6. uva 11885 - Number of Battlefields(矩阵高速幂)

    题目连接:uva 11885 - Number of Battlefields 题目大意:给出周长p,问多少种形状的周长为p的,而且该图形的最小包围矩阵的周长也是p,不包含矩形. 解题思路:矩阵高速幂 ...

  7. UVA10518 - How Many Calls?(矩阵高速幂)

    UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...

  8. HDU2842-Chinese Rings(递推+矩阵高速幂)

    pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...

  9. HDU2276 - Kiki &amp; Little Kiki 2(矩阵高速幂)

    pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...

随机推荐

  1. SuperSocket源码解析之配置系统

    一 继承Net配置系统 Net应用程序配置机制跟程序集引用大致类似,均具有继承性,如iis几乎每个应用程序都会有一个Web.config,比如我们使用vs2012以上版本创建一个web应用程序会自带一 ...

  2. docker学习笔记2:容器操作

    一.列出主机上已经创建的容器 docker ps -a 二.创建交互式容器 命令: docker run -i -t ubuntu /bin/bash 其中-i -t 表示创建一个提供交互式shell ...

  3. Jquery progressbar通过Ajax请求获取后台进度演示

    项目源代码下载:http://download.csdn.net/detail/nuptboyzhb/6262253 1.简介 本文主要演示Jquery progressbar的进度条功能.js通过a ...

  4. Swift Error fatal error: unexpectedly found nil while unwrapping an Optional value

    致命的错误: 对一个为空的optional值进行解包. 强制解包(forced unwrapping): 在optional后面加叹号. 比如  "value!" 错误代码: le ...

  5. 深入理解 Spring 事务原理【转】

    本文转自码农网 – 吴极心原创  连接地址:http://www.codeceo.com/article/spring-transactions.html 一.事务的基本原理 Spring事务的本质其 ...

  6. Windows NT 技术简介

    Windows NT 技术简介 NT:New Technoly(新技术,因比DOS.WIN9X采用了很多新技术而得名) Windows NT基本介绍 WindowsNT是Microsoft推出的面向工 ...

  7. 列举一些常见的Python HTTP服务器

    要使 Python 写的程序能在 Web 上被访问,还需要搭建一个支持 Python 的 HTTP 服务器.下面列举一些常见的 Python HTTP 服务器,以及它们目前的大致发展情况,以便用户的对 ...

  8. 基于visual Studio2013解决面试题之0808寻找中间数

     题目

  9. IT大数据服务管理高级课程(IT服务,大数据,云计算,智能城市)

    个人简历 金石先生是马克思主义中国化的研究学者,上海财经大学经济学和管理学硕士,中国民主建国会成员,中国特色社会主义人文科技管理哲学的理论奠基人之一.金石先生博学多才,对问题有独到见解.专于工作且乐于 ...

  10. 用angularjs开发下一代web应用(二):angularjs应用骨架(二)

    1.浅谈非入侵式JavaScript <div ng-click="doSomething()">...</div>这些指令和原来的事件处理器有下面不同之处 ...