UVA10655-Contemplation! Algebra
来源:互联网 发布:淘宝鹊桥软件 编辑:程序博客网 时间:2024/06/14 13:54
![](file:///C:\Users\John\AppData\Roaming\Tencent\Users\664607530\QQ\WinTemp\RichOle\B%_MJHFFLTCY70V4WTK`JBO.png)
![](file:///C:\Users\John\AppData\Roaming\Tencent\Users\664607530\QQ\WinTemp\RichOle\B%_MJHFFLTCY70V4WTK`JBO.png)
题目分析:有种错误的方法是这样的:利用已知的两个方程联立,求解出a和b,进而求出答案。这种方法之所以错,是因为这种方法有局限性。联立之后会得到一个二元一次方程,只有当该方程有实数解确切的说是当某个数据满足该方程有实数解时,这种方法得到的结果才有可能正确。显然,题中数据不可能这么片面。正确的方法是这样的:
令a+b=A,ab=B,S(n)=an+bn。则S(n)=an+bn=(a+b)(an-1+bn-1)-abn-1-an-1b=(a+b)(an-1+bn-1)-ab(an-2+bn-2)=A*S(n-1)-B*S(n-2)(n≥2)。
n=1或n=0时,答案显然。
要注意:数据中可能会出现A=B=0的情况,此时a=b=0,所以不能简单的判定当A=B=0时就结束输入数据。
所以输入要用c++,或者while(scanf("%d %d %d",&x,&y,&n)==3)
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;#define ll long longstruct Matrix{ ll v[3][3]; Matrix() { memset(v,0,sizeof v); }}m,k,t;Matrix mul(Matrix a,Matrix b){ Matrix sum; for(int i=1; i<3; i++) for(int j=1; j<3; j++) for(int k=1; k<3; k++) sum.v[i][j]+=a.v[i][k]*b.v[k][j]; return sum;}Matrix pow(Matrix a,ll n){ Matrix sum=k; while(n) { if(n&1) sum=mul(sum,a); n>>=1; a=mul(a,a); } return sum;}int main(){ ll x,y,n; while(cin>>x>>y>>n) { if(n==0) {printf("2\n");continue;} if(n==1) {printf("%lld\n",x);continue;} k.v[1][1]=k.v[2][2]=1; m.v[1][1]=x;m.v[1][2]=1;m.v[2][1]=-y; t=pow(m,n-1); printf("%lld\n",x*t.v[1][1]+2*t.v[2][1]); } return 0;}
0 0
- UVA10655-Contemplation! Algebra
- Contemplation! Algebra(矩阵快速幂,uva10655)
- Uva10655 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(构造矩阵)
- UVA 10655 Contemplation! Algebra(矩阵乘法)
- UVA 10655 Contemplation! Algebra(矩阵快速幂)
- UVA 10655 Contemplation! Algebra (矩阵快速幂)
- UVA 10655 Contemplation! Algebra(构造矩阵和快速幂)
- Android Studio分享项目到github上
- JavaSE_04面向对象2
- c#索引器
- JavaSE_05面向对象3
- Google Play Services Location:接收位置更新
- UVA10655-Contemplation! Algebra
- select count(*) from sys_passport where register_time between '2016-07-21 00:00:00' and '2016-07-22
- php mongo 扩展
- eclipse注释模板
- 从缓冲上看阻塞与非阻塞socket在发送接收上的区别
- LeetCode Intersection of Two Arrays II
- loadrunner 插入文本检查点
- JavaSE_06异常处理
- 395. Longest Substring with At Least K Repeating Characters