zoukankan      html  css  js  c++  java
  • 矩阵快速幂(纯数学递推)

    Problem of Precision

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


    Problem Description
     
    Input
    The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n. (1 <= n <= 10^9)
     
    Output
    For each input case, you should output the answer in one line.
     
    Sample Input
    3 1 2 5
     
    Sample Output
    9 97 841
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  3117 2254 1588 2294 2971
     
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include <stdio.h>
    #include <queue>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1024
    using namespace std;
    typedef long long ll ;
    struct node{
        int a[2][2];
        node()
        {
            memset(a , 0 , sizeof(a));
        }
    };
    
    node mul(node A , node B)
    {
        node C ;
        for(int i = 0 ; i < 2 ; i ++)
        {
            for(int j = 0 ; j < 2 ; j++)
            {
                for(int k = 0 ; k < 2 ; k++)
                {
                    C.a[i][j] = (C.a[i][j] + A.a[i][k] * B.a[k][j]) % mod;
                }
            }
        }
        return C;
    }
    
    node pow1(node A , int m)
    {
        node ans ;
        for(int i = 0 ; i < 2 ; i++)
            ans.a[i][i] = 1 ;
        while(m)
        {
            if(m&1)
            {
                ans = mul(ans , A);
            }
            m >>= 1 ;
            A = mul(A , A);
        }
        return ans ;
    }
    
    
    
    int main()
    {
        int t ;
        scanf("%d" , &t);
        while(t--)
        {
            int n ;
            scanf("%d" , &n);
            node A ,B ,C ;
            A.a[0][0] = 5 , A.a[0][1] = 12 ;
            A.a[1][0] = 2 , A.a[1][1] = 5 ;
    
            B.a[0][0] = 5 , B.a[1][0] = 2 ;
    
           
            C = mul(pow1(A , n-1) , B);
            printf("%d
    " , (C.a[0][0]*2 - 1)%mod);
    
        }
    
    
        return 0 ;
    }
     
  • 相关阅读:
    Jenkins系列——使用SonarQube进行代码质量检查
    HTTP1.0工作原理
    Jenkins系列——使用checkstyle进行代码规范检查
    Jenkins系列——定时构建
    Hadoop环境搭建
    eclipse3.4+对的处理插件(附SVN插件安装实例)
    MD5
    RedHat6.5更新软件源
    ubuntu软件推荐
    disconf系列【2】——解决zk部署情况为空的问题
  • 原文地址:https://www.cnblogs.com/nonames/p/11360597.html
Copyright © 2011-2022 走看看