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 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 and q 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 a n + b n. You can always assume that a n + b n fits in a signed 64-bit integer.
Sample Input
10 16 2 7 12 3 0 0
Sample Output
68 91
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 49
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-;
/*
矩阵快速幂!
an = p an - q an-1
*/
LL p, q, n;
struct Mat
{
Mat()
{
memset(a, , sizeof(a));
}
LL a[][];
Mat operator * (const Mat& rhs)
{
Mat ret;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (a[i][j])
{
for (int k = ; k < ; k++)
ret.a[i][k] += a[i][j] * rhs.a[j][k];
}
}
}
return ret;
}
};
Mat fpow(Mat m, LL b)
{
Mat tmp = m, ans;
ans.a[][] = ans.a[][] = ;
while (b != )
{
if (b & )
ans = tmp * ans;
tmp = tmp * tmp;
b /= ;
}
return ans;
}
int main()
{
while (scanf("%lld%lld%lld", &p, &q, &n) == )
{ LL a1 = p, a2 = p*p - * q;
if (n == )
printf("2\n");
else if (n == )
printf("%lld\n", a1);
else if (n == )
printf("%lld\n", a2);
else
{
n = n - ;
Mat M;
M.a[][] = p, M.a[][] = -q, M.a[][] = ;
M = fpow(M, n);
printf("%lld\n", M.a[][] * a2 + M.a[][] * a1);
}
}
}
Contemplation! Algebra 矩阵快速幂的更多相关文章
- Contemplation! Algebra(矩阵快速幂,uva10655)
Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...
- 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 输 ...
随机推荐
- ACM_01背包
背包1 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量不超过 W ...
- Android 性能优化(27)*zipalign让apk数据对齐,运行更快。
1.zipalign 简介 zipalign is an archive alignment tool that provides important optimization to Android ...
- SQL数据库基础知识——抽象类
抽象类,只为继承而出现,不定义具体的内容,只规定该有哪些东西:一般抽象类中只放置抽象方法,只规定了返回类型和参数:比如: 人 - 有吃饭,睡觉方法: 男人 - 继承人抽象类,必须实现吃饭,睡觉的方法主 ...
- SQL在一张表中根据父ID获取所有的子ID
with a as ( select id,name,parentid from categories where id=53 union all select x.id,x.name,x.paren ...
- 15 C#中的条件执行,if else
在这一节的练习中,给大家介绍C#编程中的一个重要部分,条件执行.也就是If else语句.我们现实生活中的很多复杂的推理都可以用这个语法实现. If else语句的常规的样子,如下面所示. if (测 ...
- Java运行报错问题——Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
http://blog.csdn.net/xifeijian/article/details/8830933 上述这个朋友博文提醒,可能是因为其他软件添加了JAVA_HOME的路径造成冲突.但他支持删 ...
- 【译】x86程序员手册22-6.4页级保护
6.4 Page-Level Protection 页级保护 Two kinds of protection are related to pages: 与页相关的保护有两类: Restriction ...
- h5移动端混编总结
1.通信机制:解决是否能通信的问题: 2.接口:解决调用会话问题: 3.数据.URL正确性:解决数据.URL跳转路径正确性问题.
- Spring框架系列(一)--Spring MVC基础知识
Web项目开发过程中一般都是使用MVC(Model-View-Controller)模式,早先的Struts2到Spring MVC,再到现在Spring Boot,都是相似的思 路.Spring B ...
- python队列的实现
队列是一种抽象数据结构,具有以下特点: (1)具有先进先出的特性(FIFO) (2)拥有两种基本操作,即加入和删除,而且使用front和rear两个指针来分别指向队列的前端和末尾. 队列的基本操作 c ...