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 输 ...
随机推荐
- 迁移桌面程序到MS Store(4)——桌面程序调用Win10 API
上一篇我们讨论了如何在转制的桌面程序中,通过StartupTask来实现转制版本的开机自启动.实际操作中,我们通过编辑Packaging工程中的Package.appxmanifest文件,来添加自启 ...
- 【计算机网络】网络层学习笔记:总结IP,NAT和DHCP
前言:这篇文章是学习网络层协议时候总结的笔记,前面的主要部分介绍的都是IP协议, 后半部分介绍NAT协议和DHCP协议 参考书籍 <计算机网络-自顶向下> 作者 James F ...
- Android必学之AsyncTask
AsyncTask,即异步任务,是Android给我们提供的一个处理异步任务的类.通过此类,可以实现UI线程和后台线程进行通讯,后台线程执行异步任务,并把结果返回给UI线程. .为什么需要使用异步任务 ...
- angularJS详解
这篇文章转载 kooky798 的博客,http://blog.csdn.net/yy374864125/article/details/41349417, 写到这么详细也是没谁了,必须粉一个 1 ...
- SSH 学习笔记
零.背景 在看 pm2 的 deploy 功能的时候,对 ssh 的不熟悉导致错误频出,包括之前对 github 的配置也用到了 SSH,所以找个机会整理一下. 一.介绍 SSH 是每一台 Linux ...
- Swift 里 Set(一)辅助类型
_UnsafeBitset  是一个固定大小的 bitmap,用来确定指定位置是否有元素存在. HashTable  具体的 hash 碰撞算法在HashTable里实现,目前使用的是简单的开放地 ...
- 02-01 Java关键字、标识符、注释、常量和进制问题、变量和数据类型
1:关键字 (1)被Java语言赋予特定含义的单词 (2)特点: 全部小写. (3)注意事项: A:goto和const作为保留字存在. B:类似于Notepad++这样的高级记事本会对关键字有特殊颜 ...
- 安装SVN并进行汉化的详细步骤
安装SVN并进行汉化的详细步骤 SAE提供了不同的代码部署方式,可以分为两类:一是通过SVN客户端部署,这是SAE推荐的代码部署方法.另一个是通过非SVN客户端部署,即在线代码在线编辑器和推荐应用安装 ...
- Python标准库 -- UUID模块(生成唯一标识)
UUID是什么: UUID: 通用唯一标识符 ( Universally Unique Identifier ),对于所有的UUID它可以保证在空间和时间上的唯一性,也称为GUID,全称为: UUID ...
- C++:复制构造函数
关于复制构造函数的具体细节:浅层复制和深层复制等可以看下 范磊老师的<零起点学通C++>视频教程和<C++ Primer Plus>一书. 相信看完后,对复制构造函数能基本掌握 ...