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. Adversarial Auto-Encoders

    目录 Another Approach: q(z)->p(z) Intuitively comprehend KL(p|q) Minimize KL Divergence How to comp ...

  2. STM32F407 串口通信:分类&常见接口 个人笔记

    串行通信的分类 常见串行通信接口 STM32 UART STM32串口异步通信需要定义的参数: ① 起始位 ② 数据位(8位或者9位) ③ 奇偶校验位(如果有的话是第9位) ④ 停止位(1,15,2位 ...

  3. 权限管理组件:rbac

    rbac: Role_Based Access Control,基于角色的权限控制 权限:一个包含正则表达式 的url就是一个权限 目录结构: rbac这个app中的文件代码如下: rbac/mode ...

  4. Kafka 生产消费 Avro 序列化数据

    https://unmi.cc/kafka-produce-consume-avro-data/ https://unmi.cc/apache-avro-serializing-deserializi ...

  5. AOJ 0118 Property Distribution (DFS)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46522 简单DFS,题目翻译参考  http://blog.csdn.net ...

  6. SOJ 2749_The Fewest Coins

    [题意]:已知整个交易系统有N (1 ≤ N ≤ 100)种不同的货币,分别价值V1,V2,V3.......VN(1 ≤ Vi ≤ 120),FJ分别有C1,C2,C3.....CN(0 ≤ Ci ...

  7. 谈谈hibernate的缓存

    一,hibernate是什么技术? 1  hibernate是一个orm 就是对象关系映射框架.  及我们创建的实体类对象与数据库表中的数据是一一对应的关系.  我们可以改变对象信息从而改变数据库中的 ...

  8. wget: unable to resolve host address “mirrors.163.com” 的解决办法

    wget:无法解析主机地址.这就能看出是DNS解析的问题. 解决办法: 登入root(VPS). 进入/etc/resolv.conf. 修改内容为下nameserver 8.8.8.8 #googl ...

  9. SQL PATINDEX检索

    语法格式:PATINDEX ( '%pattern%' , expression ) 返回pattern字符串在表达式expression里第一次出现的位置,起始值从1开始算. pattern字符串在 ...

  10. java常用工具类 - 全角转半角、半角转全角

    全角转半角.半角转全角代码 /** * <PRE> * 提供对字符串的全角->半角,半角->全角转换 * codingwhy.com * </PRE> */ pub ...