zoukankan      html  css  js  c++  java
  • HDU 2256 Problem of Precision (矩阵乘法)

    Problem of Precision

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


    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
     
     
    杭电 hdu 2256 Problem of Precision - fwhjyhp - ☆漫步云端☆的博客
     
     

     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int mod=1024;
    
    struct Matrix{
        int arr[2][2];
    };
    
    Matrix unit,init;
    
    Matrix Mul(Matrix a,Matrix b){
        Matrix c;
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++){
                c.arr[i][j]=0;
                for(int k=0;k<2;k++)
                    c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j]%mod)%mod;
                c.arr[i][j]%=mod;
            }
        return c;
    }
    
    Matrix Pow(Matrix a,Matrix b,int k){
        while(k){
            if(k&1){
                b=Mul(b,a);
            }
            a=Mul(a,a);
            k>>=1;
        }
        return b;
    }
    
    void Init(){
        unit.arr[0][0]=5,   unit.arr[0][1]=2,   unit.arr[1][0]=unit.arr[1][1]=0;
    
        init.arr[0][0]=init.arr[1][1]=5,    init.arr[0][1]=2,   init.arr[1][0]=12;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n;
        Init();
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            Matrix res=Pow(init,unit,n-1);
            int ans=(2*res.arr[0][0]-1)%mod;    //注意这里,刚开始这里没有%mod,WA了好几次
            printf("%d
    ",ans);
        }
        return 0;
    }
     
  • 相关阅读:
    14071702(SkeletalControl_Limb)
    14072202(带IK的Recoil)
    UE3植被工具-支持刷Actor)
    UDK:AdventureKit 攀爬系统
    16082402(SkeletalMesh的绘制流程)
    windows下安装nodejs
    laravel迁移文件
    laravel的资源路由resource
    sleep参数是整型还是小数
    node.js和npm的关系
  • 原文地址:https://www.cnblogs.com/jackge/p/3146233.html
Copyright © 2011-2022 走看看