UVA 10655 - Contemplation! Algebra(矩阵快速幂)
来源:互联网 发布:万德数据库打不开 编辑:程序博客网 时间:2024/05/16 18:55
UVA 10655 - Contemplation! Algebra
题目链接
题意:给定p, q, n代表p=a+b,q=ab求an+bn
思路:矩阵快速幂,公式变换一下得到(an+bn)(a+b)=an+1+bn+1+ab(an−1+bn−1),移项一下得到an+1+bn+1=(an+bn)p−q(an−1+bn−1)
这样就可以用矩阵快速幂求解了
代码:
#include <stdio.h>#include <string.h>long long p, q, n;struct mat {long long v[2][2];mat() {memset(v, 0, sizeof(v));}mat operator * (mat c) {mat ans;for (int i = 0; i < 2; i++) {for (int j = 0; j < 2; j++) {for (int k = 0; k < 2; k++) {ans.v[i][j] = ans.v[i][j] + v[i][k] * c.v[k][j]; } } } return ans; }};mat pow_mod(mat x, long long k) {mat ans;ans.v[0][0] = ans.v[1][1] = 1;while (k) {if (k&1) ans = ans * x;x = x * x;k >>= 1; } return ans;}long long solve() {if (n == 0) return 2;if (n == 1) return p;if (n == 2) return p * p - 2 * q;mat a;a.v[0][0] = p; a.v[0][1] = -q; a.v[1][0] = 1;a = pow_mod(a, n - 2);return a.v[0][1] * p + a.v[0][0] * (p * p - 2 * q);}int main() {while (scanf("%lld%lld%lld", &p, &q, &n) == 3) {printf("%lld\n", solve());}return 0;}
1 0
- uva 10655 Contemplation! Algebra 矩阵快速幂
- UVA 10655 - Contemplation! Algebra(矩阵快速幂)
- uva 10655 - Contemplation! Algebra(矩阵快速幂)
- UVA-10655 Contemplation! Algebra (矩阵快速幂)
- UVA 10655 Contemplation! Algebra(矩阵快速幂)
- UVA 10655 Contemplation! Algebra (矩阵快速幂)
- UVA 10655 Contemplation! Algebra(构造矩阵和快速幂)
- uva 10655 Contemplation! Algebra(矩阵快速幂)
- uva 10655 Contemplation! Algebra(矩阵快速幂)
- 【矩阵快速幂+输入终止条件判断】Contemplation! Algebra UVA
- uva 10655 - Contemplation! Algebra 【矩阵快速幂】【最坑的输入】
- UVA 10655 Contemplation! Algebra(矩阵乘法)
- UVA 10655 Contemplation! Algebra(构造矩阵)
- UVA 10655 Contemplation! Algebra(矩阵乘法)
- Contemplation! Algebra(矩阵快速幂,uva10655)
- Uva10655 Contemplation! Algebra矩阵快速幂
- UVA 10655 Contemplation! Algebra
- UVA 10655 Contemplation! Algebra
- Android MD5校验码的生成与算法实现
- cocos2d-x像素级触摸处理
- php(date()函数)
- 南京邮电大学OJ——1150整除的尾数
- Hibernate中query接口
- UVA 10655 - Contemplation! Algebra(矩阵快速幂)
- 修改Android工程的名称(非Eclipse重命名)
- 串行口应用
- Python 运行时捕获 Warning
- Lucene索引的帮助类
- java正则表达式学习
- hdu 1418 抱歉
- 学习OpenCV之CV篇
- cocos2d-x lua 学习笔记