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. 洛谷P5279 [ZJOI2019]麻将(乱搞+概率期望)

    题面 传送门 题解 看着题解里一堆巨巨熟练地用着专业用语本萌新表示啥都看不懂啊--顺便\(orz\)余奶奶 我们先考虑给你一堆牌,如何判断能否胡牌 我们按花色大小排序,设\(dp_{0/1,i,j,k ...

  2. robot framework-tags(标签)实例

    robot framework的标签是一个简单而又强大的分类机制,功能如下: 标签在reports,logs以及测试数据中展示,显示关于测试用例的元数据信息 用例的执行统计(total,passed, ...

  3. 架构师养成记--25.linux用户管理

    用户管理配置文件用户信息文件:/etc/passwd密码文件:/etc/shadow用户配置文件:/etc/login.defs /etc/default/useradd新用户信息文件:/etc/sk ...

  4. whdxlib

    1 数据库系统实现 实 验 指 导 书 齐心 彭彬 计算机工程与软件实验中心 2016 年 3 月2目 录实验一.JDBC 应用程序设计(2 学时) ......................... ...

  5. 区间DP的学习(持续更新)

    例题: 1.Multiplication Puzzle 原题地址:http://poj.org/problem?id=1651 2.Dire Wolf 原题地址:http://acm.split.hd ...

  6. windows mobile ,wince 系统,用代码启动cab文件安装

    有时候需要用代码来启动安装cab,以下是代码.不能实现静默安装. 启动后会提示用户是否安装,需要用户点击是才行. using System; using System.Collections.Gene ...

  7. (转)MySQL出现同步延迟有哪些原因?如何解决?

    http://oldboy.blog.51cto.com/2561410/1682147----MySQL出现同步延迟有哪些原因?如何解决? 原文:http://www.zjian.me/mysql/ ...

  8. eclipse修改Properties资源文件的默认编码

    在eclipse下,打开window-->preferences-->general-->content Types-->java Properties File 将其编码方式 ...

  9. Freemarker不显示对象的属性

    Freemarker不显示对象的属性 今天使用Freemarker在springboot项目中通过模板生成一些html文件.但是发现没有显示对象的属性. 找了很长时间,终于发现不显示对象的属性可能是两 ...

  10. python笔记01-----列表操作

    在python中列表用 '[]' 表示 列表的查询操作 列表的切片 names = ["a","b","c"]             #定 ...