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

题意:按f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10) (x>=10) ; f(x) = x(x<10)来计算f(x)%m的值。

分析:这题要用递推,并且k值很大,所以需要用矩阵快速幂。

构造的矩阵是:

a0 a1 a2 a3 a4 a5 a6 a7 a8 a9
1                  
  1                
    1              
      1            
        1          
          1        
            1      
              1    
                1  
*
f(x-1)
f(x-2)
f(x-3)
f(x-4)
f(x-5)
f(x-6)
f(x-7)
f(x-8)
f(x-9)
f(x-10)
=
f(x)
f(x-1)
f(x-2)
f(x-3)
f(x-4)
f(x-5)
f(x-6)
f(x-7)
f(x-8)
f(x-9)

写个结构类型代表矩阵,以及矩阵的相乘的函数和矩阵快速幂的函数,注意一下初始化。

#include<stdio.h>
#include<string.h>
int n,k,m;
struct matrix
{
    int a[15][15];
    int row,col;
    void init(int r,int c){
        memset(a,0,sizeof(a));
        row=r;col=c;
    }
} big,f,u;
matrix mul(matrix a,matrix b)
{
    matrix c;
    c.init(a.row,b.col);
    for(int i=0; i<a.row; i++)
        for(int j=0; j<b.col; j++)
        {
            for(int k=0; k<a.col; k++)
                c.a[i][j]+=(a.a[i][k]*b.a[k][j])%m;
            c.a[i][j]%=m;
        }
    return c;
}
void init()
{
    big.init(10,10);
    f.init(10,1);
    for(int i=1; i<10; i++)
        big.a[i][i-1]=1;
    for(int i=0; i<10; i++)
        f.a[i][0]=9-i;
}
matrix qpow(matrix a,int k)
{
    matrix ans;
    ans.init(a.row,a.col);
    for(int i=0;i<a.row;i++)
        ans.a[i][i]=1;
    while(k)
    {
        if(k&1)ans=mul(ans,a);
        a=mul(a,a);
        k>>=1;
    }
    return ans;
}
int main()
{
    init();
    while(~scanf("%d%d",&k,&m))
    {
        for(int i=0; i<10; i++)
            scanf("%d",&big.a[0][i]);
        u=mul(qpow(big,k-9),f);
        printf("%d\n",u.a[0][0]%m);
    }
    return 0;
}
时间: 2024-12-27 20:46:29

【HDU 1757】 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): 2930    Accepted Submission(s): 1762 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x)

【HDU 5399】Too Simple

题 Description Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai thought this problem was too simple, sometimes naive. So she ask you for help. Teacher Mai has m functions f1,f2,...,fm:{1,2,...,n}→{1,2,...,n}(that mea

矩阵十题【八】 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 (乘法矩阵)

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

【BZOJ3489】A simple rmq problem kd-tree

[BZOJ3489]A simple rmq problem Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直接输出0.我会采取一些措施强制在线. Input 第一行为两个整数N,M.M是询问数,N是序列的长度(N<=100000,M<=200000) 第二行为N个整数,描述这个序列{ai},其中所有1<=ai<=N 再下面M行,每

一本通1548【例 2】A Simple Problem with Integers

1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],-,a[n],你需要依次进行 q 个操作,操作有两类: 1 l r x:给定 l,r,x,对于所有 i∈[l,r],将 a[i] 加上 x(换言之,将 a[l],a[l+1],-,a[r] 分别加上 x): 2 l r:给定 l,r,求 a[i]∑i=[l,r].?a[i] 的值(换言之,求 a[l]+a[l+1]+?+a[r] 的值). 输入格式 第一行包