hdu 3326 Another kind of Fibonacci (矩阵构造)

题目大意:

描述了另外一种斐波那契

F[n] = x*F[n-1] + y*F[n-2];

求segma(F[i]^2);

思路分析:

构造矩阵的详细 请戳我

构造矩阵可以得到

中间矩阵为

1 1 0
0

0 x^2      y^2   2*x*y

0 1
0 0

0 x
0 y

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define N 30
typedef long long LL;
using namespace std;
const LL mod = 10007;

struct matrix
{
    int a[4][4];
}origin;

matrix multiply(matrix x,matrix y)
{
    matrix temp;
    memset(temp.a,0,sizeof(temp.a));
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<4;k++)
            {
                temp.a[i][j]+=x.a[i][k]*y.a[k][j];
                temp.a[i][j]=(temp.a[i][j])%mod;
            }
        }
    }
    return temp;
}

matrix matmod(matrix a,LL k)
{
    matrix res;

    memset(res.a,0,sizeof res.a);
    for(int i=0;i<4;i++)res.a[i][i]=1;

    while(k)
    {
        if(k&1)
        res=multiply(res,a);
        k>>=1;
        a=multiply(a,a);
    }
    return res;
}

int main()
{
    int  n,x,y;
    while(scanf("%d%d%d",&n,&x,&y)!=EOF)
    {
        matrix st;

        x%=mod;
        y%=mod;

        st.a[0][0]=st.a[0][1]=1;
        st.a[0][2]=st.a[0][3]=0;

        st.a[1][0]=0;
        st.a[1][1]=(x*x)%mod;
        st.a[1][2]=(y*y)%mod;
        st.a[1][3]=(2*x*y)%mod;

        st.a[2][0]=st.a[2][2]=st.a[2][3]=0;
        st.a[2][1]=1;

        st.a[3][0]=st.a[3][2]=0;
        st.a[3][1]=x%mod;
        st.a[3][3]=y%mod;

        matrix end = matmod(st,n);

        int ans=0;

        for(int i=0;i<4;i++)
        {
            ans+=end.a[0][i];
            ans%=mod;
        }
        printf("%d\n",ans%mod);
    }
    return 0;
}

hdu 3326 Another kind of Fibonacci (矩阵构造),布布扣,bubuko.com

时间: 2024-08-04 15:38:23

hdu 3326 Another kind of Fibonacci (矩阵构造)的相关文章

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 3306 Another kind of Fibonacci(快速幂矩阵)

题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MOD 10007 #defin

矩阵十题【四】 HDU 3306 Another kind of Fibonacci

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3306 题目大意:A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2):给定三个值N,X,Y求S(N):S(N) = A(0)^2 +A(1)^2+--+A(n)^2. 学了这几题,还是不太很懂,后来看题解,渐渐也是懂了一点. 题目的意思是求出A(0)^2 +A(1)^2+--+A(n)^2 考虑1*4 的矩阵[s[n-2]

HDU - 1757 A Simple Math Problem (构造矩阵)

Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); And ai(0<=i<=9) can only be 0 or 1 . Now, I will give a0 ~ a9 and two positive in

hdu 1588(Fibonacci矩阵求和)

题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a,所以 f(g(i))= f(k*i+b)= (A^(k*i+b-1)).a,i从 0取到 n-1,取出公因式 A^(b-1)(因为矩阵满足分配率),然后所求结果可化为 A^(b-1) * (A^0 + A^k + A^2k +....+ A^(n-1)k),化到这里后难点就是求和了,一开始我尝试暴力

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) =

简单数论之矩阵构造

其实矩阵构造就是对公式的化简,最后运用矩阵快速幂求值 下面来看一题 Everybody knows Fibonacci numbers, now we are talking about the Tribonacci numbers: T[0] = T[1] = T[2] = 1; T[n] = T[n - 1] + T[n - 2] + T[n - 3] (n >= 3)Given a and b, you are asked to calculate the sum from the ath

HDU 4965 Fast Matrix Calculation 【矩阵】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4965 题目大意:给你一个N*K的矩阵A以及一个K*N的矩阵B (4 <= N <= 1000)以及 (2 <=K <= 6),然后接下来四步: 算一个新的矩阵C=A*B 算M=C^ (N*N) 对于M中的每个元素%6 将M中每个元素加起来,算出和. 也就是求出A*B * A*B * A*B * A*B * A*B *--* A*B   但是A*B形成的矩阵是N*N,而N大小有可能是10

HDU 5171 GTY&#39;s birthday gift 矩阵快速幂

点击打开链接 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 78 Problem Description FFZ's birthday is coming. GTY wants to give a gift to ZZF. He as