B – A Simple Math Problem

HDU - 1757

矩阵构造入门题

\[\left[
\begin{array}{ccc}
f_0\\\\
f_1\\\\
f_2\\\\
f_3\\\\
f_4\\\\
f_5\\\\
f_6\\\\
f_7\\\\
f_8\\\\
f_9\\\\
\end{array}
\right]
*
\left[
\begin{array}{ccc}
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\\\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\\\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\\\
a_9 & a_8 & a_7 & a_6 & a_5 & a_4 & a_3 & a_2 & a_1 & a_0\\\\
\end{array}
\right]

=
\left[
\begin{array}{ccc}
f_1\\\\
f_2\\\\
f_3\\\\
f_4\\\\
f_5\\\\
f_6\\\\
f_7\\\\
f_8\\\\
f_9\\\\
f_{10}\\\\
\end{array}
\right]
\]


#include<bits/stdc++.h>
using namespace std;

int k,mod;

struct jz{
    int g[20][20];
    void init(){
        memset(g,0,sizeof(g));
    }
    void one(){
        memset(g,0,sizeof(g));
        for(int i = 1; i <= 10; ++ i) g[i][i] = 1;
    }
};
jz operator * (jz a,jz b){
    jz c; c.init();
    for(int i = 1; i <= 10; ++ i) 
    for(int j = 1; j <= 10; ++ j)
    for(int k = 1; k <= 10; ++ k)
    c.g[i][j] += a.g[i][k] * b.g[k][j] % mod, c.g[i][j] %= mod;
    return c;
}
jz ksm(jz x,int y){
    jz z; z.one();
    while(y){
        if(y & 1) z = z * x;
        y >>= 1;
        x = x * x;
    }
    return z;
}

jz a;

int main(){
    while(scanf("%d%d",&k,&mod) != EOF){
        a.init();
        for(int i = 1; i <= 9; ++ i) a.g[i][i + 1] = 1;

        for(int i = 1; i <= 10; ++ i) scanf("%d",&a.g[10][11 - i]);
        if(k < 10) { printf("%d\n",k % mod); continue; }

        k -= 9;
        a = ksm(a,k);

        int ans = 0;
        for(int i = 1; i <= 10; ++ i) 
        ans += (i - 1) * a.g[10][i] % mod, ans %= mod;
        printf("%d\n",ans);
    }
    return 0;
}
/*
1   0100000000                      2
2   0010000000                      3
3   0001000000                      4
4   0000100000                      5
5   0000010000                      6
6   0000001000                      7
7   0000000100                      8
8   0000000010                      9
9   0000000001                      10
10  a10 a9 a8 a7                       11
*/


原文链接: https://www.cnblogs.com/zzhzzh123/p/13355558.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    B - A Simple Math Problem

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/369322

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 下午6:53
下一篇 2023年3月2日 下午6:53

相关推荐