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

    A Simple Math Problem

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4544    Accepted Submission(s): 2741

    Problem Description
    Lele now is thinking about a simple function f(x).
    If x < 10 f(x) = x.
    If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
    And ai(0<=i<=9) can only be 0 or 1 .
    Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
    Input
    The problem contains mutiple test cases.Please process to the end of file.
    In each case, there will be two lines.
    In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
    In the second line , there are ten integers represent a0 ~ a9.
    Output
    For each case, output f(k) % m in one line.
    Sample Input
    10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0
    Sample Output
    45 104
    Author
    linle
    /*
    { f[n] f[n-1]...f[n-9] } = { [ f[n-1] f[n-2] ...f[n-10] } *  
    a[0] 1 0 ...0
    a[1] 0 1 ...0
    ............1
    a[9] 0 0 ...0
    */  
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    using namespace std;
    typedef long long ll;
    
    const long long N = 10;
    ll f1,f2,k;
    ll mod;
    int n;
    struct Fast_Matrax {
        ll a[N][N];
        Fast_Matrax() {
            memset(a,0,sizeof(a));
        }
        void init() {
            for(int i=0; i<N; i++)
                for(int j=0; j<N; j++)
                    a[i][j]=(i==j);
        }
        Fast_Matrax operator * (const Fast_Matrax &B)const {
            Fast_Matrax C;
            for(int i=0; i<N; i++)
                for(int k=0; k<N; k++)
                    for(int j=0; j<N; j++)
                        C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j]%mod+mod)%mod;
            return C;
        }
        Fast_Matrax operator ^ (const ll &t)const {
            Fast_Matrax A=(*this),res;
            res.init();
            ll p=t;
            while(p) {
                if(p&1)res=res*A;
                A=A*A;
                p>>=1;
            }
            return res;
        }
    } ans,tmp,x;
    int main() {
        for(int i=0; i<10; i++) {
            x.a[0][i]=9-i;
        }
        while(~scanf("%lld%lld",&k,&mod)) {
            //tmp.init();
            for(int i=0; i<10; i++) {
                scanf("%lld",&tmp.a[i][0]);
                if(i<9)
                    tmp.a[i][i+1]=1;
            }
            if(k<10) {
                printf("%lld
    ",k%mod);
                continue;
            }
            ans=x*(tmp^(k-9));
            printf("%lld
    ",ans.a[0][0]);
        }
        return 0;
    }
  • 相关阅读:
    [置顶] flex4事件监听与自定义事件分发(三)
    不要通过终止进程的方式清理内存
    两个关于XML解析报错问题小记
    HDU H204 阿牛的EOF牛肉串
    <Win32_8>由浅入深——滚动条
    设计模式19---设计模式之状态模式(State)(行为型)
    NetworkX学习笔记-5-NetworkX中怎样对多个网络赋属性,并根据属性排序
    OpenRisc-41-or1200的cache模块分析
    Hibernate 入门的第一个程序
    NDK GDB 中打印vector , vector<vector <> >
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6533346.html
Copyright © 2011-2022 走看看