hdu 3306 Another kind of Fibonacci

点击此处即可传送hdu 3306

                            **Another kind of Fibonacci**
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2005    Accepted Submission(s): 787

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

题目大意:就是给你三个数 n 和x,y,
f(n) = x*f(n-1) + y*f(n-2);
然后求s(n) = f(0)^2 + f(1)^2 +….+f(n)^2,就是这样了;
解题思路:还是矩阵乘法取余;
直接上代码:

/*
2015 - 8 - 14 下午
Author: ITAK

今天非常非常的不顺心啊。。。。

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 4;
const int mod = 10007;
typedef long long LL;
typedef struct
{
    LL  m[maxn][maxn];
}Matrix;
Matrix P = {1,1,0,0,
            0,0,0,0,
            0,0,0,0,
            0,1,0,0
           };
Matrix I = {1,0,0,0,
            0,1,0,0,
            0,0,1,0,
            0,0,0,1
           };
Matrix matrix_mul(Matrix a, Matrix b)
{
    int i, j, k;
    Matrix c;
    for(i=0; i<maxn; i++)
    {
        for(j=0; j<maxn; j++)
        {
            c.m[i][j] = 0;
            for(k=0; k<maxn; k++)
                c.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod;
            c.m[i][j] %= mod;
        }
    }
    return c;
}

Matrix quick_mod(LL m)
{
    Matrix ans = I, b = P;
    while(m)
    {
        if(m & 1)
            ans = matrix_mul(ans, b);
        m >>= 1;
        b = matrix_mul(b, b);
    }
    return ans;
}
int main()
{
    LL n, x, y;
    while(~scanf("%lld%lld%lld",&n,&x,&y))
    {
        Matrix tmp;
        x %= mod;
        y %= mod;
        P.m[1][1] = (x*x) % mod;
        P.m[1][2] = (2*x*y) % mod;
        P.m[1][3] = (y*y) % mod;
        P.m[2][1] = x;
        P.m[2][2] = y;
        tmp = quick_mod(n);
        LL ans = (tmp.m[0][0]+tmp.m[0][1]+tmp.m[0][2]+tmp.m[0][3])%mod;
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-27 17:45:40

hdu 3306 Another kind of Fibonacci的相关文章

hdu 1250 Hat&amp;#39;s Fibonacci

点击此处即可传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your

hdu 3509 Buge&amp;#39;s Fibonacci Number Problem

点击此处即可传送 hdu 3509 题目大意:F1 = f1, F2 = f2;; F(n) = a*F(n-1) + b*F(n-2); S(n) = F1^k + F2^k +-.+Fn^k; 求S(n) mod m; 解题思路: 1:首先一个难题就是怎么判断矩阵的维数(矩阵的维数是个变量) 解决方法:开一个比较大的数组,然后再用一个公有变量记一下就行了,具体详见代码: 2:k次方,找规律: 具体上代码吧: /* 2015 - 8 - 16 晚上 Author: ITAK 今日的我要超越昨日

HDOJ/HDU 1250 Hat&amp;#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

矩阵快速幂专题【完结】

第一题 hdu 1757 A Simple Math Problem 点击打开链接 思路:矩阵快速幂 分析: 1 最简单的矩阵快速幂的题目,直接利用矩阵求解即可 点击打开查看代码 第二题 hdu 1575 Tr A 点击打开hdu 1575 思路: 矩阵快速幂 分析: 1 题目给定一个n*n的矩阵要求矩阵的k次幂之后的矩阵的对角线的和 2 矩阵快速幂的裸题 点击打开查看代码 第三题 hdu 2604 Queuing 点击打开hdu 2604 思路: 递推+矩阵快速幂 分析; 1 根据题目的意思,

hdu 1588 Gauss Fibonacci

点击此处即可传送hdu 1588 **Gauss Fibonacci** Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2849 Accepted Submission(s): 1179 Problem Description Without expecting, Angel replied quickly.She says: "I'v h

hdu 3117 Fibonacci Numbers

点击此处即可传送到hdu 3117 **Fibonacci Numbers** Problem Description The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively

hdu 1568 Fibonacci

点击此处即可传送hdu 1568 **Fibonacci** Problem Description 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来. 接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了.所以规定超过4位的只要说出前4位就可以了,可是CodeStar自己又记不住.于

hdu 1021 Fibonacci Again

点击此处即可传送hdu 1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44439 Accepted Submission(s): 21214 Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1)

【HDU 4786 Fibonacci Tree】最小生成树

一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中的一个. 求最小生成树和最大生成树,得到生成树边权和的下界left和上界right.这道题由于每条边权值不是0就是1,因此生成树边权和可以覆盖到[left,right]的每一个数.那么求得[left,right],看是否有fibonacci数在区间内就可以了. 1 #include <cstdio>