【UVA10655】 Contemplation! Algebra
题目
给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\)。
$0\le n\lt 2^{63} $
分析
大水题。
先考虑 \(n\) 较小的情况,可以很容易的想到递推:
\text{令} F(i) & = a ^ n + b ^ n \\
& = (a + b)(a ^ {n - 1} + b ^ {n - 1}) - (ab ^ {n - 1} + a^{n - 1}b) \\
& = (a + b)(a ^ {n - 1} + b ^ {n - 1}) - ab(a ^ {n - 2} + b ^ {n - 2}) \\
& = p \times F(i - 1) - q \times F(i - 2)
\end{array}
\]
然后发现这个递推式可以用矩阵优化:
p & - q \\
1 & 0
\end{matrix}\right]
\times
\left[\begin{matrix}
F[i] \\
F[i - 1]
\end{matrix}\right] =
\left[\begin{matrix}
F[i] \times p & + & F[i - 1]\times (-q) \\
F[i] \times 1 & + & F[i - 1]\times 0
\end{matrix}\right] =
\left[\begin{matrix}
F[i + 1] \\
F[i]
\end{matrix}\right]
\]
即:
p & - q \\
1 & 0
\end{matrix}\right]^n
\times
\left[\begin{matrix}
F[1] \\
F[0]
\end{matrix}\right] =
\left[\begin{matrix}
F[n + 1] \\
F[n]
\end{matrix}\right]
\]
显然,\(F[1] = p,\ F[0] = 2\)。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 10;
struct matrix {
ll a[MAXN][MAXN]; int rowSize, lineSize;
matrix(int x, int y) {
rowSize = x; lineSize = y;
}
ll *operator [](const unsigned &i) {return a[i];}
matrix operator *(matrix y) {
matrix ans(rowSize, y.lineSize);
for(int i = 0; i < ans.rowSize; i++)
for(int j = 0; j < ans.lineSize; j++) {
ans[i][j] = 0;
for(int k = 0; k < lineSize; k++)
ans[i][j] += a[i][k] * y[k][j];
}
return ans;
}
} u(2, 2);
matrix qPow(matrix x, ll b) {
matrix ans = u, base = x;
while(b) {
if(b & 1)
ans = ans * base;
base = base * base;
b >>= 1;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
u[0][0] = 1; u[0][1] = 0;
u[1][0] = 0; u[1][1] = 1;
ll p, q, n;
while(scanf("%lld%lld%lld", &p, &q, &n) == 3) {
matrix a(2, 2), b(2, 2), st(2, 1);
a[0][0] = p; a[0][1] = -q;
a[1][0] = 1; a[1][1] = 0;
st[0][0] = p; st[1][0] = 2;
b = qPow(a, n) * st;
printf("%lld\n", b[1][0]);
}
return 0;
}
UVA10655 Contemplation! Algebra
【UVA10655】 Contemplation! Algebra的更多相关文章
- 【线性代数】Linear Algebra Big Picture
Abstract: 通过学习MIT 18.06课程,总结出的线性代数的知识点相互依赖关系,后续博客将会按照相应的依赖关系进行介绍.(2017-08-18 16:28:36) Keywords: Lin ...
- Contemplation! Algebra(矩阵快速幂,uva10655)
Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...
- 【原创】开源Math.NET基础数学类库使用(01)综合介绍
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 【原创】开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】
转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...
- SCI&EI 英文PAPER投稿经验【转】
英文投稿的一点经验[转载] From: http://chl033.woku.com/article/2893317.html 1. 首先一定要注意杂志的发表范围, 超出范围的千万别投,要不就是浪费时 ...
- 【输入法】Rime-中州韵 基本设置 附:官方定制指南
前言 不知不觉就到了年终了,距离上次更新博客已经有一个半月,这段时间天天在加班,也没作一下新的学习计划,趁着元旦放假,写一点好玩的东西,这次要记录的是一点关于Rime相关的东西,文章本身不会长,只是说 ...
- 【线性代数】7-2:线性变化的矩阵(The Matrix of a Linear Transformation)
title: [线性代数]7-2:线性变化的矩阵(The Matrix of a Linear Transformation) categories: Mathematic Linear Algebr ...
- 【线性代数】7-3:对角化和伪逆(Diagonalization and the Pseudoinverse)
title: [线性代数]7-3:对角化和伪逆(Diagonalization and the Pseudoinverse) categories: Mathematic Linear Algebra ...
随机推荐
- Swiper插件
中文官网:Swiper中文网 英文:英文网 此插件功能比较强大,网页端.手机端都可以使用的插件.这里记录一下在微信h5页面里面滑动获取数据. 先下载css和js,引用到项目中 这里有6个节点,没划一个 ...
- winfrom中上传文件保存在webFrom里面
winfrom里面的代码 private void button1_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(text ...
- java.util包中 Set 和 List 的区别
http://ligaosong.iteye.com/blog/903692 对于Set 和 List都是 接口 Collection 的子接口 1.Set 不允许重复,List允许重复 2.Set ...
- HTML5<fieldset>标签
1.<fieldset>标签对表单中的相关元素进行分组. 2.<fieldset>标签会在相关表单元素周围绘制边框. <!DOCTYPE html><html ...
- Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 36
例: <aop:config> <aop:pointcut expression="execution(* com.zsn.Service.Impl.*.*(..))&qu ...
- springmvc 前端表单提交给后端出现乱码
在springmvc框架练习中遇到了乱码问题,经过一番网上查找解决方法之后,最后发现是需要在tomcat中的server.xml中添加编码设置 URIEncoding="UTF-8" ...
- Intellij IDEA 像eclipse那样给maven添加依赖,且Intellij idea里在pom.xml里添加Maven依赖,本地仓库下拉列表显示包很少的血的经验
打开pom.xml,在它里面使用快捷键:ALT+Insert ————>点击dependency 再输入想要添加的依赖关键字,比如:输个spring 出现下图: 根据需求选择版本,完成以后 ...
- filebeat的安装及配置
概述:Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasti ...
- 汇编语言编写Hello World
;================================= ; HELLO world DATAS segment string DB 'HELLO World','$' DATAS end ...
- JavaScript 事件机制
1 什么是事件 JavaScript 使我们有能力创建动态页面.事件是可以被 JavaScript 侦测到的行为. 网页中的每个元素都可以产生某些可以触发 JavaScript 函数的事件.比方说,我 ...