uva 10655 - Contemplation! Algebra(矩阵高速幂)
题目连接: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(矩阵高速幂)的更多相关文章
- UVa 10655 Contemplation! Algebra 矩阵快速幂
题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
- 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 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 ...
- uva 11885 - Number of Battlefields(矩阵高速幂)
题目连接:uva 11885 - Number of Battlefields 题目大意:给出周长p,问多少种形状的周长为p的,而且该图形的最小包围矩阵的周长也是p,不包含矩形. 解题思路:矩阵高速幂 ...
- UVA10518 - How Many Calls?(矩阵高速幂)
UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...
- HDU2842-Chinese Rings(递推+矩阵高速幂)
pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...
- HDU2276 - Kiki & Little Kiki 2(矩阵高速幂)
pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...
随机推荐
- MAMP:在 OSX 中搭建 Apache, MySQL, PHP 环境并本地安装、调试 WordPress
MAMP 这个名字来源于 Macintosh Apache MySQL PHP,显然专门用来在 Mac 环境下搭建 Apache.MySQL.PHP 平台. 虽然 OSX 中已经预装了 Apache ...
- PHP函数详细剖析之rtrim函数 By ACReaper
string rtrim ( string $str [, string $charlist ] ) 这个函数很好理解.r表示右边.trim表示修剪.即右边修剪.默认修剪字符str右边的字符.默认修剪 ...
- c语言,指针与数组--指针与二维数组2
指向一维数组的指针 char (*p)[10] ;指向一维数组的指针类型 typedef char(*TYPE_P2ARRAY)[10] ; 该指针可以指向数组 ,且使用起来效果节本相同, ...
- Microsoft Win32 Programmer's Reference.chm
实在是太棒了,感谢这位网友: http://download.csdn.net/detail/tgyd6800/9632351
- android 打包 /${zipalign}" error=2, No such file or directory
当我更新完android L proview之后我的打包出问题了,报错/${zipalign}" error=2, No such file or directory 排查了一下午才知道 近 ...
- 程序实现LayoutAnimationController
在res/anim下新建anim_set.xml: <?xml version="1.0" encoding="utf-8"?> <set x ...
- Apache 服务器
1.介绍 Apache原来用于小型或试验性Internet网络,后来逐步扩展到各种系统中,对Linux的支持几乎完美.Apache可以支持SSL技术,支持多台虚拟主机.Apache是以进程为基础的结构 ...
- Python写入文件,但是发现文件为空,竟然未写入!
问题描述: fw=open(r'C:\test.txt','w') s="Hello World!" fw.write(s) ========== 此时查看C盘根目录,发现test ...
- OpenCV中遇到Microsoft C++ 异常 cv::Exception
我在实现<OpenCV2计算机视觉编程手册>第2章 2.2 节 存取像素值 中的椒盐噪声例子中遇到的程序错误. 原始输入程序: #include <opencv2/core/core ...
- android布局margin和padding差异!
事实上从使用的时候就能够差别开来. android:padding android:layout_margin padding是在本控件级别的,而margin是在layout级别的. 最好拿有背景的控 ...