题目大意:给出a+b的值和ab的值,求a^n+b^n的值。

题目分析:有种错误的方法是这样的:利用已知的两个方程联立,求解出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)。

                      由此构造2x2的矩阵:matrix[1][1]=A,matrix[1][2]=-B;

matrix[2][1]=1,matrix[2][2]=0;

n=1或n=0时,答案显然。

要注意:数据中可能会出现A=B=0的情况,此时a=b=0,所以不能简单的判定当A=B=0时就结束输入数据。

代码如下:

 # include<iostream>
# include<cstdio>
# include<cmath>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long
struct matrix
{
int r,c;
LL m[][];
matrix(int _r,int _c):r(_r),c(_c){}
};
void init(matrix &m,int a,int b)
{
m.m[][]=a,m.m[][]=-b;
m.m[][]=,m.m[][]=;
}
matrix multiply(matrix a,matrix b)
{
matrix m(a.r,b.c);
for(int i=;i<=m.r;++i){
for(int j=;j<=m.c;++j){
m.m[i][j]=;
for(int k=;k<=a.c;++k)
m.m[i][j]+=a.m[i][k]*b.m[k][j];
}
}
return m;
}
matrix matrix_pow(matrix m,int n)
{
if(n==){
m.m[][]=m.m[][]=;
m.m[][]=m.m[][]=;
return m;
}
if(n==)
return m;
matrix res=matrix_pow(m,n/);
res=multiply(res,res);
if(n&)
res=multiply(res,m);
return res;
}
int main()
{
LL a,b,n;
while(scanf("%lld%lld%lld",&a,&b,&n)==)
{
if(n==){
printf("2\n");
continue;
}
if(n==){
printf("%d\n",a);
continue;
}
matrix mat(,);
init(mat,a,b);
mat=matrix_pow(mat,n-);
matrix ans(,);
ans.m[][]=a*a-*b;
ans.m[][]=a;
ans=multiply(mat,ans);
printf("%lld\n",ans.m[][]);
}
return ;
}

UVA-10655 Contemplation! Algebra (矩阵)的更多相关文章

  1. uva 10655 - Contemplation! Algebra(矩阵高速幂)

    题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...

  2. UVa 10655 Contemplation! Algebra 矩阵快速幂

    题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...

  3. 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 ...

  4. Contemplation! Algebra(矩阵快速幂,uva10655)

    Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...

  5. 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 ...

  6. 【UVA10655】 Contemplation! Algebra

    题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) ...

  7. UVa 10655 n次方之和(矩阵快速幂)

    https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: ...

  8. UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...

  9. UVA 11551 - Experienced Endeavour(矩阵高速幂)

    UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...

随机推荐

  1. EXKMP

    (我和lesphere,reverse研究了这个东西一上午)QAQ kmp是求字符串S的任意前缀与字符串T的最长的相同的前缀和后缀 exkmp第求字符串S的任意后缀与字符串T的最长公共前缀 与kmp相 ...

  2. c++不自动生成相关函数比如赋值、拷贝函数

    默认情况下,如果没有明确声明某些函数比如赋值.拷贝函数,c++会自动生成这些函数,通常他们是对成员进行by-value拷贝,有些时候,赋值.拷贝对象并无什么意义或者不合理,比如对于socket或者th ...

  3. 使用PopupWindow弹窗提醒

    一.新建view.xml 注意里面的控件要一个一个的定义离上一个控件的距离,即margin_top,不然最后的效果是紧缩的 二.在java中定义两个变量 1.View view=null: 2.pop ...

  4. 【ssh免登录】设置集群环境ssh免登录步骤

    1.每台机器都需要执行,生成自己的密钥 # ssh-keygen -t rsa 过程中遇到选项,全部enter #cd ~/.ssh # cat id_rsa.pub > authorized_ ...

  5. BZOJ5281: [Usaco2018 Open]Talent Show 01分数规划+01背包

    Description FarmerJohn要带着他的N头奶牛,方便起见编号为1…N,到农业展览会上去,参加每年的达牛秀!他的第i头奶牛重 量为wi,才艺水平为ti,两者都是整数.在到达时,Farme ...

  6. gulp常用命令

    gulp 默认的执行的命名文件为gulpfile 换成其他命名就识别不了 因为需要安装两次gulp或者说其他插件,一个是全局-g安装一个是本地目录安装, 本地目录安装时目录移动或者名字被改变就会失效提 ...

  7. Unity3D学习笔记(二):个体层次、绝对和局部坐标、V3平移旋转

    Directional Light:平行光源/方向性光源,用来模拟太阳光(角度只与旋转角度有关,与位置无关) Point Light:点光源,用来模拟灯泡,向四周发散光源 Spotlight:锥光源/ ...

  8. C#学习笔记(三):逻辑关系运算符和if语句

    条件语句 分支语句和循环语句是程序里最重要的逻辑. IF语句.分支语句.循环语句 using System; using System.Collections.Generic; using Syste ...

  9. NOI 8785 装箱问题(0-1背包)

    http://noi.openjudge.cn/ch0206/8785/ 描述 有一个箱子容量为V(正整数,0<=v<=20000),同时有n个物品(0< n<n<=30 ...

  10. C# ashx接收ContentType="text/xml"类型值

    public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain&qu ...