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. sprintf与strcat

    sprintf可以将整数转化为字符串,也可以连接两个字符串.但是用sprintf在连接两个字符串时,容易出现错误. 因此连接两个字符串时候用strcat, 将整数转化为字符串时候用sprintf. 转 ...

  2. centos networkmanager 和 network配置冲突

    1.由于NetworkManager与 network 有冲突,所以要把NetworkManager关掉

  3. 字符串模式匹配算法1 - BF和KMP算法

    在字符串S中定位/查找某个子字符串P的操作,通常称为字符串的模式匹配,其中P称为模式串.模式匹配有多种算法,这里先总结一下BF算法和KMP算法. 注意:本文在讨论字符位置/指针/下标时,全部使用C语法 ...

  4. Azure Powershell部署使用平台映像的托管Windows VM及相关问题说明

    1.脚本背景信息: a.使用平台镜像(Windows Server 2016 zh-cn)部署高性能托管磁盘虚拟机 b.虚拟机默认不开启Boot诊断 c.添加三块已经创建好的数据磁盘 d.添加已创建好 ...

  5. 代码 | 自适应大邻域搜索系列之(2) - ALNS算法主逻辑结构解析

    00 前言 在上一篇推文中,教大家利用了ALNS的lib库求解了一个TSP问题作为实例.不知道你萌把代码跑起来了没有.那么,今天咱们再接再厉.跑完代码以后,小编再给大家深入讲解具体的代码内容.大家快去 ...

  6. P5242 [USACO19FEB]Cow Dating

    题目链接 题意分析 首先我们可以得出计算公式 \[s_i=\prod_{k=1}^i(1-p_k)\] \[f_i=\sum_{k=1}^i\frac{p_k}{1-p_k}\] 那么 \[ans(i ...

  7. 线性表中顺序表的的理解和实现(java)

    线性表的顺序表示指的是用一组地址连续的存储单元以此存储线性表的数据元素,这种表示也称作线性表的顺序存储结构或顺序映像.通常,称这种存储结构的线性表为顺序表.特点是:逻辑上相邻的数据元素,其物理次序上也 ...

  8. Linux(ubuntu18.04)切换python版本

    前言 Ubuntu18.04系统在安装python时会安装两个版本:2.7和3.6.默认情况下系统环境使用的是python2,但是我们有时需要使用python3来作为我们的开发环境,所以需要自由切换p ...

  9. 无比迅速敏捷地开发iOS超精美控件

    目录 前言 设计 编码 PaintCode 前言 自从人生第一篇博客<iOS中的预编译指令的初步探究>问世以来 浏览量竟然达到了360多,(路过的大神勿笑!)这些浏览量使我兴奋异常但又令我 ...

  10. TX2 安装v4l

    在TX2上使用v4l2-ctl --all -d /dev/video0查看相机参数时报错: v4l2-ctl :command not found 手动安装: sudo apt-get instal ...