Hdu 1757A Simple Math Problem矩阵

构造个矩阵搞下就行了

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

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string.h>
typedef long long LL;
using namespace std;
LL  vis[100];
LL  M;
struct Matrix
{
    LL  m[12][12];
};
LL  n;
Matrix Mul(Matrix a, Matrix b)
{
    Matrix ans;
    for (LL i = 0; i < 10; i++){
        for (LL j = 0; j < 10; j++){
            ans.m[i][j] = 0;
            for (LL k = 0; k < 10; k++){
                ans.m[i][j] += a.m[i][k] * b.m[k][j];
                ans.m[i][j] %= M;
            }
        }
    }
    return ans;
}

Matrix quick(Matrix a, LL b)
{
    Matrix ans;
    for (LL i = 0; i < 10; i++)
    for (LL j = 0; j < 10; j++)
        ans.m[i][j] = (i == j);
    while (b){
        if (b & 1) ans = Mul(ans, a);
        a = Mul(a, a);
        b >>= 1;
    }
    return ans;
}

Matrix init()
{
    for (LL i = 0; i < 10; i++)
        scanf("%d", &vis[i]);
    Matrix ans;
    for (LL i = 0; i < 10; i++)
    for (LL j = 0; j < 10; j++)
        ans.m[i][j] = 0;
    for (LL i = 0; i < 10; i++)
        ans.m[0][i] = vis[i];
    for (LL i = 1; i < 10; i++){
        ans.m[i][i - 1] = 1;
    }
    return ans;
}

void gao()
{
    LL t = n - 9;
    Matrix ans = init();
    ans = quick(ans, t);
    LL sum = 0;
    for (LL i = 0; i < 10; i++){
        sum += ans.m[0][i] * (9 - i);
        sum %= M;
    }
    cout << sum << endl;
}

int main()
{
    while (cin >> n >> M){
        if (n < 10){
            cout << n << endl;
            continue;
        }
        gao();
    }
    return 0;
}
时间: 2024-10-12 01:03:18

Hdu 1757A 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): 2831    Accepted Submission(s): 1693 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 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 pos

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 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 w

hdu 1757 A Simple Math Problem 矩阵优化+快速幂

#include<bits/stdc++.h> using namespace std; typedef long long LL; const int n=10; int kt,m; struct Matrix { int mp[n][n]; }; Matrix mul(Matrix a,Matrix b) { int i,j,k; Matrix c; for(i=0; i<10; i++) for(j=0; j<10; j++) { c.mp[i][j]=0; for(k=0;

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 pos

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 1715 A Simple Math Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 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); 给出k,m和a0~a9,求f(k)%m,  k<2*10^9 , m < 10^5 这是一个递推式,故可以用矩阵乘法来求 和上题类似,具体思路过程见上题

HDU - 1757 A Simple Math 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 in