HDU3306Another kind of Fibonacci(简单矩阵快速幂)

哎,本来是想学学矩阵构造的方法的,,突然发现自己不用看直接就会yy构造。。。

看下右边有什么。。

题目地址:Another kind of Fibonacci

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int mod=10007;

int p[4][4],a[4][4],tmp[4][4];
int n,x,y,ans[4],res[4];

void init()
{
    int i,j;
    a[0][0]=1,a[0][1]=x*x%mod,a[0][2]=y*y%mod,a[0][3]=2*x*y%mod;
    a[1][0]=0,a[1][1]=x*x%mod,a[1][2]=y*y%mod,a[1][3]=2*x*y%mod;
    a[2][0]=0,a[2][1]=1,a[2][2]=0,a[2][3]=0;
    a[3][0]=0,a[3][1]=x,a[3][2]=0,a[3][3]=y;

    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
            p[i][j]=a[i][j];
}

void cal1()
{
    int i,j,k;
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
        {
            tmp[i][j]=a[i][j];
            a[i][j]=0;
        }

    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
            for(k=0;k<4;k++)
                a[i][j]=(a[i][j]+tmp[i][k]*p[k][j])%mod;
}

void cal2()
{
    int i,j,k;
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
        {
            tmp[i][j]=p[i][j];
            p[i][j]=0;
        }

    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
            for(k=0;k<4;k++)
                p[i][j]=(p[i][j]+tmp[i][k]*tmp[k][j])%mod;
}

int main()
{
    int i,j;
    while(cin>>n>>x>>y)
    {
        x%=mod,y%=mod;
        ans[0]=2;
        ans[1]=ans[2]=ans[3]=1;

        init();
        n-=2;
        while(n)
        {
            if(n&1)
                cal1();
            cal2();
            n>>=1;
        }

        memset(res,0,sizeof(res));
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                res[i]=(res[i]+a[i][j]*ans[j])%mod;
            }
        }

        printf("%d\n",res[0]);
    }
    return 0;
}

HDU3306Another kind of Fibonacci(简单矩阵快速幂)

时间: 2024-11-03 03:24:47

HDU3306Another kind of Fibonacci(简单矩阵快速幂)的相关文章

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

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

Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Prob

hdu 1588 Gauss Fibonacci(矩阵快速幂)

Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2090    Accepted Submission(s): 903 Problem Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r

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

HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,

POJ 3070 Fibonacci(矩阵快速幂)

题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. 1 //3070 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 struct Matrix 9 { 10 int v[2][2]; 11 }; 12 int n; 13 14 Matrix matrix_mul(Matrix a,Matrix b) 1

【POJ 3070】Fibonacci(矩阵快速幂)

[POJ 3070]Fibonacci(矩阵快速幂) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12333   Accepted: 8752 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

UVA - 10229 - Modular Fibonacci (矩阵快速幂 + fibonacci)

题目传送:UVA - 10229 思路:就是简单的矩阵快速幂求fibonacci数列,然后注意可能中间结果会爆int,因为2^19有50多万 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #includ

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 i

POJ3070 Fibonacci(矩阵快速幂)

用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Time :2016/1/19 20:11:43 ************************************************ */ #include <iostream> #include <algorithm> #include <cstring> #in