首页 > 其他分享 >hdu:Problem Description Lele now is thinking about a s(矩阵快速幂)

hdu:Problem Description Lele now is thinking about a s(矩阵快速幂)

时间:2023-01-14 11:22:05浏览次数:55  
标签:10 hdu Description thinking there Lele line matrix

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.

 

输入样例

10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0

输出样例

45
104

本题只需要计算系数矩阵,最后一行乘积和即可算出f(k)
附ac代码
#include<bits/stdc++.h>
using namespace std;
const int N=10;
struct matrix{
    long long ma[N][N];
};
int k,m;
matrix mutil(matrix a,matrix b)
{
    matrix c;
    for(int i=0;i<10;++i)
      for(int j=0;j<10;++j)
      c.ma[i][j]=0;
    for(int i=0;i<10;++i)
      for(int j=0;j<10;++j)
       for(int z=0;z<10;++z)
       {c.ma[i][j]+=((a.ma[i][z]%m)*(b.ma[z][j]%m))%m;
       c.ma[i][j]%=m;
       }
    return c;
}
matrix pow_ma(matrix a,int n)
{
    if(n==1) return a;
    matrix s;
    s=pow_ma(mutil(a,a),n/2);
    if(n%2) s=mutil(s,a);
    return s;
}
int a[N];
int main()
{
    while(scanf("%d%d",&k,&m)==2)
    {
        for(int i=0;i<=9;++i)
        scanf("%d",&a[i]);
        if(k<10)
        {
         printf("%d\n",k%m);continue;
        }
        matrix ans;
        for(int i=0;i<N;++i)
          for(int j=0;j<N;++j)
          ans.ma[i][j]=0;
        //ans记得需要初始化-----
        for(int i=0;i<=9;++i)
            ans.ma[0][i]=a[i];
        for(int i=1;i<=9;++i)
            ans.ma[i][i-1]=1;
        ans=pow_ma(ans,k-9);
        long long sum=0;
        for(int i=0;i<10;++i)
        sum+=(ans.ma[0][i]%m*((9-i)%m))%m;
        printf("%lld\n",sum%m);
    }
    return 0;
}

 

标签:10,hdu,Description,thinking,there,Lele,line,matrix
From: https://www.cnblogs.com/ruoye123456/p/17051473.html

相关文章

  • HDU 5306 Gorgeous Sequence
    \(HDU\)\(5306\)\(Gorgeous\)\(Sequence\)标签:区间最值操作,吉司机线段树,简单模板题一、题目描述现在有这样的一个问题:你有一个长度为\(n\)(\(n≤1e6\))的序列,你......
  • hdu:Ignatius and the Princess II(全排列,dfs)
    ProblemDescriptionNowourherofindsthedoortotheBEelzebubfeng5166.Heopensthedoorandfindsfeng5166isabouttokillourprettyPrincess.Butnow......
  • hdu:I Hate It(线段树单点更新)
    ProblemDescription很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。这让很多学生很反感。不管你喜不喜欢,现在需要你做的是,就是按照......
  • HDU-3949 XOR 题解报告
    题目地址题意:从一个序列中取任意个元素进行异或,求能异或出的所有数字中的第k小。分析:性质:一个线性基异或上另一个不同的线性基,并作为自己的新值,这并不改变整个线性......
  • hdu:张煊的金箍棒(2)(线段树)
    ProblemDescription张煊的金箍棒升级了!升级后的金箍棒是由几根相同长度的金属棒连接而成(最开始都是铜棒,从1到N编号);张煊作为金箍棒的主人,可以对金箍棒施以任意的变换,每......
  • hdu:张煊的金箍棒(3)(线段树)
    ProblemDescription张煊的金箍棒升级了!升级后的金箍棒是由N段相同长度的金属棒连接而成(最开始每段金属棒的价值都是1,从1到N编号);张煊作为金箍棒的主人,可以对金箍棒任意......
  • hdu:Choose the best route(dijstra,虚设点)
    ProblemDescriptionOneday,Kikiwantstovisitoneofherfriends.Assheisliabletocarsickness,shewantstoarriveatherfriend’shomeassoonasp......
  • hdu:最短路(堆优化的dijkstra)
    ProblemDescription在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现......
  • hdu: 张煊的金箍棒(3)(树状数组的区间修改,区间查询)
    ProblemDescription张煊的金箍棒升级了!升级后的金箍棒是由N段相同长度的金属棒连接而成(最开始每段金属棒的价值都是1,从1到N编号);张煊作为金箍棒的主人,可以对金箍棒任意......
  • 【ADSP-BF561 EZ-KIT Lite】GENERAL DESCRIPTION
    TheADSP-BF561processorisahigh-performancememberoftheBlackfinfamilyofproductstargetingavarietyofmultimediaandtelecommunicationsapplications.......