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

这道题目根据递推式是可以转化为矩阵的:


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
using namespace std; typedef long long LL; LL p,q,n; struct Matrix
{
LL m[][];
}ans,base; Matrix multi(Matrix a,Matrix b)
{
Matrix temp;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
temp.m[i][j]=;
for(int k=;k<;k++)
{
temp.m[i][j]=(temp.m[i][j]+a.m[i][k]*b.m[k][j]);
}
}
return temp;
} void pow(int x)
{
ans.m[][]=ans.m[][]=;
ans.m[][]=ans.m[][]=;
base.m[][]=;
base.m[][]=;
base.m[][]=-q;
base.m[][]=p;
while(x)
{
if(x&)
{
ans=multi(ans,base);
}
base=multi(base,base);
x>>=;
}
printf("%lld\n",ans.m[][]*p+*ans.m[][]);
} int main()
{
//freopen("D:\\input.txt","r",stdin);
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;}
pow(n-);
}
return ;
}
UVa 10655 n次方之和(矩阵快速幂)的更多相关文章
- UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)
题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...
- UVa 11149 Power of Matrix 矩阵快速幂
题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\ ...
- POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...
- uva 10518 - How Many Calls?(矩阵快速幂)
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...
- Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)
题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3); 求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...
- UVA - 11149 (矩阵快速幂+倍增法)
第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio ...
- UVa 10870 Recurrences (矩阵快速幂)
题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...
- UVA - 10870 Recurrences 【矩阵快速幂】
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
随机推荐
- ORA-01153: an incompatible media recovery is active
ORA-01153: an incompatible media recovery is active Cause: Attempted to start an incompatible media ...
- 设备加速传感器(accelerometer) ---- HTML5+
模块:Accelerometer Accelerometer模块管理设备加速度传感器,用于获取设备加速度信息,包括x(屏幕水平方向).y(垂直屏幕水平方向).z(垂直屏幕平面方向)三个方向的加速度信息 ...
- ST-LINK使用注意
利用ST-LINK下载程序注意事项: 1.接线 按照上面图对着自己的开发板连接相应的引脚就可以了. 2.keil5配置 线连接完之后,要对自己的工程进行相关的 配置才能正确进行下载. 首先选择ST-L ...
- Linux系列-Xshell连接本地VMware安装的Linux虚拟机
一.安装VMwareWorkstation并安装RedHat虚拟机,这里安装步骤省略,网络的资料很多,大侠们不如百度或者谷歌一下,大把的资料. 二.打开本地电脑的“网络连接”,你会发现多出了2个网络适 ...
- mysql查看线程详解(转载)
如果有 SUPER 权限,则可以看到全部的线程,否则,只能看到自己发起的线程(这是指,当前对应的 MySQL 帐户运行的线程). mysql> show processlist; +—–+——— ...
- Why do some system users have /usr/bin/false as their shell? What's the difference between /sbin/nologin and /bin/false
https://www.quora.com/How-can-bin-true-and-bin-false-Linux-utilities-be-used MySQL :: MySQL 8.0 Refe ...
- Maven的pom文件配置
pom.xml文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...
- EasyUI Pagination 分页
通过 $.fn.pagination.defaults 重写默认的 defaults. 分页(pagination)允许用户通过翻页导航数据.它支持页面导航和页面长度选择的可配置选项.用户可以在分页的 ...
- python开发之路目录
Python 目录 基础 python入门 python数据类型.字符编码.文件处理 python函数基础 python函数进阶 python装饰器函数 python装饰器函数 python递归函数 ...
- 7.3 Models -- Creating And Deleting Records
一.Creating 1. 你可以通过调用在store中的createRecord方法来创建records. store.createRecord('post', { title: 'Rails is ...