zoukankan      html  css  js  c++  java
  • 矩阵快速幂(共轭函数两种递推式)

    题目链接:https://cn.vjudge.net/contest/261339#problem/B

    AC1:ans= x(n)+y(n)*sqrt(6),所以,ans=x(n)+y(n)*sqrt(6)+(x(n)-y(n)*sqrt(6))-(x(n)-y(n)*sqrt(6))=2*x(n)-(x(n)-y(n)*sqrt(6)),可以推出

    (x(n)-y(n)*sqrt(6))是大于0小于等于1的,所以ans= 2*x(n)-1。

    AC代码1:

    #include<iostream>
    #include<string>
    #include<cstring>
    #include<iomanip>
    #include<stdio.h>
    #include<cmath>
    using namespace std;
    # define mod 1024
    # define ll long long
    struct Matrix
    {
        ll a[5][5];
        Matrix operator *(Matrix t)
        {
            Matrix temp;
            for(ll i=1; i<=2; i++)
            {
                for(ll j=1; j<=2; j++)
                {
                    temp.a[i][j]=0;
                    for(ll k=1; k<=2; k++)
                    {
                        temp.a[i][j]+=((a[i][k]%mod)*(t.a[k][j]%mod)+mod)%mod;
                    }
                }
            }
            return temp;
        }
    };
    ll quickpow(Matrix t,ll t1)
    {
        t1--;
        Matrix temp=t;
        while(t1)
        {
            if(t1&1)temp=temp*t;
            t=t*t;
            t1>>=1;
        }
        ll x=(5*temp.a[1][1]%mod+2*temp.a[2][1]%mod)%mod;
        return (x*2-1)%mod;
    }
    int main()
    {
        ll T;
        scanf("%lld",&T);
        while(T--)
        {
            Matrix temp;
            temp.a[1][1]=5;
            temp.a[1][2]=2;
            temp.a[2][1]=12;
            temp.a[2][2]=5;
            ll n;
            scanf("%lld",&n);
            if(n==1)printf("%lld
    ",9);
            else
            {
                ll ans=quickpow(temp,n-1);
                printf("%lld
    ",ans);
            }
        }
        return 0;
    }
    

    方法二:这个推得方法就和我的上一篇博客的推理方法是一样的了。直接写矩阵 

    {10   -1}

    {1      0}.

    AC代码2:

    #include<iostream>
    #include<string>
    #include<cstring>
    #include<iomanip>
    #include<cmath>
    #include<stdio.h>
    #include<algorithm>
    #include<queue>
    using namespace std;
    # define ll long long
    # define maxn
    # define inf 0x3f3f3f3f
    # define mod 1024
    struct Matrix
    {
        ll a[5][5];
        Matrix operator *(Matrix t)
        {
            Matrix temp;
            for(int i=1; i<=2; i++)
            {
                for(int j=1; j<=2; j++)
                {
                    temp.a[i][j]=0;
                    for(int k=1; k<=2; k++)
                    {
                        temp.a[i][j]+=((a[i][k]%mod)*(t.a[k][j]%mod)+mod)%mod;
                    }
                }
            }
            return temp;
        }
    };
    ll quickpow(Matrix t,ll t1)
    {
        Matrix temp=t;
        t1--;
        while(t1)
        {
            if(t1&1)temp=temp*t;
            t=t*t;
            t1>>=1;
        }
        return (temp.a[1][1]*10%mod+temp.a[1][2]*2%mod+mod)%mod-1;
    }
    int main()
    {
        ll T;
        scanf("%lld",&T);
        while(T--)
        {
            Matrix t;
            t.a[1][1]=10;
            t.a[1][2]=-1;
            t.a[2][1]=1;
            t.a[2][2]=0;
            ll n;
            scanf("%lld",&n);
            if(n==1)printf("%lld
    ",9);
            else
            {
                ll ans=quickpow(t,n-1);
                printf("%lld
    ",ans);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Spring的事务处理机制
    英特尔诺基亚联手研发Symbian系统的智能手机
    国际最新LOGO设计趋势总结
    Java学习笔记_身份验证机制
    用Validator检查你的表单
    软件企业:细节造就竞争力
    优化软件性能的方法
    【开发经验】Struts常见错误及原因分析
    CF547E Mike and Friends
    [ZJOI2015]诸神眷顾的幻想乡
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10262855.html
Copyright © 2011-2022 走看看