Another kind of Fibonacci

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1272    Accepted Submission(s): 490

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
 
Author
wyb

虽然以前接触过矩阵,但是拿到这题后还是无从下手,后来上网看了好多有关矩阵的东西,总算明白如何用矩阵来求解问题了,这道题算是自己的一个矩阵入门吧。

这题与Fibonacci求f(n)很像,但是这题求得是前n项的平方和,已知,A(n)=x*A(n-1)+y*A(n-2)--(1),S(n)=A(0)^2+A(1)^2+……+A(n)^2=S(n-1)+A(n)^2--(2)

将(1)式带入(2)得,S(n)=S(n-1)+x*x*A(n-1)^2+y*y*A(n-2)^2+2*x*y*A(n-1)*A(n-2)--(3),根据(3)式可得矩阵B={S(n-1),A(n-1)^2,A(n-2)^2,A(n-1)*A(n-2)},

现在需要一个四阶矩阵A,使得B乘以A后得到{S(n),A(n)^2,A(n-1)^2,A(n)*A(n-1)},接下来就是构造这个函数A,不难得出

|1       0      0     0|

|x*x  x*x    1     x|

A=  |y*y  y*y    0     0|

|2xy  2xy    0     y|

最后{S(1) , A(1)^2 , A(0)^2 , A(1)*A(2)}*A^(n-1)={S(n) , A(n)^2 , A(n-1)^2 , A(n)*A(n-1)},该矩阵第一个元素即为所求答案。其中A^(n-1)用快速幂求解。

#include<stdio.h>
#include<string.h>
void fac1(__int64 a[4][4],__int64 b[4][4])
{
 __int64 i,j,k,c[4][4];
 memset(c,0,sizeof(c));
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   for(k=0;k<4;k++)
   {
    c[i][j]+=(a[i][k]*b[k][j])%10007;
   }
  }
 }
 for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   a[i][j]=c[i][j]%10007;
}
void fac2(__int64 a[4][4],__int64 b[4])
{
 __int64 i,j,c[4];
 memset(c,0,sizeof(c));
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
   c[i]+=(b[j]*a[j][i])%10007;
 }
 for(i=0;i<4;i++)
  b[i]=c[i]%10007;
}
void pow(__int64 a[4][4],__int64 n)
{
 __int64 i,j,ans[4][4];
 memset(ans,0,sizeof(ans));
 for(i=0;i<4;i++)
  ans[i][i]=1;
 while(n>=1)
 {
  if(n%2)fac1(ans,a);
  n/=2;
  fac1(a,a);
 }
 for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   a[i][j]=ans[i][j]%10007;
}
int main()
{
 __int64 n,x,y;
 while(scanf("%I64d%I64d%I64d",&n,&x,&y)!=EOF)
 {
  __int64 a[4][4],b[4];
  a[0][0]=a[1][2]=1;
  a[0][1]=a[0][2]=a[0][3]=a[2][2]=a[2][3]=a[3][2]=0;
  a[1][0]=a[1][1]=(x*x)%10007;
  a[1][3]=x%10007;
  a[2][0]=a[2][1]=(y*y)%10007;
  a[3][0]=a[3][1]=(2*x*y)%10007;
  a[3][3]=y%10007;
  b[0]=2;b[1]=1;b[2]=1;b[3]=1;
  pow(a,n-1);
  fac2(a,b);
  printf("%I64d\n",b[0]%10007);
 }
 return 0;
}

HDU3306-Another kind of Fibonacci(矩阵构造)的更多相关文章

  1. HDU3306 Another kind of Fibonacci 矩阵

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3306 题意概括 A0=1,A1=1,AN=X*AN-1+Y*AN-2(N>=2).求SN,SN ...

  2. BZOJ3286 Fibonacci矩阵 矩阵 快速幂 卡常

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3286 题意概括 n,m,a,b,c,d,e,f<=10^1000000 题解 神奇的卡常题目 ...

  3. hdu 1588(Fibonacci矩阵求和)

    题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...

  4. hdu3306 Another kind of Fibonacci【矩阵快速幂】

    转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...

  5. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  6. POJ3070 Fibonacci[矩阵乘法]【学习笔记】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

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

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

  8. 【全国互虐】Fibonacci矩阵

    orz啊又被屠了 人生如此艰难 题意: 给定一个k维的n^k的超立方体 超立方体的元素Ai1,i2,...,ik 的值为f(i1+i2+...+ik-k+1) f为斐波那契数列 求该超立方体的所有元素 ...

  9. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

随机推荐

  1. Python学习第十二课——json&pickle&XML模块&OS模块

    json模块 import json dic={'name':'hanhan'} i=8 s='hello' l=[11,22] data=json.dumps(dic) #json.dumps() ...

  2. USN日志

    转载:https://www.iteye.com/blog/univasity-805234    https://blog.51cto.com/velika/1440105 源码:https://f ...

  3. elasticsearch 自定义routing

    由于线上elasticsearch集群数据量越来越大,优化已经已经是重中之重. 优化的方式有很多中,网上一大堆,自行百度. 优化方案中有个叫routing的方案是个需要熟悉业务日志才能使用.于是我就研 ...

  4. 在 aws emr 上,将 hbase table A 的数据,对 key 做 hash,写到另外一张 table B

    先 scan 原表,然后 bulkload 到新表. 采坑纪录1. bulkload 产生 hfile 前,需要先对 hash(key) 做 repartition,在 shuffle 的 read ...

  5. eclispe+maven+ssm+sql_server/mysql配置

    链接: https://pan.baidu.com/s/1_BFI8XfS8l89-3-1IjlVZg 密码: x9in

  6. 【快学springboot】8.JPA乐观锁OptimisticLocking

    介绍 当涉及到企业应用程序时,正确地管理对数据库的并发访问是至关重要的.为此,我们可以使用Java Persistence API提供的乐观锁定机制.它导致在同一时间对同一数据进行多次更新不会相互干扰 ...

  7. jgrid异步数据加载

    参考:https://blog.csdn.net/hurryjiang/article/details/7077725

  8. stringstream常见用法介绍

    1 概述 <sstream> 定义了三个类:istringstream.ostringstream 和 stringstream,分别用来进行流的输入.输出和输入输出操作.本文以 stri ...

  9. 移动端触摸touchstart监听事件

    click.mousedown等事件适用于PC端,在移动端会有一定时间的延迟,所以更好的优化移动端体验,要用touch事件, 1.首先要添加一个监听事件,监听移动端行为 element.addEven ...

  10. JDBC--Statement使用

    1.通过Statement实现类执行更新操作(INSERT.UPDATE .DELETE): --1)获取数据库连接Connection的对象: --2)通过Connection类的createSta ...