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);
}
}
}

最新文章

  1. sm30表维护做排序
  2. git rebase 介绍
  3. sorl维护索引库sorl4j的使用
  4. ***iOS 项目的目录结构能看出你的开发经验
  5. Android ListFragment实例Demo(自己定义适配器)
  6. Android应用开发-小巫CSDN博客client之显示博文具体内容
  7. 筛选实现C++实现筛选法
  8. 自动安装memcached服务端与PHP扩展Memcached
  9. AWS上获取监控数据(EC2/RDS都支持)
  10. kettle简介(整体架构,运行方式,使用方法)
  11. c#文件图片操作
  12. java中this的N种使用方法
  13. Arduino通过MAX9814实现录音
  14. 使用nmon监控Linxu服务器性能
  15. vue 自定义组件使用v-model
  16. HDU 2071 Max Num
  17. 尚学堂java 参考答案 第七章
  18. ubuntu播放音频没声音
  19. 心跳包(HeartBeat)
  20. 根据时间获取最新数据 SQL(每一个人或者每一项)

热门文章

  1. php可以定义数组的常量吗
  2. [转]windows 7 下快速搭建php环境(windows7+IIS7+php+mysql)
  3. ES6 学习笔记 - let和const
  4. kill 8080 port on windows
  5. 判断IE浏览器的类型以及提示信息(低版本浏览器不予显示)
  6. android 蓝牙开发---与蓝牙模块进行通讯 基于eclipse项目
  7. RabbitMQ调用
  8. Linux下如何从mysql数据库里导出导入数据
  9. java_IO_3
  10. Java实现打包文件