UVA 10655 Contemplation! Algebra
来源:互联网 发布:区域软件 编辑:程序博客网 时间:2024/04/30 04:31
题目链接:http://acm.hust.edu.cn/vjudge/problem/41740
题意:给出a+b和ab的值,n,求a^n+b^n。
思路:n可能很大,所以直接考虑矩阵快速幂。
( a^n + b^n )( a + b ) = a^(n+1) + b^(n+1) + ab( a^(n-1) + b^(n-1) )
a^(n+1) + b^(n+1) = ( a^n + b^n )( a + b ) - ab( a^(n-1) + b^(n-1) )
令f(i) = a^i + b^i
f(i+1) = (a+b)fi - ab*f(i-1)
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffffint p,q;int n;struct matrix{ LL a[2][2]; void init(LL A = 0,LL B = 0,LL C = 0,LL D = 0) { a[0][0] = A , a[0][1] = B , a[1][0] = C , a[1][1] = D; } void E() { a[0][0] = a[1][1] = 1; a[1][0] = a[0][1] = 0; }};matrix multi( matrix & tx , matrix & ty ){ matrix ans; ans.init(); rep( i , 0 , 1 ) rep( j , 0 , 1 ) rep( k , 0 , 1 ) ans.a[i][j] = ( ans.a[i][j] + tx.a[i][k] * ty.a[k][j] ) ; return ans;}LL solve(){ matrix ans,temp; temp.init(0,-q,1,p); ans.E(); if ( n == 0 ) return 2; if ( n == 1 ) return p; while( n ) { if ( n & 1 ) ans = multi( ans , temp ); temp = multi( temp , temp ); n >>= 1; } LL t = ans.a[0][0] * 2 + ans.a[1][0] * p; return t;}int main(){ while( scanf("%d %d",&p,&q) == 2 ) { if ( scanf("%d",&n) != 1 && p + q == 0 ) break; printf("%lld\n",solve()); } return 0;}
0 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 (矩阵快速幂)
- 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(矩阵快速幂)
- uva 10655 Contemplation! Algebra(矩阵快速幂)
- uva 10655 - Contemplation! Algebra 【矩阵快速幂】【最坑的输入】
- 禀父母·谨守父亲保身之则
- 软件性能测试理论手札(二)
- vbox安装ubuntu 16.04时出现"busybox-initramfs"
- (三)使用swftools将pdf转换为swf
- Spring学习笔记(一)----IoC之DI
- UVA 10655 Contemplation! Algebra
- (4.6.17.7)进程保活(四:5.0以上):-native保活5.0以上方案推演过程以及代码详述
- mysql忘记登录密码需要注意的地方
- POJ1003:Hangover
- maven 项目引入jar包
- 精卫填海
- 驱动编写
- 函数指针和函数对象的比较
- 单调队列