矩阵十题【八】 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

这是一个递推式,故可以用矩阵乘法来求

和上题类似,具体思路过程见上题。

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
const int MAX = 15;

struct Matrix
{
    int v[MAX][MAX];
};

int n=10,M;

Matrix mtMul(Matrix A, Matrix B)        // 求矩阵 A * B
{
    int i, j, k;
    Matrix C;
    for(i = 0; i < n; i ++)
        for(j = 0; j < n; j ++)
        {
            C.v[i][j] = 0;
            for(k = 0; k < n; k ++)
                C.v[i][j] = (A.v[i][k] * B.v[k][j] + C.v[i][j]) % M;
        }
    return C;
}

 Matrix mtPow(Matrix origin,int k)  //矩阵快速幂
 {
     int i;
     Matrix res;
     memset(res.v,0,sizeof(res.v));
     for(i=1;i<=n;i++)
         res.v[i][i]=1;
     while(k)
     {
         if(k&1)
             res=mtMul(res,origin);
         origin=mtMul(origin,origin);
         k>>=1;
     }
     return res;
 }

void out(Matrix A)
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        cout<<A.v[i][j]<<" ";
        cout<<endl;
    }
    cout<<endl;
}

int main ()
{
    int num;
    while(~scanf("%d%d",&num,&M))
    {
    Matrix A;
    memset(A.v,0,sizeof(A.v));
    for(int i=0;i<10;i++) A.v[0][i]=i;
    //out(A);

    Matrix ans;
    memset(ans.v,0,sizeof(ans.v));
    for(int i=0;i<10;i++) ans.v[i+1][i]=1;
    for(int i=9;i>=0;i--)
        scanf("%d",&ans.v[i][9]);
    //out(ans);
    ans=mtPow(ans,num);
    A=mtMul(A,ans);
    cout<<A.v[0][0]<<endl;
    }
}

矩阵十题【八】 HDU 1715 A Simple Math Problem

时间: 2024-12-11 11:55:36

矩阵十题【八】 HDU 1715 A Simple Math Problem的相关文章

2014多校第十场1004 || HDU 4974 A simple water problem

题目链接 题意 : n支队伍,每场两个队伍表演,有可能两个队伍都得一分,也可能其中一个队伍一分,也可能都是0分,每个队伍将参加的场次得到的分数加起来,给你每个队伍最终得分,让你计算至少表演了几场. 思路 : ans = max(maxx,(sum+1)/2) :其实想想就可以,如果所有得分中最大值没有和的一半大,那就是队伍中一半一半对打,否则的话最大的那个就都包了. 1 #include <cstdio> 2 #include <cstring> 3 #include <st

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 - 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

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)

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 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

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(矩阵快速幂)

题目地址:HDU 1757 终于会构造矩阵了.其实也不难,只怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10) 构造的矩阵是:(我代码中构造的矩阵跟这个正好是上下颠倒过来了) |0 1 0 ......... 0|    |f0|   |f1 | |0 0 1 0 ...... 0|    |f1|   |f2 | |...................1| *  |..| = |...|

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

[题目链接]:click here~~ [题目大意]: 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); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const