hdu1757 A Simple Math Problem

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

/*
裸题
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 30;
ll sz,mod,f[maxn],a[maxn],ans[maxn];
struct mtx{
    ll v[maxn][maxn];
    void clear(){
        memset(v,0,sizeof(v));
    }
    mtx mul(mtx A,mtx B){
        mtx C;
        C.clear();
        for(int i = 1;i <= sz;i++){
            for(int j = 1;j <= sz;j++){
                for(int k = 1;k <= sz;k++){
                    C.v[i][j] = (C.v[i][j] + A.v[i][k]*B.v[k][j]) % mod;
                }
            }
        }
        return C;
    }
    mtx pow(mtx A,int n){
        mtx R;
        R.clear();
        for(int i = 1;i <= sz;i++) R.v[i][i] = 1;
        while(n){
            if(n&1) R = R.mul(R,A);
            n >>= 1;
            A = A.mul(A,A);
        }
        return R;
    }
    void get_tr(mtx A){
        for(int i = 1;i <= sz;i++){
            for(int j = 1;j <= sz;j++){
                ans[j] = (ans[j] + f[i]*A.v[i][j]) % mod;
            }
        }
    }
};
int main(){
    ll d,n,m;
   while(scanf("%I64d%I64d",&n,&m) == 2) {
       d = 10;
       mtx A;
    for(int i = 1; i <= d; i++) { cin >> a[i]; a[i] %= m; }
    for(int i = d; i >= 1; i--) { f[i] = d-i; f[i] %= m; }
    if(n < 10){
        cout<<n<<endl;
        continue;
    }
    n++;
    A.clear();
    memset(ans,0,sizeof(ans));
    for(int i = 1; i <= d; i++) A.v[i][1] = a[i];
    for(int i = 2; i <= d; i++) A.v[i-1][i] = 1;

    sz = d;
    mod = m;
    A = A.pow(A,n-d);
    A.get_tr(A);
    cout << ans[1] << endl;
  }
    return 0;
}
时间: 2024-07-29 09:27:04

hdu1757 A Simple Math Problem的相关文章

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): 2791    Accepted Submission(s): 1659 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) =

hdu1757 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): 3522    Accepted Submission(s): 2130 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f

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): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

hdu1757-- A Simple Math Problem(矩阵快速幂优化)

A Simple Math Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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

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): 108 Accepted Submission(s): 77   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

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): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

hdu 5974 A Simple Math Problem

A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1645    Accepted Submission(s): 468 Problem Description Given two positive integers a and b,find suitable X and Y to meet th

hdoj 1757 A Simple Math Problem

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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(

FZYZ-2071 A Simple Math Problem IX

P2071 -- A Simple Math Problem IX 时间限制:1000MS      内存限制:262144KB 状态:Accepted      标签:    数学问题-博弈论   无   无 Description 给定a,b,n,保证a≥2,b≥1,a^b≤n.两个人在玩游戏,每个人每次可以把a加1,或者把b加1,但是不能违反a^b<=n,无法再进行操作的人就输掉了这一场游戏. 假设两个人都足够聪明,按照最优策略进行游戏,问先手是否有必胜策略. Input Format 第