码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2256 Problem of Precision(矩阵快速幂)

时间:2018-09-30 23:21:03      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:amp   就会   n+1   ==   print   mod   hdu   color   while   

题目:就是求(sqrt(2)+sqrt(3))^(2*n)向下取整然后在MOD1024

思路:这个题挺有意思,但是这个题我觉得只能这样了,因为这个题可以做是因为这个题目限制的很死,我们把(sqrt(2)+sqrt(3))转化为(5+2*sqrt(6))^n

设Sn=An+bn,An=(5+2*sqrt(6))^n,Bn=(5-2*sqrt(6))^n,可以发现Bn是小于1的,那我们最后的答案就是Sn-1取模

然后我们构造Sn*((5+2*sqrt(6))+(5-2*sqrt(6))),继续化简就会得到Sn*10=Sn+1-Sn-1,然后矩阵快速幂就可以 了

代码:

#include <cstdio>
using namespace std;
typedef long long LL;

const int MOD=1024;
///使用前要先对r,c赋值
struct mat{
    long long a[30][30];
    int r,c;
    mat operator *(const mat &b)const{
        mat ret;
        for (int i=0;i<r;i++){
            for (int j=0;j<b.c;j++){
                ret.a[i][j]=0;
                for (int k=0;k<c;k++)
                    ret.a[i][j]+=a[i][k]*b.a[k][j],ret.a[i][j]%=MOD;
            }
        }
        ret.r=r;
        ret.c=b.c;
        return ret;
    }
    void init_unit(int x)
    {
        r=c=x;
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(i==j)a[i][j]=1;
                else a[i][j]=0;
            }
        }
    }
}unit;
mat qmod(mat p,LL n){
    unit.init_unit(p.c);
    mat ans=unit;
    while(n){
        if(n&1)ans=p*ans;
        p=p*p;
        n>>=1;
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        mat A;
        A.r=2;A.c=2;
        A.a[0][0]=10;A.a[0][1]=-1;
        A.a[1][0]=1;A.a[1][1]=0;
        mat B;
        B.r=B.c=2;
        B.a[0][0]=10;
        B.a[1][0]=2;
        mat ans;
        ans.r=ans.c=2;
        ans=qmod(A,n-1);
        ans=ans*B;
        printf("%lld\n",(ans.a[0][0]+MOD-1)%MOD);
    }
    return 0;
}

 

HDU 2256 Problem of Precision(矩阵快速幂)

标签:amp   就会   n+1   ==   print   mod   hdu   color   while   

原文地址:https://www.cnblogs.com/lalalatianlalu/p/9733619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!