Another kind of Fibonacci

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

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
 
同学说,矩阵这一块,最难到如何构造矩阵,这题是构造矩阵的经典例题。
 
如果构造的呢??
A(N)= X*A(N-1)  +  Y*A(N-2)
S(N)= S(N-1)      +  A(N)^2;
合并一下
S(N)= S(N-1) + X^2*A(N-1)^2 + Y^2*A(N-2) +2*X*Y*A(N-1)A(N-2);
 
很像做过到一道题目:HDU 1757  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
那么把参数提取出来就有4个了:
                        S(N-1)     A(N-1)^2   A(N-2)%^2     A(N-1)A(N-2)
对应到系数         1               X^2           Y^2                 2*X*Y
 
| 1     X^2      Y^2       2*X*Y  |   |  S(N-1)           |
| 0     X^2      Y^2       2*X*Y  |   |  A(N-1)^2       |
| 0        1          0            0       |  |  A(N-2)^2       |
| 0       X           0            Y       |  |  A(N-1)A(N-2) |
 
 
自己推一推就可以的。
这一题还有一个地方需要注意:
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1
注意相乘时的溢出。
 
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; struct node
{
__int64 mat[][];
}M_hxl,M_tom; void make_init(__int64 x,__int64 y)
{
M_hxl.mat[][]=;
M_hxl.mat[][]=(x*x)%;
M_hxl.mat[][]=(y*y)%;
M_hxl.mat[][]=(*x*y)%; M_hxl.mat[][]=;
M_hxl.mat[][]=(x*x)%;
M_hxl.mat[][]=(y*y)%;
M_hxl.mat[][]=(*x*y)%; M_hxl.mat[][]=;
M_hxl.mat[][]=;
M_hxl.mat[][]=;
M_hxl.mat[][]=; M_hxl.mat[][]=;
M_hxl.mat[][]=x;
M_hxl.mat[][]=;
M_hxl.mat[][]=y;
} void make_first(node *cur)
{
__int64 i,j;
for(i=;i<=;i++)
for(j=;j<=;j++)
if(i==j)
cur->mat[i][j]=;
else cur->mat[i][j]=;
} struct node cheng(node cur,node now)
{
node ww;
__int64 i,j,k;
memset(ww.mat,,sizeof(ww.mat));
for(i=;i<=;i++)
for(k=;k<=;k++)
if(cur.mat[i][k])
{
for(j=;j<=;j++)
if(now.mat[k][j])
{
ww.mat[i][j]+=cur.mat[i][k]*now.mat[k][j];
if(ww.mat[i][j]>=)
ww.mat[i][j]%=;
}
}
return ww;
}
void power_sum2(__int64 n)
{
__int64 sum=;
make_first(&M_tom);
while(n)
{
if(n&)
{
M_tom=cheng(M_tom,M_hxl);
}
n=n>>;
M_hxl=cheng(M_hxl,M_hxl);
}
sum=sum+*M_tom.mat[][]+M_tom.mat[][]+M_tom.mat[][]+M_tom.mat[][];
if(sum>=)
sum=sum%;
printf("%I64d\n",sum); } int main()
{
__int64 n,x,y;
while(scanf("%I64d%I64d%I64d",&n,&x,&y)>)
{
x=x%;//防止溢出
y=y%;//防止溢出
memset(M_hxl.mat,,sizeof(M_hxl.mat));
make_init(x,y);
power_sum2(n-);
}
return ;
}

HDU 3306 Another kind of Fibonacci ---构造矩阵***的更多相关文章

  1. hdu 3306 Another kind of Fibonacci(矩阵高速幂)

    Another kind of Fibonacci                                                        Time Limit: 3000/10 ...

  2. HDU 3306 Another kind of Fibonacci(快速幂矩阵)

    题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <s ...

  3. HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)

    Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...

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

  5. hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)

    题意:有一个递推式f(x) 当 x < 10    f(x) = x.当 x >= 10  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...

  6. HDU 3306 - Another kind of Fibonacci

    给你 A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2). 求 S(N) = A(0) 2 +A(1) 2+……+ ...

  7. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  8. HDU - 1588 Gauss Fibonacci (矩阵高速幂+二分求等比数列和)

    Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very cle ...

  9. HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...

随机推荐

  1. Pangolin库的使用

    使用Pangolin画出相机的轨迹(包括朝向). 数据集结构data.csv: #timestamp, p_RS_R_x [m], p_RS_R_y [m], p_RS_R_z [m], q_RS_w ...

  2. linux进程查看及管理的工具

     介绍Linux进程查看及管理的工具:pstree, ps, pidof, pgrep, top, htop, glance, pmap, vmstat, dstat, kill, pkill, jo ...

  3. 【ASP.NET Core MVC 入门须知】Net Core和Net Framework 的区别

     1.简单介绍 从上面图中我们可以看到.net  主要分为三个部分 .net FrameWork,.net Core ,Xamarin XAMARIN  主要用来构建APP的主要用的是C#语言 .NE ...

  4. npm安装包很慢

    每次安装时: 可以通过指定 --registry,指向国内镜像服务器地址来加快安装速度. npm install -gd express --registry=http://registry.npm. ...

  5. C++基础知识-inline、const、mutable、this、static

    一.在类定义中实现成员函数inline 类内的成员函实现其实也叫作类内的成员函数定义. 这种直接在类的定义中实现的函数,会被当做inline内联函数来处理. 二.成员函数末尾的const const: ...

  6. 手动抠下的wordpress登录页面样式

    CSS文件 login.css body, html { height: 100%; margin: 0; padding: 0; } html[Attributes Style] { -webkit ...

  7. 对 ArrayList 进行分页.

    /** * 测试分页 */ @Test public void testPage() { int bulkSize = 2; List<Integer> dataList = new Ar ...

  8. 关于npm run dev报错npm ERR! missing script: dev

    出现这个问题应当重新使用 vue init webpack 来初始化工程. 在执行 npm run dev 就可以执行了.

  9. 开源一个C# Class实现Openfire登陆、推出、消息发送,方便其他系统集成IM功能了

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. Visual Studio 跨平台開發實戰(3) - Xamarin iOS 多頁面應用程式開發 (转帖)

    前言 在前一篇教學中, 我們學會如何使用Visual Studio 搭配Xcode 進行iOS基本控制項的操作. 但都是屬於單一畫面的應用程式. 這次我們要來練習如何透過Navigation Cont ...