Another kind of Fibonacci

                                                       Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

                                                                                  Total Submission(s): 1691    Accepted Submission(s): 660

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)=S(n-1)+A(n)^2,A(n)^2=X^2*A(n-1)^2+Y^2*A(n-2)^2+2*X*Y*A(n-1)*A(n-2)。A(n)*A(n-1)=Y*A(n-1)*A(n-2)+X*A(n-1)^2,能够构造例如以下的矩阵。然后用二分矩阵的方法求解就可以。

递推矩阵


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=10007;
struct matrix
{
long long ma[5][5];
};
matrix multi(matrix x,matrix y)//矩阵相乘
{
matrix ans;
memset(ans.ma,0,sizeof(ans.ma));
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(x.ma[i][j])//稀疏矩阵优化
for(int k=1;k<=4;k++)
{
ans.ma[i][k]=(ans.ma[i][k]+(x.ma[i][j]*y.ma[j][k])%mod)%mod;
}
}
}
return ans;
}
matrix pow(matrix a,long long m)
{
matrix ans;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
while(m)
{
if(m&1)
ans=multi(ans,a);
a=multi(a,a);
m=m>>1;
}
return ans;
}
int main()
{
long long x,y,n;
while(~scanf("%I64d%I64d%I64d",&n,&x,&y))
{
matrix a,b;
memset(a.ma,0,sizeof(a.ma));
memset(b.ma,0,sizeof(b.ma));
a.ma[1][1]=1;
a.ma[1][2]=1;
a.ma[2][2]=(x*x)%mod;
a.ma[2][3]=(y*y)%mod;
a.ma[2][4]=(2*x*y)%mod;
a.ma[3][2]=1;
a.ma[4][2]=x;
a.ma[4][4]=y;
b.ma[1][1]=1;
b.ma[2][1]=1;
b.ma[3][1]=1;
b.ma[4][1]=1;
a=pow(a,n);
a=multi(a,b);
printf("%I64d\n",a.ma[1][1]);
}
return 0;
}

hdu 3306 Another kind of Fibonacci(矩阵高速幂)的更多相关文章

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

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

    题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路:  设A = |(1, 1),(1, 0) ...

  3. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

  4. HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

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

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

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

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  7. HDU 2254 奥运(矩阵高速幂+二分等比序列求和)

    HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意:  中问题不解释. 分析:  依据floyd的算法,矩阵的k次方表示这个矩阵走了k步.  所以k ...

  8. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  9. HDU 1575 Tr A(矩阵高速幂)

    题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...

随机推荐

  1. Vue中Mixins使用

    mixins是一种分发Vue组件中可复用功能的一种灵活方式. mixins是一个JavaScript对象,可以包含组件中的任意选项,比如Vue实例中生命周期的各个钩子函数,也可以是data.compo ...

  2. 【Django】Form组件

    目录 Form组件介绍 常用字段与插件 Form组件中所有内置字段 从数据库中获取数据 校验示例 检验手机号是否合法 方式一(基本操作) 方式二(自定义验证规则) 方式三(利用钩子) 验证密码一致性 ...

  3. Android程序使用SOAP调用远程WebService服务

    在现在的应用程序中.不可能不使用到网络,而且后台一定是有server作为支撑的. 现在最常见的调用方式有HTTP的GET .POST方式,返回JSON或者XML数据. 可是也有一种常见的形式就是调用W ...

  4. Android ViewPager嵌套ViewPager滑动冲突处理方法

    dispatchTouchEvent方法用于事件的分发,Android中所有的事件都必须经过这个方法的分发, 然后决定是自身消费当前事件还是继续往下分发给子控件处理.返回true表示不继续分发,事件没 ...

  5. Vue自定义函数挂到全局方法

    方法一:使用Vue.prototype //在mian.js中写入函数 Vue.prototype.getToken = function (){ ... } //在所有组件里可调用函数 this.g ...

  6. 最简单的实体手机测试移动端前端Vue Cli3搭建网站的方法

    手机和PC同用一个路由的情况下,直接在手机的浏览器上输入Ip: 192.168.1.100:8080 就能看到了. 其中192.168.1.100是PC的IP.不同的自己改下就好. 就这么简单.啥都不 ...

  7. BZOJ1503: [NOI2004]郁闷的出纳员(Splay)

    Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经 ...

  8. 【习题 7-7 UVA-12558】Egyptian Fractions (HARD version)

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 枚举最大量maxdep 在dfs里面传剩余的要凑的分子.分母 以及上一次枚举的值是多少. 然后找到最小的k,满足1/ ...

  9. 从数据库中生成XML文件

    前台页面<%@ page language="java" contentType="text/html; charset=utf-8" pageEncod ...

  10. 手撕面试题ThreadLocal!!!

    说明 面试官:讲讲你对ThreadLocal的一些理解. 那么我们该怎么回答呢????你也可以思考下,下面看看零度的思考: ThreadLocal用在什么地方? ThreadLocal一些细节! Th ...