zoukankan      html  css  js  c++  java
  • Count(广工14届竞赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470

    这道题目题解就扔了个矩阵快速幂啥都没写。。。。。这题解是太看得懂我这个弱鸡了。

    既然是矩阵快速幂那么先扔个矩阵快速幂的学习链接:https://www.luogu.org/problemnew/show/P3390

    废话不说上图。这个是斐波那契数列的矩阵的推导。

    既然有这种骚东西,肯定有其他的递推式。然后就是我们JX大佬给的神图,我研究半天才懂

     有了这些工具,那么这道题目就可以解决了,继续上我的推导过程。

     很明显,这道题n从3开始,那么快速幂矩阵时的次方应该是n-2。好了,重点都讲完了,上代码。。。

    #include <bits/stdc++.h>
    using namespace std;
    #define re register
    #define ll long long
    const int mod=123456789;
    void read(int &a)
    {
        a=0;
        int d=1;
        char ch;
        while(ch=getchar(),ch>'9'||ch<'0')
            if(ch=='-')
                d=-1;
        a=ch-'0';
        while(ch=getchar(),ch>='0'&&ch<='9')
            a=a*10+ch-'0';
        a*=d;
    }
    void write(int x)
    {
        if(x<0)
            putchar(45),x=-x;
        if(x>9)
            write(x/10);
        putchar(x%10+'0');
    }
    int n=6;
    struct note
    {
        int a[10][10];
    };
    note ans,a,b;
    void init()
    {
        a.a[1][1]=1;
        a.a[1][2]=2;
        a.a[1][3]=1;
        a.a[1][4]=3;
        a.a[1][5]=3;
        a.a[1][6]=1;
        a.a[2][1]=1;
        a.a[2][2]=0;
        a.a[2][3]=0;
        a.a[2][4]=0;
        a.a[2][5]=0;
        a.a[2][6]=0;
        a.a[3][1]=0;
        a.a[3][2]=0;
        a.a[3][3]=1;
        a.a[3][4]=0;
        a.a[3][5]=0;
        a.a[3][6]=0;
        a.a[4][1]=0;
        a.a[4][2]=0;
        a.a[4][3]=1;
        a.a[4][4]=1;
        a.a[4][5]=0;
        a.a[4][6]=0;
        a.a[5][1]=0;
        a.a[5][2]=0;
        a.a[5][3]=1;
        a.a[5][4]=2;
        a.a[5][5]=1;
        a.a[5][6]=0;
        a.a[6][1]=0;
        a.a[6][2]=0;
        a.a[6][3]=1;
        a.a[6][4]=3;
        a.a[6][5]=3;
        a.a[6][6]=1;
        b.a[1][1]=2;
        b.a[1][2]=1;
        b.a[1][3]=1;
        b.a[1][4]=2;
        b.a[1][5]=4;
        b.a[1][6]=8;
    }
    note Mat(note x,note y)
    {
        note c;
        for(re int i=1;i<=6;i++)
            for(re int j=1;j<=6;j++)
                c.a[i][j]=0;
        for(re int i=1;i<=6;i++)
            for(re int j=1;j<=6;j++)
                for(re int k=1;k<=6;k++)
                    c.a[i][j]=(c.a[i][j]+1ll*x.a[i][k]*y.a[k][j]%mod)%mod;
        return c;
    }
    int main()
    {
        int T;
        read(T);
        while(T--)
        {
            ll n;
            scanf("%lld",&n);
            n-=2;
            init();
            for(re int i=1;i<=6;i++)
                for(re int j=1;j<=6;j++)
                    ans.a[i][j]=0;
            for(re int i=1;i<=6;i++)
                ans.a[i][i]=1;
            while(n)
            {
                if(n&1)
                    ans=Mat(ans,a);
                a=Mat(a,a);
                n>>=1;
            }
            int sum=0;
            for(re int i=1;i<=6;i++)
                sum=(sum+1ll*ans.a[1][i]*b.a[1][i]%mod)%mod;
            write(sum);
            putchar('
    ');
        }
        return 0;
    }
    View Code

     代码应该很好懂,我就不注释了 Orz

  • 相关阅读:
    hdu 5366 简单递推
    hdu 5365 判断正方形
    hdu 3635 并查集
    hdu 4497 数论
    hdu5419 Victor and Toys
    hdu5426 Rikka with Game
    poj2074 Line of Sight
    hdu5425 Rikka with Tree II
    hdu5424 Rikka with Graph II
    poj1009 Edge Detection
  • 原文地址:https://www.cnblogs.com/acm1ruoji/p/10556136.html
Copyright © 2011-2022 走看看