残(矩阵快速幂)

定义f(x)为斐波那契数列第x项

输出f(f(x))%1000000007;

x<=10^100;

思路:

  矩阵快速幂+各种神奇的模;

来,上代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

#define mod  1000000007LL
#define mod2 2000000016LL
#define mod3 6000000048LL

using namespace std;

struct MatrixType {
    long long c[3][3];
};

long long dis[95];

char Cget;

inline void in(long long &now)
{
    now=0,Cget=getchar();
    while(Cget>‘9‘||Cget<‘0‘) Cget=getchar();
    while(Cget>=‘0‘&&Cget<=‘9‘)
    {
        now=now*10+Cget-‘0‘;
        Cget=getchar();
    }
}

long long f1(long long mi)
{
    long long now=1;
    struct MatrixType res,mul;
//    mul.sta(),res.st();
    mul.c[1][1]=mul.c[1][2]=mul.c[2][1]=res.c[1][2]=1;
    mul.c[2][2]=res.c[1][1]=res.c[2][1]=res.c[2][2]=0;
    while(now<=mi)
    {
        if(now&mi)
        {
            struct MatrixType pos;
            pos.c[1][1]=0;
            pos.c[1][2]=0;
            pos.c[2][2]=0;
            pos.c[2][1]=0;
            for(int i=1;i<=2;i++)
            {
                for(int j=1;j<=2;j++)
                {
                    for(int v=1;v<=2;v++)
                    {
                        pos.c[i][j]=(pos.c[i][j]+res.c[i][v]*mul.c[v][j]%mod2)%mod2;
                    }
                }
            }
            res=pos;
        }
        struct MatrixType poxs;
        poxs.c[1][1]=0;
        poxs.c[1][2]=0;
        poxs.c[2][2]=0;
        poxs.c[2][1]=0;
        for(int i=1;i<=2;i++)
        {
            for(int j=1;j<=2;j++)
            {
                for(int v=1;v<=2;v++)
                {
                    poxs.c[i][j]=(poxs.c[i][j]+mul.c[i][v]*mul.c[v][j]%mod2)%mod2;
                }
            }
        }
        mul=poxs,now<<=1;
    }
    return res.c[1][1];
}

long long f(long long mi)
{
    long long now=1;
    struct MatrixType res,mul;
//    mul.sta(),res.st();
    mul.c[1][1]=mul.c[1][2]=mul.c[2][1]=res.c[1][2]=1;
    mul.c[2][2]=res.c[1][1]=res.c[2][1]=res.c[2][2]=0;
    while(now<=mi)
    {
        if(now&mi)
        {
            struct MatrixType pos;
            pos.c[1][1]=0;
            pos.c[1][2]=0;
            pos.c[2][2]=0;
            pos.c[2][1]=0;
            for(int i=1;i<=2;i++)
            {
                for(int j=1;j<=2;j++)
                {
                    for(int v=1;v<=2;v++)
                    {
                        pos.c[i][j]=(pos.c[i][j]+res.c[i][v]*mul.c[v][j]%mod)%mod;
                    }
                }
            }
            res=pos;
        }
        struct MatrixType poxs;
        poxs.c[1][1]=0;
        poxs.c[1][2]=0;
        poxs.c[2][2]=0;
        poxs.c[2][1]=0;
        for(int i=1;i<=2;i++)
        {
            for(int j=1;j<=2;j++)
            {
                for(int v=1;v<=2;v++)
                {
                    poxs.c[i][j]=(poxs.c[i][j]+mul.c[i][v]*mul.c[v][j]%mod)%mod;
                }
            }
        }
        mul=poxs,now<<=1;
    }
    return res.c[1][1];
}

int main()
{
    freopen("na.in","r",stdin);
    freopen("na.out","w",stdout);
    long long t,len;
    in(t);char ch[401];
//    dis[1]=1;
//    for(int i=2;i<=90;i++) dis[i]=dis[i-1]+dis[i-2];
    while(t--)
    {
        long long x=0;
        scanf("%s",ch),len=strlen(ch);
        for(int i=len-1;i>=0;i--) x=(x*10+ch[i]-‘0‘)%mod3;
        cout<<f(f1(x))%mod<<endl;
    }
    fclose(stdin),fclose(stdout);
    return 0;
}
时间: 2024-08-24 21:19:30

残(矩阵快速幂)的相关文章

矩阵快速幂专题(一)

最近闲来无事,准备集中精力刷一波数论与图论.矩阵快速幂是数论里面的重要组成部分,值得我好好学习一下.因为题目比较多,分析也比较多,所以将此专题分成几个部分.做完这一专题,可能会暂时转向图论部分,然后等我组合数学学得差不多了,再回过头来继续做数论题. 矩阵快速幂算法的核心思想是将问题建模转化为数学模型(有一些简单题目是裸的矩阵模型,但是大部分难题就是难在要构造矩阵,用矩阵方法解决问题),推倒递推式,构造计算矩阵,用快速幂的思想求解矩阵A的n次方取mod,从而得到矩阵里面你需要的数据. 矩阵快速幂问

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: 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); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

矩阵快速幂 模板与简单讲解

模板 快速幂模板 1 void solve(matrix t,long long o) 2 { 3 matrix e; 4 5 memset(e.a,0,sizeof(e.a)); 6 7 for (int i = 0;i < d;i++) 8 e.a[i][i] = 1; 9 10 while (o) 11 { 12 if (o & 1) 13 { 14 e = mul(e,t); 15 } 16 17 o >>= 1; 18 19 t = mul(t,t); 20 } 21

233 Matrix 矩阵快速幂

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333...