zoukankan      html  css  js  c++  java
  • 1250 Fibonacci数列(矩阵乘法)

    1250 Fibonacci数列
    时间限制: 1 s
    空间限制: 128000 KB
    题目等级 : 钻石 Diamond
    题目描述 Description
    定义:f0=f1=1, fn=fn-1+fn-2(n>=2)。{fi}称为Fibonacci数列。
    输入n,求fn mod q。其中1<=q<=30000。
    输入描述 Input Description
    第一行一个数T(1<=T<=10000)。
    以下T行,每行两个数,n,q(n<=109, 1<=q<=30000)
    输出描述 Output Description
    文件包含T行,每行对应一个答案。
    样例输入 Sample Input
    3
    6 2
    7 3
    7 11
    样例输出 Sample Output
    1
    0
    10
    数据范围及提示 Data Size & Hint
    1<=T<=10000
    n<=109, 1<=q<=30000
    分类标签 Tags
    矩阵乘法 数论

    /*
    矩阵乘法快速幂.
    */
    #include<iostream>
    #include<cstdio>
    #define MAXN 3
    #define LL long long
    using namespace std;
    LL p,q,a1,a2,n,m;
    LL a[MAXN][MAXN],ans[MAXN][MAXN],c[MAXN][MAXN],b[MAXN][MAXN];
    LL read()
    {
        LL x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
        return x*f;
    }
    void mi(int n)
    {
        while(n)
        {
            if(n&1)
            {
                for(int i=1;i<=2;i++)
                  for(int j=1;j<=2;j++)
                    for(int k=1;k<=2;k++)
                    c[i][j]=(c[i][j]+ans[i][k]*b[k][j]%m)%m;
                for(int i=1;i<=2;i++)
                  for(int j=1;j<=2;j++)
                    ans[i][j]=c[i][j],c[i][j]=0;
            }
            for(int i=1;i<=2;i++)
              for(int j=1;j<=2;j++)
                for(int k=1;k<=2;k++)
                  c[i][j]=(c[i][j]+b[i][k]*b[k][j]%m)%m;
            for(int i=1;i<=2;i++)
              for(int j=1;j<=2;j++)
                b[i][j]=c[i][j],c[i][j]=0;
            n>>=1;
        }
    }
    void slove()
    {
        a[1][1]=1,a[1][2]=2;
        b[1][2]=ans[1][2]=1,b[2][1]=ans[2][1]=1;
        b[1][1]=ans[1][1]=0;
        b[2][2]=ans[2][2]=1;
        mi(n);
        printf("%d
    ",(ans[1][1]+ans[2][1])%m);
    }
    int main()
    {
        int t;
        t=read();
        while(t--)
        {
            n=read();m=read();
            n--;
            slove();
        }
        return 0;
    }
  • 相关阅读:
    Day1-while and for/break and continue
    Day1-用户输入及字符串格式化输入
    Day1-python基础
    2-21-源码编译搭建LNMP环境
    2-20-使用apache搭建web网站
    2-19-mysql优化
    2-18-搭建mysql集群实现高可用
    2-17-MySQL读写分离-mysql-proxy
    2-16-2MySQL主从
    2-14-存储过程-触发器-事务
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068215.html
Copyright © 2011-2022 走看看