UVA-10655 Contemplation! Algebra (矩阵)
题目大意:给出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 (矩阵)的更多相关文章
- uva 10655 - Contemplation! Algebra(矩阵高速幂)
题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...
- UVa 10655 Contemplation! Algebra 矩阵快速幂
题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...
- 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 ...
- Contemplation! Algebra(矩阵快速幂,uva10655)
Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...
- 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 ...
- 【UVA10655】 Contemplation! Algebra
题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) ...
- UVa 10655 n次方之和(矩阵快速幂)
https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: ...
- UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
随机推荐
- Python入门之面向对象的多态
本章目录: 一.多态 二.多态性 三.鸭子类型 ============================== 一.多态 多态指的是一类事物有多种形态. 动物有多种形态:人,狗,猪. import ab ...
- web应用下的安全问题以及tomcat/nginx对应解决方法(持续更新、亲测可解决问题)
最近一券商那边扫描反馈了下面几个非业务型安全漏洞,要求解决,如下: XSS 自己写个脚本response的时候对特殊字符进行了处理,或者网上搜下一堆(不要忘了回车.换行). HTML form wit ...
- 怎么说, 开发会很乐意去主动修改bug?
怎么说, 开发会很乐意去主动修改bug? 一图顶上千言万语,如下:
- jackson 常用注解,比如忽略某些属性,驼峰和下划线互转
一般情况下使用JSON只使用了java对象与字符串的转换,但是,开发APP时候,我们经常使用实体类来做转换:这样,就需要用到注解: Jackson默认是针对get方法来生成JSON字符串的,可以使用注 ...
- ubuntu14.04禁止触摸板和恢复触摸板
1.使用xinput list查看与触摸板相关的id,以下是本机的输出,没搞清楚为什么是Mouse!!! jello@jello:~$ xinput list⎡ Virtual core pointe ...
- P1471 方差
题目 luogu 思路 \[\frac{1}{n}*\sum_{1}^{n}( a_{i}-A)^{2}\] \[\frac{1}{n}*\sum_{1}^{n}( a_{i}^2-2*A*a_{i} ...
- 【第十四章】 springboot + profile(不同环境读取不同配置)
具体做法: 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中:prod环境下的配置配置在application-prod.prope ...
- BZOJ1407: [Noi2002]Savage exgcd
Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, Li表示每个野人所住的初始洞穴编号,每年走过的洞穴 ...
- css布局一屏幕的自适应高度
css ;;list-style: none;} .top{height: 100px;background-color:orange;} .max{;background-color:skyblue ...
- 【旧版本】Ubuntu 14.04 下 P416编译器 p4c的安装
注:此为2017年5月份的安装方法,最新的p4c安装方法见: Ubuntu14.04下 安装p4c 参考: p4c README Ubuntu 14.04 下 P4v16编译器 p4c的安装 系统要求 ...