HDU 1757 简单的矩阵快速幂

http://acm.hdu.edu.cn/showproblem.php?pid=1757

A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4888    Accepted Submission(s): 2941


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);这个方程转化成的系数矩阵,

第9项矩阵                                     *系数矩阵                            =第10项矩阵(矩阵的第0行第0列表示  f(10))

f9 f8 f7 f6 f5 f4 f3 f2 f1 f0             a0  1  0  0  0  0  0  0  0  0        f10 f9 f8 f7 f6 f5 f4 f3 f2 f1

0  0  0  0  0  0  0  0  0  0              a1  0  1  0  0  0  0  0  0  0           0  0  0  0  0  0  0  0  0  0

0  0  0  0  0  0  0  0  0  0              a2  0  0  1  0  0  0  0  0  0           0  0  0  0  0  0  0  0  0  0

0  0  0  0  0  0  0  0  0  0              a3  0  0  0  1  0  0  0  0  0           0  0  0  0  0  0  0  0  0  0

0  0  0  0  0  0  0  0  0  0              a4  0  0  0  0  1  0  0  0  0           0  0  0  0  0  0  0  0  0  0

0  0  0  0  0  0  0  0  0  0    乘以   a5  0  0  0  0  0  1  0  0  0   等于 0  0  0  0  0  0  0  0  0  0

0  0  0  0  0  0  0  0  0  0              a6  0  0  0  0  0  0  1  0  0           0  0  0  0  0  0  0  0  0  0

0  0  0  0  0  0  0  0  0  0              a7  0  0  0  0  0  0  0  1  0           0  0  0  0  0  0  0  0  0  0

0  0  0  0  0  0  0  0  0  0              a8  0  0  0  0  0  0  0  0  1           0  0  0  0  0  0  0  0  0  0

0  0  0  0  0  0  0  0  0  0              a9  0  0  0  0  0  0  0  0  0           0  0  0  0  0  0  0  0  0  0

其中 f10  =a0*f9+a1*f8+a2*f7+......+a9*f0;

同理用第10项矩阵乘以系数矩阵就能得到第11项。

有第9项矩阵,让求第k项,所以成以系数矩阵 (k-9)次,就可以第k项。因为k很大,所以用快速幂。

所以叫做矩阵快速幂0.0。

AC

#include <iostream>
#include <queue>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 50000
#define LL long long
using namespace std;
int m;
struct node
{
    LL a[15][15];
}a,ans;//a是方程转化成的系数矩阵,ans是初始矩阵。
node operator * (node b,node c)//自定义矩阵乘法
{
    node d;
    memset(d.a,0,sizeof(d.a));
    for(int i=0;i<10;i++)
        for(int j=0;j<10;j++)
        {
            for(int k=0;k<10;k++)
                d.a[i][j]+=b.a[i][k]*c.a[k][j];
                d.a[i][j]%=(LL)m;
        }
     return d;
}
void init()//矩阵初始化
{
    memset(a.a,0,sizeof(a.a));
    memset(ans.a,0,sizeof(ans.a));
    for(int i=0;i<10;i++)
    {
        ans.a[0][i]=9LL-(LL)i;
        scanf("%I64d",&a.a[i][0]);
        a.a[i][i+1]=1LL;(1LL表示LL型的1)
    }
}
int main()
{
    int k;
    while(~scanf("%d%d",&k,&m))
    {
        init();
        if(k<10)
        {
            printf("%d\n",k%m);
            continue;
        }
        k-=9;
        while(k)
        {
            if(k%2)
                ans= ans*a;
            a= a*a;
            k=k>>1;
        }
        printf("%I64d\n",ans.a[0][0]);
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值