Contemplation! Algebra(矩阵快速幂,uva10655)
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 long struct matrix
{
LL mat[][];
}; matrix multiply(matrix a,matrix b)
{
matrix c;
memset(c.mat,,sizeof(c.mat));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(a.mat[i][j]==)continue;
for(int k=;k<;k++)
{
if(b.mat[j][k]==)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=;i<;i++)
for(int j=;j<;j++)
res.mat[i][j]=(i==j);
while(m)
{
if(m&) res=multiply(res,a);
m>>=;
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==)printf("2\n");//cout<<2<<endl;//
else if(n==)printf("%lld\n",p);// cout<<p<<endl;//
else if(n==) printf("%lld\n",p*p-*q);//cout<<p*p-2*q<<endl;//
else
{
//初始矩阵(p*p-2*q,p)
matrix ans;
ans.mat[][]=p;
ans.mat[][]=;
ans.mat[][]=-q;
ans.mat[][]=; ans=quickmod(ans,n-);
LL ant=p*ans.mat[][]+*ans.mat[][];
// cout<<ant<<endl;
printf("%lld\n",ant);
// printf("%lld\n",(p*ans.mat[0][0]+2*ans.mat[1][0]));
}
}
return ;
}
加油,少年!!!
Contemplation! Algebra(矩阵快速幂,uva10655)的更多相关文章
- 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 ... 
- UVa 10655 Contemplation! Algebra 矩阵快速幂
		题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ... 
- uva 10655 - Contemplation! Algebra(矩阵高速幂)
		题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ... 
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
		题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ... 
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
		非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ... 
- 51nod 1113 矩阵快速幂
		题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ... 
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
		还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ... 
- HDU5950(矩阵快速幂)
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ... 
- 51nod 1126 矩阵快速幂 水
		有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ... 
随机推荐
- .NET Entity Framework (with Oracle ODP.NET) -Code First
			上一篇文章介绍了.NET Entity Framework ,并演示了Model First模式,本文将继续讨论 Code First 模式的实现. 一.摘要 1.目标 本文验证了通过Oracle D ... 
- MVC 5使用ViewBag(对象)显示数据
			前面Insus.NET有演示使用ViewData来实现控制器与视图的通讯.如果想了解的话,可以从下面两个链接可以查看:<MVC 5使用ViewData(对象)显示数据>http://www ... 
- 【文文殿下】[AH2017/HNOI2017]礼物
			题解 二项式展开,然后暴力FFT就好了.会发现有一个卷积与c无关,我们找一个最小的项就行了. Tips:记得要倍长其中一个数组,防止FFT出锅 代码如下: #include<bits/stdc+ ... 
- jQuery基础(4)- 位置信息、事件流、事件对象、事件代理、jquery事件
			一.jQuery的位置信息 jQuery的位置信是JS的client系列.offset系列.scroll系列封装好的一些简便api. 1.宽度和高度 a.获取宽度和高度,例如: .width() // ... 
- 网络基础、ftp任务(进度条、计算文件大小、断点续传、搭建框架示例)
			一.网络基础 1.端口,是什么?为什么要有端口? 端口是为了将同一个电脑上的不同程序进行隔离. IP是找电脑:端口是找电脑上的应用程序: 端口范围:1 – 65535 : 1 - 1024 不要 ... 
- C#6.0语言规范(十三) 接口
			接口定义合同.实现接口的类或结构必须遵守其合同.接口可以从多个基接口继承,并且类或结构可以实现多个接口. 接口可以包含方法,属性,事件和索引器.接口本身不为它定义的成员提供实现.接口仅指定必须由实现接 ... 
- 【spring cloud】服务启动后正常,但是无法上线,一直处于down状态
			spring cloud eureka 如果出现某个应用实例 down(1), 说明 spring admin 健康检测没有通过导致 eureka 注册中心不会把这个实例从列表中删除掉. 这样所有使用 ... 
- JavaScript 那些不经意间发生的数据类型自动转换
			JavaScript可以自由的进行数据类型转换,也提供了多种显式转换的方式.但是更多的情况下,是由JavaScript自动转换的,当然这些转换遵循着一定的规则,了解数据类型自由转换的规则是非常必要的. ... 
- python 跨平台获取网卡信息和本机ip地址
			笔者在项目中遇到过获取本机网卡ip的例子,利用python库psutil解决了此问题. def get_netcard(): """获取网卡名称和ip地址 "& ... 
- opencv2函数学习之flip:实现图像翻转
			在opencv2中,flip函数用来进行图片的翻转,包括水平翻转,垂直翻转,以及水平垂直翻转. void flip(const Mat& src, Mat& dst, int flip ... 
