zoukankan      html  css  js  c++  java
  • HDU.1757 A Simple Math Problem (矩阵快速幂)

    HDU.1757 A Simple Math Problem (矩阵快速幂)

    点我挑战题目

    题意分析

    给出一个递推式:
    1.x<=9时,f(x) = x,
    2.x>9时,f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10).
    现在想让你求解f(k) % m 的值为多少.

    当x<=9时,特判输出x % m.
    当x>9是,构造如下矩阵乘法:

    f(x) 0 0 0 0 0 0 0 0 0
    f(x-1) 0 0 0 0 0 0 0 0 0
    f(x-2) 0 0 0 0 0 0 0 0 0
    f(x-3) 0 0 0 0 0 0 0 0 0
    f(x-4) 0 0 0 0 0 0 0 0 0
    f(x-5) 0 0 0 0 0 0 0 0 0
    f(x-6) 0 0 0 0 0 0 0 0 0
    f(x-7) 0 0 0 0 0 0 0 0 0
    f(x-8) 0 0 0 0 0 0 0 0 0
    f(x-9) 0 0 0 0 0 0 0 0 0

    等于

    a0 a1 a2 a3 a4 a5 a6 a7 a8 a9
    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 0

    f(x-1) 0 0 0 0 0 0 0 0 0
    f(x-2) 0 0 0 0 0 0 0 0 0
    f(x-3) 0 0 0 0 0 0 0 0 0
    f(x-4) 0 0 0 0 0 0 0 0 0
    f(x-5) 0 0 0 0 0 0 0 0 0
    f(x-6) 0 0 0 0 0 0 0 0 0
    f(x-7) 0 0 0 0 0 0 0 0 0
    f(x-8) 0 0 0 0 0 0 0 0 0
    f(x-9) 0 0 0 0 0 0 0 0 0
    f(x-10) 0 0 0 0 0 0 0 0 0

    如此便可得到最后结果。

    代码总览

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <sstream>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <cmath>
    #define INF 0x3f3f3f3f
    #define nmax 200
    #define MEM(x) memset(x,0,sizeof(x))
    using namespace std;
    const int Dmax = 11;
    int N = 10;
    int MOD;
    typedef struct{
        int matrix[Dmax][Dmax];
        void init()//初始化为单位矩阵
        {
            memset(matrix,0,sizeof(matrix));
            for(int i = 0; i<Dmax;++i) matrix[i][i] = 1;
        }
    }MAT;
    MAT ADD(MAT a, MAT b)
    {
        for(int i = 0; i<N;++i){
            for(int j = 0;j<N;++j){
                a.matrix[i][j] +=b.matrix[i][j];
                a.matrix[i][j] %= MOD;
            }
        }
        return a;
    }
    MAT MUL(MAT a, MAT b)
    {
        MAT ans;
        for(int i = 0; i<N;++i){
            for(int j = 0; j<N;++j){
                ans.matrix[i][j] = 0;
                for(int k = 0; k<N;++k){
                    ans.matrix[i][j] += ( (a.matrix[i][k] % MOD) * (b.matrix[k][j] % MOD) ) % MOD;
                }
                ans.matrix[i][j] %= MOD;
            }
        }
        return ans;
    }
    MAT POW(MAT a, int t)
    {
        MAT ans; ans.init();
        while(t){
            if(t&1) ans = MUL(ans,a);
            t>>=1;
            a = MUL(a,a);
        }
        return ans;
    }
    void OUT(MAT a)
    {
        for(int i = 0; i<N;++i){
            for(int j =  0; j<N;++j){
                printf("%5d",a.matrix[i][j]);
            }
            printf("
    ");
        }
    }
    void IN(MAT & a,MAT & temp)
    {
        memset(a.matrix,0,sizeof(a.matrix));
        memset(temp.matrix,0,sizeof(temp.matrix));
        for(int i = 0; i<N;++i) a.matrix[i][0] = 9-i;
        for(int i = 0; i<N;++i) scanf("%d",&temp.matrix[0][i]);
        for(int i = 1; i<N;++i) temp.matrix[i][i-1] = 1;
    }
    void CAL(MAT a)
    {
        printf("%d
    ",a.matrix[0][0] % MOD);
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int K;
        while(scanf("%d%d",&K,&MOD) != EOF){
            if(K<=9){
                printf("%d
    ",K);
                continue;
            }
            MAT init,temp;
            IN(init,temp);
            temp = POW(temp,K-9);
            temp = MUL(temp,init);
            CAL(temp);
        }
        return 0;
    }
  • 相关阅读:
    Yahoo 14条 雅虎十四条 优化原则【转】
    从HTML1到HTML5,回首HTML发展历史【转】
    HTML的发展历史【转】
    jquery中prop()方法和attr()方法的区别【转】
    一个文献综述的写法
    ammunition用法
    混了SQL连接就运行这个
    cruel用法
    ball up用法
    episode用法
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367087.html
Copyright © 2011-2022 走看看