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

矩阵快速幂。。。

很明显,要把a和b解出来很困难,因为还有虚数,讨论很烦

那么我们就要转化一下 用p和q解决问题

我们先用a^(n-1)+b^(n-1)推出a^n+b^n

要想升幂,还是生一次 只能乘a+b,但是会多出来两项(在纸上写一下,这里不方便打公式)

中间的两项提出一个a*b,变成了a^(n-2)+b^(n-2) 那么我们就可以递推了

但是太慢了,就用矩阵快速幂。。。

构造矩阵就行了

注意特判n==0

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct mat {
ll a[][];
} A, B;
ll p, q, n;
mat operator * (mat A, mat B)
{
mat ret; memset(ret.a, , sizeof(ret.a));
for(int i = ; i < ; ++i)
for(int j = ; j < ; ++j)
for(int k = ; k < ; ++k) ret.a[i][j] = ret.a[i][j] + A.a[i][k] * B.a[k][j];
return ret;
}
mat power(mat A, ll t)
{
mat ret; memset(ret.a, , sizeof(ret.a));
for(int i = ; i < ; ++i) ret.a[i][i] = ;
for(; t; t >>= , A = A * A) if(t & ) ret = ret * A;
return ret;
}
int main()
{
while(scanf("%lld%lld%lld", &p, &q, &n) == )
{
if(n == ) { puts(""); continue; }
if(n == ) { printf("%lld\n", p); continue; }
if(n == ) { printf("%lld\n", p * p - * q); continue; }
A.a[][] = p; A.a[][] = -q;
A.a[][] = ; A.a[][] = ;
B.a[][] = p * p - * q; B.a[][] = p;
B = power(A, n - ) * B;
printf("%lld\n", B.a[][]);
}
return ;
}

uva10655的更多相关文章

  1. 【UVA10655】 Contemplation! Algebra

    题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) ...

  2. UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...

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

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

  4. UVA-10655 Contemplation! Algebra (矩阵)

    题目大意:给出a+b的值和ab的值,求a^n+b^n的值. 题目分析:有种错误的方法是这样的:利用已知的两个方程联立,求解出a和b,进而求出答案.这种方法之所以错,是因为这种方法有局限性.联立之后会得 ...

  5. uva10655矩阵快速幂

    a^(n+2)+b^(n+2)=(a+b)*(a^(n+1)+b^(n+1))-a*b*(a^n+b^n) 坑爹的题目关系式都推出来了居然还是wa了..... 不能只看p,q=0就退出,因为a,b不一 ...

  6. UVa 10655 n次方之和(矩阵快速幂)

    https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: ...

随机推荐

  1. Hadoop2.x异常总结

    问题1: 在执行bin/hdfs namenode -format格式化HDFS命令时,抛出异常,异常如下: 16/10/26 18:32:45 ERROR namenode.NameNode: Fa ...

  2. Python面试快问快答,理论要的就是速度与精准,Python面试题No2

    今天的面试题 第1题:python2和python3的range(100)的区别 range()函数的含义 range函数是一个用来创建算数级数序列的通用函数,返回一个[start, start + ...

  3. JS判断滚动条是否停止滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 怎样在WINDOWS下面编译OpenSSL

    编译OPENSSL的步骤: 第一步:下载ActivePerl(http://www.activestate.com/, ),安装ActivePerl,默认安装路径在C:\Perl64.打开命令提示符, ...

  5. 树剖 lca

    GeneralLiu  橙边为轻边 红边为重边 绿数为每个点的 top 橙数为每个点的编号 步骤 1 先预处理 每个点的 deep深度  size子树大小  dad父节点 2 再预处理 每个点的 to ...

  6. bzoj 1430 小猴打架 prufer 性质

    小猴打架 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 709  Solved: 512[Submit][Status][Discuss] Descri ...

  7. 营救(洛谷 P1396)

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  8. android开发里跳过的坑——“org.apache.http.message.BasicHeaderValueFormatter.INSTANCE”错误

    在android4.4.2的系统里,写了一个系统应用,其中有一个功能是通过表单上传图片的,使用了httpclient-4.5.3.jar httpmime-4.5.3.jar httpcore-4.4 ...

  9. 2018/4/7 Mybatis源码结构概览

    在观看Mybatis源码的过程中,有一点疑惑,就是Mybatis的缓存设计明显有问题,首先,Mybatis缓存分为两级,先说一级,生命周期为一个sqlsession,只有在查询相同方法时才会命中缓存, ...

  10. 改动Centosserver主机名称

    1.暂时改动server主机名称: hostname myhost. myhost为你指定的主机名称. 2.永久性的改动主机名称 Centosserver安装好之后.默认的主机名为:localhost ...