zoukankan      html  css  js  c++  java
  • HDU-6030 Happy Necklace 打表+矩阵快速幂

    Happy Necklace

    前天个人赛规律都找出来了,n的范围是(10^{18}),我一想GG,肯定是矩阵快速幂,然后就放弃了。

    昨天学了一下矩阵快速幂。

    题意

    现在小Q要为他的女朋友一个有n个宝石的好的项链(直线),定义好的项链为:如果任意素数长的子串中蓝宝石的数量不小于红宝石的数量就是好的。小Q可以买多少种好的项链呢?

    思路

    n这么大,绝对是规律题。

    先简单分析一下,其实对于好项链的定义可以化简为任意长度为3的子串中至少有两个蓝宝石,然后打表。

    #include<bits/stdc++.h>
    using namespace std;
    int arr[100],pre[100];
    int check(int cnt)
    {
        for(int i =1; i<=cnt; i++)
        {
            pre[i]=pre[i-1]+(arr[i]==1);
            if(i>=3&&(pre[i]-pre[i-3])<2)
                return 0;
        }
        return 1;
    }
    int main()
    {
        for(int i=3; i<=20; i++)
        {
            int ans=0;
            for(int j=0; j<(1<<i); j++)
            {
                for(int k=1; k<=i; k++)
                    arr[k]=0;
                int tmp=j,cnt=0;
                while(tmp)
                {
                    arr[++cnt]=tmp%2;
                    tmp/=2;
                }
                if(check(i))
                    ans++;
            }
            printf("%d %d
    ",i,ans);
        }
        return 0;
    }
    
    
    3 4
    4 6
    5 9
    6 13
    7 19
    8 28
    9 41
    10 60
    11 88
    12 129
    13 189
    14 277
    15 406
    16 595
    17 872
    18 1278
    19 1873
    20 2745
    

    可以发现a[i]=a[i-1]+a[i-3]

    矩阵构造如下:

    (left[egin{matrix}1 & 0 & 1\1 & 0 & 0\0 & 1 & 0end{matrix} ight]*left[egin{matrix}a_{n-1}\a_{n-2}\a_{n-3}end{matrix} ight]=left[egin{matrix}a_{n-1}+a_{n-3} \a_{n-1}\a_{n-2}end{matrix} ight]=left[egin{matrix}a_{n} \a_{n-1}\a_{n-2}end{matrix} ight])

    称最左边的矩阵为关系矩阵A,已知1 2 3 的值分别为2 3 4 ,

    [left[egin{matrix}a_{n}\a_{n-1}\a_{n-2}end{matrix} ight]=A^{n-3}*left[egin{matrix}4\3\2end{matrix} ight] ]

    代码

    //#include<bits/stdc++.h>
    #include<vector>
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<string>
    #include<math.h>
    #include<queue>
    #include<map>
    #define pb push_back
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int N=1e6+10;
    const int mod=1e9+7;
    const int inf=0x3f3f3f3f;
    const double eps=1e-14;
    
    struct Matrix
    {
        ll mat[3][3];
    };
    Matrix multi(Matrix a,Matrix b)
    {
        Matrix res;
        memset(res.mat,0,sizeof(res.mat));
        for(ll i=0; i<3; i++)
        {
            for(ll j=0; j<3; j++)
            {
                for(ll k=0; k<3; k++)
                {
                    res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                    res.mat[i][j]%=mod;
                }
            }
        }
        return res;
    }
    Matrix qpow(Matrix a,ll b)
    {
        Matrix ans;
        memset(ans.mat,0,sizeof(ans.mat));
        for(ll i=0; i<3; i++) ans.mat[i][i]=1;//单位矩阵
        while(b)
        {
            if(b&1)
                ans=multi(ans,a);
            b>>=1;
            a=multi(a,a);
        }
        return ans;
    }
    int main()
    {
        Matrix base;
        memset(base.mat,0,sizeof(base.mat));
        base.mat[0][0]=base.mat[0][2]=base.mat[1][0]=base.mat[2][1]=1;
        ll T;
        scanf("%lld",&T);
        while(T--)
        {
            ll n;
            scanf("%lld",&n);
            if(n==2) printf("3
    ");
            else if(n==3) printf("4
    ");
            else
            {
                Matrix now=qpow(base,n-3);
                printf("%lld
    ",(now.mat[0][0]*4%mod+now.mat[0][1]*3%mod+now.mat[0][2]*2%mod)%mod);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    mysql基础以优化
    Mysql建立索引基础
    Mysql(1)
    SVN学习总结
    Github
    Java Eclipse断点调试
    Java设计模式图文详解
    代理模式
    Java——动态代理技术
    Spring基本概念理解
  • 原文地址:https://www.cnblogs.com/valk3/p/12793235.html
Copyright © 2011-2022 走看看