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

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 <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 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

思路:矩阵快速幂;

S(n) = ∑f(n)2 = S(n-1)+f(n)2 = S(n-1)+x2f(n-1)2+y2f(n-2)2+2xyf(n-1)f(n-2);

然后f(n)*f(n-1) = (x*f(n-1)+y*f(n-2))*f(n-1) = x*f(n-1)2+y*f(n-1)*f(n-2);

然后构造矩阵;

其中的第三个矩阵写错了,应该是s[n-1];     f[n-1]^2;     f[n-2]^2;  f[n-1]*f[n-2];

 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 }
时间: 2024-10-16 05:40:30

Another kind of Fibonacci(hdu3306)的相关文章

HDU3306—Another kind of Fibonacci

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3306 题目意思:一个斐波那契数列的变式,本来是A[n]=A[n-1]+A[n-2],现在变成A[n]=N*A[n-1]+Y*A[n-2].一个很简单的矩阵快速幂. S(N) = A(0)2 +A(1)2+--+A(n)2对系数矩阵稍微变化一下就可以了.唯一需要注意的是N和Y可能很大,所以需要先mod一下. 思路:首先先求A[n]^2,因为A[n]=N*A[n-1]+Y*A[n-2],所以A[n]^2

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. 这个矩阵有点毒.. 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<math.h> 5 //#include<iostream> 6 using namespace std; 7 8 #de

NYOJ 480 Fibonacci Again!

Fibonacci Again! 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 求第n个斐波那契数是否是一个素数,n为整数 f[n]=f[n-1]+f[n-2] (2<n<30) f[1]=3,f[2]=7 输入 输入整数m,0<m<30,输入-1表示结束输入 输出 假设f[m]是素数 则输出Yes,否则输出No, 每行输出占一行. 例子输入 2 3 例子输出 Yes No #include<stdio.h> int f[35]={0

HDU1848 Fibonacci again and again

Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8198    Accepted Submission(s): 3412 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;

hdu 5167 Fibonacci(DFS)

hdu 5167 Fibonacci 问题描述 斐波那契数列的递归定义如下: Fi=???01Fi?1+Fi?2i = 0i = 1i > 1 现在我们需要判断一个数是否能表示为斐波那契数列中的数的乘积. 输入描述 有多组数据,第一行为数据组数T(T≤100,000). 对于每组数据有一个整数n,表示要判断的数字. 0≤n≤1,000,000,000 输出描述 对于每组数据,如果可以输出"Yes",否则输出"No". 输入样例 3 4 17 233 输出样例

HDU 4099 Revenge of Fibonacci

Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others) Total Submission(s): 2027    Accepted Submission(s): 475 Problem Description The well-known Fibonacci sequence is defined as following: Here w

Fibonacci斐波拉契数列----------动态规划DP

n==10 20 30 40 50 46 体验一下,感受一下,运行时间 #include <stdio.h>int fib(int n){ if (n<=1)     return 1; else            return fib(n-1)+fib(n-2); }int main( ){ int n; scanf("%d",&n); printf("%d\n" ,fib(n) );} 先 n==10 20 30 40 50 46

HUDJ 1021 Fibonacci Again

Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 36028    Accepted Submission(s): 17385 Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n)

Fibonacci数

Fibonacci数 描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为F(n)=1 ...........(n=1或n=2)F(n)=F(n-1)+F(n-2).....(n>2)现要你来求第n个斐波纳奇数.(第1个.第二个都为1) 输入 第一行是一个整数m(m<5)表示共有m组测试数据每次测试数据只有一行,且只有一个整形数n(n<20) 输出 对每组输入n,输出第n个Fibonacci数 实现: package July;