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. Orcad中较好的习惯背景

    设置背景,能让眼睛不那么疲劳,尤其是白色背景,有的时候很耀眼,看的时间长了,眼疼. 因此萌生出更换背景的想法: Option->Preference中 第一列的Prin中"Alias& ...

  2. C++——"%"运算符

    基本介绍 r=a%b,余数的取值范围为-b+1~b-1,符号与a一致 要想得到0~b-1的余数,可以用:(a%b+b)%b 数位拆解 方法一:反复的:%10,/10 sizea = 0; while ...

  3. JDK的安装和环境变量配置

    1.安装JDK开发环境 下载网站: http://www.oracle.com/technetwork/java/javase/downloads/index.html 进入后选择Accept Lic ...

  4. HDU 5492 Find a path

    Find a path Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...

  5. linux & command line & console & logs

    linux & command line & console & logs how to get the logs form linux command console htt ...

  6. [COJ0970]WZJ的数据结构(负三十)

    [COJ0970]WZJ的数据结构(负三十) 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计一个数据结构,回答M次操作. 1 x v:对于树上的每一个节点y,如果将x.y在树上的距离记为 ...

  7. ****使用ftp软件上传下载php文件时换行符丢失bug

    在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了,导致php文件 ...

  8. System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。

    错误背景: 操作系统:编程环境:VS2013.  语言:VB.net:  数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...

  9. webpack-入口篇

    入口起点[Entry Points]: 单个入口(简写)语法 用法:entry: string|Array<string> 例子(webpack.config.js) const conf ...

  10. 【Akka】Actor模型探索

    Akka是什么 Akka就是为了改变编写高容错性和强可扩展性的并发程序而生的.通过使用Actor模型我们提升了抽象级别,为构建正确的可扩展并发应用提供了一个更好的平台.在容错性方面我们採取了" ...