题目连接: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. 12-UIKit(View绘制、绘制曲线、绘制文字、贴图)

    目录: 1. View绘制 2. 绘制曲线 3. 绘制文字 4. 贴图 回到顶部 1. View绘制 1.1 做出自己的视图对象 TRCell : UITableViewCell : UIView U ...

  2. Arcgis镶嵌数据集java代码操作

    转自:http://www.cdtarena.com/javapx/201307/9105.html 镶嵌数据集结合了之前arcgis管理影像的栅格目录和栅格数据集,为解决海量影像管理提供了很好的方案 ...

  3. 【译】在ASP.NET中创建PDF-iTextSharp起步

    原文 [译]在ASP.NET中创建PDF-iTextSharp起步 .Net framework 中自身并不包含可以和pdf打交道的方法.所以,当你需要你的ASP.Net Web应用程序中包含创建或与 ...

  4. 海量数据存储之Key-Value存储简介

    Key-value存储简介 具备高可靠性及可扩展性的海量数据存储对互联网公司来说是一个巨大的挑战,传统的数据库往往很难满足该需求,并且很多时候对于特定的系统绝大部分的检索都是基于主键的的查询,在这种情 ...

  5. android Gallery滑动不流畅的解决

    import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; impo ...

  6. javascript笔记整理(回调、递归、内置顶层函数)

    1.回调函数(通过函数的指针来调用函数,把一个函数的指针做为另一个函数的参数,当调用这个参数的时候,这个函数就叫做回调函数) a.通过指针来调用 function aa(a,b,fun){ retur ...

  7. 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10

    关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...

  8. UPC 2959: Caoshen like math 这就是个水题

    http://acm.upc.edu.cn/problem.php?id=2959 这就是个水题,之所以要写这个题是感觉很有纪念意义 用力看就是盲……23333333333333333 这个题就是最小 ...

  9. duck

    http://bjdw.artgooo.com/event/rubber/duck.shtml

  10. check————身份证

    -- Access 不支持 Substring 查询,可以替换为 mid 查询. select 序号,姓名,身份证号,性别from 身份表where (len(身份证号)<>15 and ...