uva10655
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的更多相关文章
- 【UVA10655】 Contemplation! Algebra
题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) ...
- UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...
- Contemplation! Algebra(矩阵快速幂,uva10655)
Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...
- UVA-10655 Contemplation! Algebra (矩阵)
题目大意:给出a+b的值和ab的值,求a^n+b^n的值. 题目分析:有种错误的方法是这样的:利用已知的两个方程联立,求解出a和b,进而求出答案.这种方法之所以错,是因为这种方法有局限性.联立之后会得 ...
- 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不一 ...
- UVa 10655 n次方之和(矩阵快速幂)
https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: ...
随机推荐
- Python之面向对象继承和派生
Python之面向对象继承和派生 什么是继承: 继承是一种创建新的类的方法.在Python中,新建的类可以继承自一个或多个父类.原始类称为基类或超类. 新建的类称为派生类或子类. Python中类的继 ...
- A - 栈
Description You are given a string consisting of parentheses () and []. A string of this type is s ...
- admin源码之url设计
如何实现批量设计url? 1.创建Django项目 2.新建app01 和 app02 3.在models.py中创建模型 4.在settings.py中完成数据库配置.app配置等 5.数据库迁移 ...
- UI组件之色彩选择器
var myData = { canvas : document.getElementById('colors'),// context : myData.canvas.getContext('2d' ...
- two strings
A1484. two strings(罗干) 时间限制:1.0s 内存限制:256.0MB [问题描述] 给定两个字符串A和B,有五种操作,操作1为在A串开头添加一个字符,操作2为在A串结尾添加一 ...
- [codevs3044][POJ1151]矩形面积求并
[codevs3044][POJ1151]矩形面积求并 试题描述 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行一个数n ...
- springboot 集成日志 yml配置
原文:https://www.cnblogs.com/bigben0123/p/7895696.html
- 使用vim正则表达式删除C/C++注释 及 两种注释风格替换
/*对于C风格的注释可以使用如下命令*/ :%s/\_s*\/\*\(\S\|\_s\)\{-}\*\///g //对于C++风格注释 :%s/\/\/.*//g /*...*/ -> //.. ...
- test markdown 写博客
欢迎使用 Cmd Markdown 编辑阅读器 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们为记录思想 ...
- outlook pst 过大,要立刻处理
用鼠標右鍵點擊Email Account,選擇“資料檔案屬性” 進入內容,再點擊“資料夾大小”, “全部大小”不要大於20,480,000KB(20GB), 如果超過請立即分拆此pst檔案, 或者點擊 ...