Another kind of Fibonacci(hdu3306)
Another kind of Fibonacci
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2353 Accepted Submission(s): 936
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.
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1

1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<set>
7 #include<math.h>
8 #include<map>
9 using namespace std;
10 typedef struct node
11 {
12 int m[4][4];
13 node()
14 {
15 memset(m,0,sizeof(m));
16 }
17 } maxtr;
18 void Init(maxtr *ans,int x,int y);
19 maxtr E();
20 maxtr quick_m(maxtr ak,int m);
21 const int mod = 10007;
22 int main(void)
23 {
24 int n,x,y;
25 while(scanf("%d %d %d",&n,&x,&y)!=EOF)
26 {
27 int f1 = 2;
28 int a1 = 1;
29 int a0 = 1;
30 int xx = 1;
31 maxtr ask ;
32 Init(&ask,x,y);
33 maxtr tp = quick_m(ask,n-1);
34 printf("%d\n",(tp.m[0][0]*2+tp.m[0][1]*a1+tp.m[0][2]*a0+tp.m[0][3]*xx)%mod);
35 }
36 return 0;
37 }
38 void Init(maxtr *ans,int x,int y)
39 { memset(ans->m,0,sizeof(ans->m));
40 x%=mod;y%=mod;
41 ans->m[0][0] = 1;
42 ans->m[0][1] = x*x%mod;
43 ans->m[0][2] = y*y%mod;
44 ans->m[0][3] =2*x*y%mod;
45 ans->m[1][1] = x*x%mod;
46 ans->m[1][2] = y*y%mod;
47 ans->m[1][3] = 2*x*y%mod;
48 ans->m[2][1] = 1;
49 ans->m[3][1] = x%mod;
50 ans->m[3][3] = y%mod;
51 }
52 maxtr E()
53 {
54 maxtr ak;
55 int i,j;
56 for(i = 0; i < 4; i++)
57 {
58 for(j = 0; j < 4; j++)
59 {
60 if(i == j)
61 ak.m[i][j] = 1;
62 }
63 }
64 return ak;
65 }
66 maxtr quick_m(maxtr ak,int m)
67 {
68 int i,j;
69 maxtr ac = E();
70 while(m)
71 {
72 if(m&1)
73 {
74 maxtr a;
75 for(i = 0; i < 4; i++)
76 for(j = 0; j < 4; j++)
77 for(int s= 0; s < 4; s++)
78 a.m[i][j] = (a.m[i][j] + ak.m[i][s]*ac.m[s][j]%mod)%mod;
79 ac = a;
80 }
81 maxtr b;
82 for(i = 0; i < 4; i++)
83 for(j = 0; j < 4; j++)
84 for(int s = 0; s < 4; s++)
85 b.m[i][j] = (b.m[i][j] + ak.m[i][s]*ak.m[s][j]%mod)%mod;
86 ak = b;
87 m>>=1;
88 }
89 return ac;
90 }
Another kind of Fibonacci(hdu3306)的更多相关文章
- hdu3306 Another kind of Fibonacci【矩阵快速幂】
转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...
- HDU3306 Another kind of Fibonacci 矩阵
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3306 题意概括 A0=1,A1=1,AN=X*AN-1+Y*AN-2(N>=2).求SN,SN ...
- HDU3306—Another kind of Fibonacci
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3306 题目意思:一个斐波那契数列的变式,本来是A[n]=A[n-1]+A[n-2],现在变成A[n]= ...
- hdu3306:Another kind of Fibonacci
A(0)=A(1)=1,A(i)=X*A(i-1)+Y*A(i-2),求S(n)=A(0)^2+A(1)^2+A(2)^2+A(3)^2+……+A(n)^2. 这个矩阵有点毒.. #include&l ...
- HDU3306 Another kind of Fibonacci
本篇题解用于作者本人对于矩阵乘法的印象加深,也欢迎大家的阅读. 题目大意 众所周知,斐波那契数列为 \(f(0)=1\) , \(f(1)=1\) ,\(f(n)=f(n-1)+f(n-2)~(n&g ...
- 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找
今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...
- #26 fibonacci seqs
Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...
- 关于java的递归写法,经典的Fibonacci数的问题
经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...
- 斐波拉契数列(Fibonacci) 的python实现方式
第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...
随机推荐
- 34. Swap Nodes in Pairs
Swap Nodes in Pairs My Submissions QuestionEditorial Solution Total Accepted: 95230 Total Submission ...
- Python文件复制shutil模块
Python中shutil模块主要用于文件操作,如复制,属性判断等 1.copyfileobj,拷贝文件内容,将文件句柄赋给该方法 def copyfileobj(src, dst, length=1 ...
- Go语言核心36讲(Go语言实战与应用二十三)--学习笔记
45 | 使用os包中的API (下) 我们在上一篇文章中.从"os.File类型都实现了哪些io包中的接口"这一问题出发,介绍了一系列的相关内容.今天我们继续围绕这一知识点进行扩 ...
- vim使用配置(转)
在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有行号的.语法高亮度显示.智能缩进等功能的. 为了更好的在vim下进行工作,需要手动配置一个配置文件: .vimrc 在启动vim时,当前用户 ...
- hadoop Sort排序
1 public int getPartition(IntWritable key,IntWritable value,int numPartitions){ 2 int Maxnumber = 12 ...
- 【Linux】【Database】【MySQL】使用percona搭建高可用的MySQL数据库
1. 简介 1.1. 官方文档: 数据库架构:https://docs.openstack.org/ha-guide/shared-database.html 1.2. 本次使用的的是Percona ...
- Linux目录结构和基础命令
Linux目录和基础命令 目录 Linux目录和基础命令 1 Linux目录结构 1.1 Linux文件名命令要求 1.2 文件的类型 2. 基础命令 2.1 ls 2.2 cd和pwd 2.3 命令 ...
- 一个超好用的 Python 标准库,彻底玩透路径操作
pathlib 学习 Python 时,尤其是在进行文件操作和数据处理时,经常会处理路径问题.最常用和常见的是 os.path 模块,它将路径当做字符串进行处理,如果使用不当可能导致难以察觉的错误,而 ...
- 2、Spring的IOC标签介绍以及实例
一.Spring_ioc配置文件bean标签介绍 1. bean标签 名称:bean 类型:标签 归属:beans标签 作用:定义spring中的资源,受此标签定义的资源将受到spring控制 格式: ...
- Mysql报错合集
目录 一.链接报错 客户端连接mysql出错 链接客户端出错 交互登陆mysql出现warning警告Using a password 导入数据到数据库报错ERROR 1050 登陆数据库提示-bas ...