zoukankan      html  css  js  c++  java
  • 【CODEVS3332】数列

    Description

    a[1]=a[2]=a[3]=1

    a[x]=a[x-3]+a[x-1]  (x>3)

    求a数列的第n项对1000000007(109+7)取余的值。

    Input

    第一行一个整数T,表示询问个数。

    以下T行,每行一个正整数n。

    Output

    每行输出一个非负整数表示答案

    Sample Input

    3

    6

    8

    10

    Sample Output

    4

    9

    19

    HINT

    对于30%的数据 n<=100;

    对于60%的数据 n<=2*10^7;

    对于100%的数据 T<=100,n<=2*10^9;

    #include<iostream>
    #include<cstring>
    using namespace std;
    void mul(long long a[3][3],long long b[3][3])
    {
        long long c[3][3];
        memset(c,0,sizeof(c));
        for (int i=0;i<3;i++)
            for (int j=0;j<3;j++)
                for (int k=0;k<3;k++)
                    c[i][j] = (c[i][j]+a[i][k]*b[k][j]) % 1000000007;
        memcpy(a,c,sizeof(c));
    }
    int main()
    {
        int T,x;
        cin>>T;
        for (int i=1;i<=T;i++)
        {
            long long a[3][3]={{0,0,1},{1,0,0},{0,1,1}};
            long long f[3][3]={{1,1,1},{0,0,0},{0,0,0}};
            cin>>x;
            x-=1; //因为第一个数存在了,所以计算次数-1 
            while (x>0)
            {
                if (x&1) mul(f,a);
                mul(a,a);
                x>>=1;
            }
            cout<<f[0][0]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    笔记
    Dwarf Tower
    第k小数
    np
    noi 抓住那头牛
    HDU 1575 Try A
    acm 易错警示
    E
    魔改森林 题解(dp+容斥+组合数学)
    F. Unusual Matrix 题解(思维)
  • 原文地址:https://www.cnblogs.com/liumengyue/p/5201136.html
Copyright © 2011-2022 走看看