hdu3306--Another kind of Fibonacci(矩阵快速幂)

Another kind of Fibonacci

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status

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 <= 2 31 ? 1

X : 2<= X <= 2 31? 1

Y : 2<= Y <= 2 31 ? 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 

Sn = S(n-1) + An^2 =  S(n-1) + X^2A*(n-1)^2 + 2*X*Y*A(n-1)*A(n-2) + Y^2*A(n-2)^2

也就得到Sn由S(n-1) , A(n-1) , A(n-2) , A(n-1)*A(n-2)得到,由此可以转化为矩阵相乘

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
#define MOD 10007
struct node
{
    LL a[5][5] ;
    int n ;
};
node mul(node p,node q)
{
    int i , j , k ;
    node s ;
    for(i = 0 , s.n = p.n ; i < p.n ; i++)
        for(j = 0 ; j < p.n ; j++)
        {
            s.a[i][j] = 0 ;
            for(k = 0 ; k < p.n ; k++)
                s.a[i][j] = ( s.a[i][j] + p.a[i][k] * q.a[k][j] ) % MOD ;
        }
    return s ;
}
node pow(node p,int k)
{
    if( k == 1 )
        return p ;
    node s = pow( p,k/2 ) ;
    s = mul(s,s) ;
    if( k%2 )
        s = mul(s,p) ;
    return s ;
}
int main()
{
    LL n , x , y , i , j , ans ;
    node p , s ;
    while( scanf("%I64d %I64d %I64d", &n, &x, &y) != EOF )
    {
        p.n = 4 ;
        memset(p.a,0,sizeof(p.a)) ;
        p.a[0][0] = 1 ;
        p.a[1][0] = p.a[1][1] = (x*x) % MOD ;
        p.a[2][0] = p.a[2][1] = (2*x*y) % MOD ;
        p.a[3][0] = p.a[3][1] = (y*y) % MOD ;
        p.a[1][2] = x % MOD ;
        p.a[2][2] = y % MOD ;
        p.a[1][3] = 1 ;
        s = pow(p,n-1) ;
        ans = s.a[0][0] * 2 ;
        for(i = 1 ; i < 4 ; i++)
            ans = ( ans + s.a[i][0] ) % MOD ;
        printf("%I64d\n", ans) ;
    }
    return 0;
}
时间: 2024-08-28 20:33:14

hdu3306--Another kind of Fibonacci(矩阵快速幂)的相关文章

poj 3070 Fibonacci (矩阵快速幂求斐波那契数列的第n项)

题意就是用矩阵乘法来求斐波那契数列的第n项的后四位数.如果后四位全为0,则输出0,否则 输出后四位去掉前导0,也...就...是...说...输出Fn%10000. 题目说的如此清楚..我居然还在%和/来找后四位还判断是不是全为0还输出时判断是否为0然后 去掉前导0.o(╯□╰)o 还有矩阵快速幂的幂是0时要特判. P.S:今天下午就想好今天学一下矩阵乘法方面的知识,这题是我的第一道正式接触矩阵乘法的题,欧耶! #include<cstdio> #include<iostream>

poj 3070 Fibonacci 矩阵快速幂

题目链接:http://poj.org/problem?id=3070 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternative formula for t

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

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

poj 3070 Fibonacci (矩阵快速幂乘/模板)

题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const int N=2,M=2,P=2; const int MOD=10000; struct Matrix { ll m[N][N]; }; Matrix

UVA10229Modular Fibonacci(矩阵快速幂)

题目链接 题目大意:给你i和m,求Mi, Mi = (F(i - 1) + F(i - 2)) % 2^m; 解题思路:因为Mi = (F(i - 1) % 2^m + F(i - 2)% 2^m) % 2^m = (M(i - 1) + M(i - 2)) % 2^m.类似于求fibonacci数加上取模,只是n很大,所以要用矩阵快速幂快速求解.参考 代码: #include <cstdio> #include <cstring> typedef long long ll; co

$loj$10222 佳佳的$Fibonacci$ 矩阵快速幂

正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\sum_{i=1}^{n}a_i\cdot(n-i)$这样一个式子是可以在矩阵快速幂中推出来的(类似这个形式的都可,,,就随着编号递增系数递减这样子$QwQ$ 具体来说就是表示成$\sum_{i=1}^{n}\sum_{j=1}^{i}a_j$,就欧克辣(具体实现后面港,,, 但是问题在于,它是$\s

POJ 3070 Fibonacci(矩阵快速幂)

Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, - An alternative formula for the Fibonacci sequence is

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

题目地址:HDU 3306 没什么智商的题目,只要把构造矩阵硬算出来就行. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <m

HDU 1588 Gauss Fibonacci(矩阵快速幂)

题目地址:HDU 1588 用于构造斐波那契的矩阵为 1,1 1,0 设这个矩阵为A. sum=f(b)+f(k+b)+f(2*k+b)+f(3*k+b)+........+f((n-1)*k+b) <=>sum=A^b+A^(k+b)+A^(2*k+b)+A^(3*k+b)+........+A^((n-1)*k+b) <=>sum=A^b+A^b*(A^k+A^2*k+A^3*k+.......+A^((n-1)*k))(1) 设矩阵B为A^k; 那么(1)式为 sum=A^b

POJ3070:Fibonacci(矩阵快速幂模板题)

http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdlib.h> #include <cstdio> #include <algorithm> #define mod 10000 using namespace std; struct m { int a[3][3]; } init,res; int n; m Mult(m x,m