zoukankan      html  css  js  c++  java
  • HDOJ 1588 Gauss Fibonacci

    /*******************************************************************************************
    首先我们看g=k*i+b; 是一个等差数列
    如果能推出f(g)这个函数也是一个等差或者等比数列,就可以得出一个公式
    f(k*i+b)  0<=i<n
    建立一个二分矩阵A=[1 1,1 0]
    f(b)=A^b  
    则:
    f(b)=A^b
    f(k+b)=A^k+b
    f(2*k+b)=A^2*k+b.
    .
    .
    f((n-1)*k+b)=A^(n-1)*k+b
    我们就可以得出一个等比数列:
    首项:A^b
    公比:A^k
    项数:n
    (res是单位矩阵)
    运用等比数列求和公式得出:sum=A^b*(res+A^k+(A^k)^2+(A^k)^3+...+(A^k)^(n-1))
    需要注意的一点是:当b=0的情况
    ***************************************************************************************/
    例如:A+A^2+A^3+A^4+A^5+A^6=(A+A^2+A^3)+A^3*(A+A^2+A^3)  用递归写
    ***************************************************************************************/

    Gauss Fibonacci

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1657    Accepted Submission(s): 722


    Problem 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 "Problem GF" told by Angel is actually "Gauss Fibonacci".
    As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.

    Arithmetic progression:
    g(i)=k*i+b;
    We assume k and b are both non-nagetive integers.

    Fibonacci Numbers:
    f(0)=0
    f(1)=1
    f(n)=f(n-1)+f(n-2) (n>=2)

    The Gauss Fibonacci problem is described as follows:
    Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
    The answer may be very large, so you should divide this answer by M and just output the remainder instead.
     

    Input
    The input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M
    Each of them will not exceed 1,000,000,000.
     

    Output
    For each line input, out the value described above.
     

    Sample Input
    2 1 4 100
    2 0 4 100
     

    Sample Output
    21
    12
     

    Author
    DYGG
     

    Source
     

    Recommend
    linle
     


    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    long long int k,b,n,MOD;

    struct Matrix
    {
        long long m[3][3];
    };

    Matrix res;

    Matrix mut(Matrix a,Matrix b)
    {
        Matrix c;
        memset(c.m,0,sizeof(c.m));
        for(int i=1;i<=2;i++)
        {
            for(int j=1;j<=2;j++)
            {
                for(int k=1;k<=2;k++)
                {
                    c.m[j]=((a.m[k]*b.m[k][j])%MOD+c.m[j])%MOD;
                }
            }
        }
        return c;
    }

    Matrix quickpow(Matrix a,int k)
    {
        Matrix res;
        memset(res.m,0,sizeof(res.m));
        res.m[1][1]=res.m[2][2]=1;
        while(k>1)
        {
            if(k&1)
            {
                k--;
                res=mut(res,a);
            }
            else
            {
                k/=2;
                a=mut(a,a);
            }
        }
        return mut(a,res);
    }

    Matrix add(Matrix a,Matrix b)
    {
        Matrix c;
        for(int i=1;i<=2;i++)
        {
            for(int j=1;j<=2;j++)
                c.m[j]=(a.m[j]+b.m[j])%MOD;
        }
        return c;
    }

    Matrix sum(Matrix p,int k)
    {
       if(k==1) return p;
       else if(k&1)  return (add(quickpow(p,k),sum(p,k-1)));
       else return (mut(sum(p,k/2),add(res,quickpow(p,k/2))));
    }

    int main()
    {
    Matrix a,ak,ab,ans;
    a.m[1][1]=a.m[1][2]=a.m[2][1]=1;
    a.m[2][2]=0;

    res.m[1][1]=res.m[2][2]=1;
    res.m[1][2]=res.m[2][1]=0;

    while(cin>>k>>b>>n>>MOD)
    {
       ak=quickpow(a,k);
       if(b!=0)
       ab=quickpow(a,b);
       else
       ab=res;//0次幂当然是单位阵了
       ans=add(res,sum(ak,n-1));
       ans=mut(ab,ans);
       printf("%I64d\n",ans.m[2][1]);
    }

        return 0;
    }


  • 相关阅读:
    进制转换
    体验mssql-cli
    从Windows迁移SQL Server到Linux
    CentOS7脱机安装SQL Server 2017
    基础知识:数据类型优先级
    SQL Server 2016正式版安装(超多图)
    制造高CPU使用率的简单方法
    SQL Server启动的几种方法
    SQL Server 2016 RC0 安装(超多图)
    机器学习:Python实现单层Rosenblatt感知器
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351024.html
Copyright © 2011-2022 走看看