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 ...
随机推荐
- nodejs 环境搭建
一 下载nodejs 官网:http://nodejs.cn/ 有时官网有点慢,可以去其他地方下载 统一下载站:http://www.3987.com/xiazai/2/43/57188.html 二 ...
- 【BZOJ2004】[Hnoi2010]Bus 公交线路 状压+矩阵乘法
[BZOJ2004][Hnoi2010]Bus 公交线路 Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1 ...
- {sharepoint}提升 SharePoint 代码执行权限
提升 SharePoint 代码执行权限 关于如何提升 SharePoint 代码执行权限及相关知识介绍的文章我们园子里有很多, 这里给出其中两篇文章的链接,就不再啰嗦了. http://www.cn ...
- [SQL] SQL 修复命令
You should run the repair from the original installation media, using the following command line ...
- awk中的冒泡排序
算法中经典的排序方式,今也用awk来实现下,代码如下: BEGIN { count=} {arrary[count]=$ count++ } END{ ;i>-;i--) { ;j<i;j ...
- MySQL命令行基本命令操作
进入命令模式后, 显示所有数据库 show databases; 选定某个数据库 use 数据库名; 创建数据库 create database 数据库名; 删除数据库 drop table 数据库名 ...
- 问答项目---金币经验奖励规则及网站配置写入config文件
具体步骤: 引入配置文件——>获取当前数组——>进行合并 public function edit(){ //引入 config.php配置文件 $file = APP_PATH.'Com ...
- 解决pip install 安装慢问题
使用豆瓣源 比如安装pyspark pip install -i https://pypi.douban.com/simple/ pyspark 速度就比用pip install快N倍 关注公众号:
- Linux创建Python虚拟环境
Linux创建Python虚拟环境 安装 pip install virtualenv 基本使用 为一个工程创建一个虚拟环境: $ cd my_project $ virtualenv venv #v ...
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求 The serverRuntime@appConcurrentRequestLimit setting is being exceeded.
今天下午17点左右,博客园博客站点出现这样的错误信息: Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@a ...