• 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;
    }
  • 相关阅读:
    7年点工,中年测试人的出路与未来
    通过 jmeter 完成对请求字段的加密
    作为职场萌新怎么才能脱颖而出?
    阿里淘宝的 S1 级别 bug,到底是谁的锅?
    年前,我偷偷背着家人辞职,报名了软件测试
    一文搞定所有 web 自动化常见问题
    利用Android中DDMS->Heap工具检测内存泄漏问题
    Android Studio中怎么使用DDMS工具?
    使用DDMS测试安卓手机APP的性能(android)------转载
    web功能测试之表单、搜索测试
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6533346.html
Copyright © 2020-2023  润新知