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(矩阵快速幂)的更多相关文章

  1. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  2. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  3. HDU 1588 Gauss Fibonacci(矩阵快速幂)

    Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  4. UVA - 10229 Modular Fibonacci 矩阵快速幂

                                 Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...

  5. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  6. poj3070 Fibonacci 矩阵快速幂

    学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...

  7. $loj$10222 佳佳的$Fibonacci$ 矩阵快速幂

    正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\s ...

  8. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

  9. POJ3070:Fibonacci(矩阵快速幂模板题)

    http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...

  10. 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] ...

随机推荐

  1. 设计模式课程 设计模式精讲 21-2 观察者模式coding

    1 代码演练 1.1 代码演练1(一对一观察) 1.2 代码演练2(一对多观察) 1.3 代码演练3(多对多观察) 1 代码演练 1.1 代码演练1(一对一观察) 需求: 木木网课程系统,教师后台提醒 ...

  2. python第三方库介绍

  3. day12-Python运维开发基础(推导式、生成器与生成器函数)

    1. 推导式(列表推导式.集合推导式.字典推导式) # ### 推导式 : 通过一行循环判断,遍历出一系列数据的方式是推导式 """ 推导式一共三种: 列表推导式,集合推 ...

  4. easyui 加载本地json 文件的方法

    easyui 加载本地json 文件的方法 2017年12月15日 17:18:07 vivian_hnd 阅读数 2155 https://blog.csdn.net/vivian_wang07/a ...

  5. redis列表-list

    Redis的list类型其实就是一个每个子元素都是string类型的双向链表,链表的最大长度是2^32.list既可以用做栈,也可以用做队列. 常用命令: 1. lpush key value [va ...

  6. Golang基础之文件操作

    目录 文件读取 os.open文件读取 os.open循环读取 bufio文件读取 bufio循环读取 ioutil读取整个文件 文件写入 os.OpenFile文件写入 bufio.NewWrite ...

  7. 三 Road

    3—5年程序员的发展和出路在哪里? 是继续做技术人,还是向管理者发力?是继续留在大公司,还是转投潜力小公司?如果没有核心竞争力,入行一两年的新程序员朋友是可以替代你大部分工作的,而且薪资还低,要怎么办 ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-primary" 类的文本样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 深入 理解char * ,char ** ,char a[ ] ,char *a[] 的区别

    转自:https://blog.csdn.net/liusicheng2008_liu/article/details/80412586 1 数组的本质 数组是多个元素的集合,在内存中分布在地址相连的 ...

  10. InnoDB 和 MyISAM的索引区别

    MyISAM索引实现 MyISAM索引文件和数据文件是分离的,索引文件的data域保存记录所在页的地址(物理存储位置),通过这些地址来读取页,进而读取被索引的行数据. MyISAM的索引原理图如下,C ...