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 矩阵快速幂的更多相关文章

  1. Contemplation! Algebra(矩阵快速幂,uva10655)

    Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...

  2. UVa 10655 Contemplation! Algebra 矩阵快速幂

    题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...

  3. uva 10655 - Contemplation! Algebra(矩阵高速幂)

    题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...

  4. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  5. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  6. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  7. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  8. 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 ...

  9. 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 输 ...

随机推荐

  1. Git学习笔记(0)-错误汇总

    一.LF will be replaced by CRLF 1.发现问题 $ git add welcome.txt warning: LF will be replaced by CRLF in w ...

  2. git tag管理

    操作 实例 创建标签 git tag -a V1.2 -m 'WebSite version 1.2' 查看标签 git tag / git show V1.2 远程推送 git push origi ...

  3. cocos2d-x win7 部署

    1. 安装 下载python  https://www.python.org/downloads/release/python-279/ 2.从官网下载cocos2d-x  http://www.co ...

  4. 2017-11-28 Html-浅谈如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

  5. 关于使用 autoFac 的注入方法

    1.使用 NUGET 安装 Autofac 2.需要引用Autofac ASP.NET MVC 5 Integration  这个扩展包. 但有时候在NuGet中找不到 该包 需要使用“程序要控制器控 ...

  6. [Windows Server 2012] 初识Windows Server 2012

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:初次见识Win ...

  7. Magento 多站点多域名安装教程(可以设置手机模版哟,亲 \(^o^)/)

    这篇文章是安装magento子域名的教程,请先进行安装之前,确认以下几点: 1.请先确认子域名是否已经指向你的服务器 2.可以编辑.htaccess文件 3. 熟悉Cpanel操作 我们的目标是建立一 ...

  8. CAD利用Select2得到所有实体(网页版)

    主要用到函数说明: IMxDrawSelectionSet::Select2 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARI ...

  9. 面向对象程序设计--Java语言第一周编程题:分数

    分数 题目内容: 设计一个表示分数的类Fraction.这个类用两个int类型的变量分别表示分子和分母. 这个类的构造函数是: Fraction(int a, int b) 构造一个a/b的分数. 这 ...

  10. tab下拉显示

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...