Contemplation! Algebra(矩阵快速幂,uva10655)
来源:互联网 发布:淘宝客服售中工作内容 编辑:程序博客网 时间:2024/06/05 15:13
Problem E
Contemplation! Algebra
Input: Standard Input
Output: Standard Output
Time Limit: 1 Second
Given the value of a+b and ab you will have to find the value of an+bn
Input
The input file contains several lines of inputs. Each line except the last line contains 3 non-negative integers p, q and n. Here p denotes the value of a+b andq denotes the value of ab. Input is terminated by a line containing only two zeroes. This line should not be processed. Each number in the input file fits in a signed 32-bit integer. There will be no such input so that you have to find the value of 00.
Output
For each line of input except the last one produce one line of output. This line contains the value of an+bn. You can always assume that an+bn fits in a signed 64-bit integer.
Sample Input Output for Sample Input
10 16 2
7 12 3
0 0
68
91
题意:已知p=a+b;q=a*b;求a^n+b^n=ans? 注意a,b是实数哦!还有题目说不用考虑0^0这种情况!
那么分析一下:n = 0 , ans0 = 2 ;
n = 1 , ans1 = a + b = p ;
n = 2 , ans2 = p * p - 2 * q = ans1 * p - ans0 * q;
同理 n = 3 , ans3 = ans2 * p - ans1 * q;
n = 4 , .......
..................
所以矩阵为
p
1
-q
0
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1596
转载请注明出处:寻找&星空の孩子
对了这个题还有一点比较坑爹;就是最后那组测试数据。。。不解释我错了5次。。。
#include<cstring>//用c++的输入就过了。#include<cstdio>#include<iostream>using namespace std;#define LL long longstruct matrix{ LL mat[2][2];};matrix multiply(matrix a,matrix b){ matrix c; memset(c.mat,0,sizeof(c.mat)); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { if(a.mat[i][j]==0)continue; for(int k=0;k<2;k++) { if(b.mat[j][k]==0)continue; c.mat[i][k]+=a.mat[i][j]*b.mat[j][k]; } } } return c;}matrix quickmod(matrix a,LL m){ matrix res; for(int i=0;i<2;i++) for(int j=0;j<2;j++) res.mat[i][j]=(i==j); while(m) { if(m&1) res=multiply(res,a); m>>=1; a=multiply(a,a); } return res;}int main(){ LL p,q,n; // while(scanf("%lld%lld%lld",&p,&q,&n),p+q+n) while(cin>>p>>q>>n) { // if(!n&&(!p||!q)) break; // scanf("%lld",&n); if(n==0)printf("2\n");//cout<<2<<endl;// else if(n==1)printf("%lld\n",p);// cout<<p<<endl;// else if(n==2) printf("%lld\n",p*p-2*q);//cout<<p*p-2*q<<endl;// else { //初始矩阵(p*p-2*q,p) matrix ans; ans.mat[0][0]=p; ans.mat[0][1]=1; ans.mat[1][0]=-q; ans.mat[1][1]=0; ans=quickmod(ans,n-1); LL ant=p*ans.mat[0][0]+2*ans.mat[1][0]; // cout<<ant<<endl; printf("%lld\n",ant);// printf("%lld\n",(p*ans.mat[0][0]+2*ans.mat[1][0])); } } return 0;}
加油,少年!!!
0 0
- Contemplation! Algebra(矩阵快速幂,uva10655)
- Uva10655 Contemplation! Algebra矩阵快速幂
- UVA10655-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
- 矩阵快速幂uva10655
- uva10655(矩阵快速幂)
- uva 10655 - Contemplation! Algebra 【矩阵快速幂】【最坑的输入】
- UVA10655矩阵快速幂(构造矩阵)
- UVA 10655 Contemplation! Algebra(矩阵乘法)
- C#窗体拖动
- 常系数线性递推的第n项及前n项和 (Fibonacci数列,矩阵)
- Fibonacci(矩阵)
- M斐波那契数列(矩阵快速幂+费马小定理)
- Another kind of Fibonacci(矩阵)
- Contemplation! Algebra(矩阵快速幂,uva10655)
- 233 Matrix(hdu5015 矩阵)
- Training little cats(poj3735,矩阵快速幂)
- Power of Matrix(uva11149+矩阵快速幂)
- Bell(矩阵快速幂+中国剩余定理)
- Biorhythms(poj1006+中国剩余定理)
- Gold Rush(hnu13249)
- Java中调用动态库dll的方法
- String和StringBuffer的区别