杭电oj3306:Another kind of Fibonacci(矩阵快速幂)
Another kind of Fibonacci
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
As we all known , the Fibonacci series : F(0) = 1, F(1) = 1, F(N) = F(N - 1) + F(N - 2) (N >= 2).Now we define another kind of Fibonacci : A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).And we want to Calculate S(N) , S(N) = A(0)^2 +A(1)2+……+A(n)2.
Input
There are several test cases.
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 2^31 – 1
X : 2<= X <= 2^31 – 1
Y : 2<= Y <= 2^31 – 1
Output
For each test case , output the answer of S(n).If the answer is too big , divide it by 10007 and give me the reminder.
Sample Input
2 1 1
3 2 3
Sample Output
6
196
思路见下图:

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 5;
typedef long long LL;
struct Matrix{
int matrix[maxn][maxn];
}ori, ans;
int n = 4, N, X, Y, m = 10007;
void init()
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
ori.matrix[i][j] = ans.matrix[i][j] = 0;
ori.matrix[0][0] = ori.matrix[2][1] = 1;
ori.matrix[0][1] = ori.matrix[1][1] = X * X % m;
ori.matrix[0][2] = ori.matrix[1][2] = Y * Y % m;
ori.matrix[0][3] = ori.matrix[1][3] = ((2 * X) % m) * Y % m;
ori.matrix[3][1] = X;
ori.matrix[3][3] = Y;
ans.matrix[0][0] = 2;
ans.matrix[1][0] = ans.matrix[2][0] = ans.matrix[3][0] = 1;
}
Matrix multiply(Matrix a, Matrix b)
{
Matrix temp;
memset(temp.matrix, 0, sizeof(temp.matrix));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
temp.matrix[i][j] = (temp.matrix[i][j] + ((a.matrix[i][k] * b.matrix[k][j]) % m)) % m;
return temp;
}
//矩阵的b次幂
Matrix binaryPow(int b)
{
Matrix temp;
memset(temp.matrix, 0, sizeof(temp.matrix));
for(int i=0;i<n;i++)
temp.matrix[i][i] = 1;
while(b > 0)
{
if(b & 1)
temp = multiply(ori, temp);
ori = multiply(ori, ori);
b >>= 1;
}
return temp;
}
int main()
{
//freopen("in.txt","r", stdin);
while(cin>>N>>X>>Y)
{
X %= m;
Y %= m;
init();
Matrix temp = binaryPow(N - 1);
ans = multiply(temp, ans);
cout<<ans.matrix[0][0]<<endl;
}
return 0;
}
杭电oj3306:Another kind of Fibonacci(矩阵快速幂)的更多相关文章
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- UVA - 10229 Modular Fibonacci 矩阵快速幂
Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- poj3070 Fibonacci 矩阵快速幂
学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...
- $loj$10222 佳佳的$Fibonacci$ 矩阵快速幂
正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\s ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- POJ3070:Fibonacci(矩阵快速幂模板题)
http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...
- hdu 3306 Another kind of Fibonacci 矩阵快速幂
参考了某大佬的 我们可以根据(s[n-2], a[n-1]^2, a[n-1]*a[n-2], a[n-2]^2) * A = (s[n-1], a[n]^2, a[n]*a[n-1], a[n-1] ...
随机推荐
- .Net使用SharpZip解压缩文件
最近,项目中使用到了上传压缩文件,文件上传到服务器后,肯定要解压,取出其中的文件才能使用,在这里做一个小结,Get这个新技能. 首先在使用NuGet管理程序在项目中添加引用ICSharpCode.Sh ...
- jmeter学习笔记一foreach控制器
ForEach控制器 输入变量前缀:上一步所提取的变量名的前缀,例如appid_1, 则appid就是前缀 start index for loop:循环的起始位置,默认为空也可 end index ...
- 本地jar在打包时打入到项目中去
<dependency> <groupId>com.hxyc</groupId> <artifactId>hxyc-common</artifac ...
- 【快学springboot】12.实现拦截器
前言 之前在[快学springboot]6.WebMvcConfigurer配置静态资源和解决跨域里有用到WebMvcConfigurer接口来实现静态资源的映射和解决跨域请求,并且在文末还说了Web ...
- Linux设备驱动的软件架构思想
驱动相关:硬件之上的软件层,负责底层硬件与用户程序的交互 设备相关:底层设备的硬件操作 总线:匹配设备和驱动 设备驱动分层的思想:为同一类设备设计一个框架,而框架中的核心层则实现了该设备的一些 ...
- S7-300 与TP900 组态 棒图 量表 滚动条 滚动条设置的值通过IO输出域显示出来
切换编程语言 注意 一定要 先选中 某一个组织块 例如 OB1 然后单击 菜单 编辑 切换编程语言 组态 300 PLC 的CPU 点击 SIMENSE LOGO 查看 循环 中断 OB35 可以 在 ...
- java并发(二):初探syncronized
参考博客 Java多线程系列--"基础篇"04之 synchronized关键字 synchronized基本规则 第一条 当线程访问A对象的synchronized方法和同步块的 ...
- Django:验证码相关问题
http://blog.csdn.net/csapr1987/article/details/7728315 https://zhidao.baidu.com/question/13837387222 ...
- 使用U盘安装Linux最美桌面发行版Elementary OS 及常用开发环境配置(JDK,Redis,MySQL,Docker,IDEA,STS)
前言 假期在家无聊,刚好把六年前的一台笔记本电脑利用起来,原来电脑虽然说配置说不上古董机器,但是运行win系统感觉还是不流畅,所幸给换成Linux桌面版系统,在网上查阅了很多,Linux桌面系统要么推 ...
- oracle练习-day02
.查询员工表和部门表.查询出雇员的编号,姓名,部门的编号和名称,地址.查询出每个员工的上级领导.在上一个例子的基础上查询该员工的部门名称.在上一个例子的基础上查询员工工资等级和他的上级领导工资等级.查 ...