hdu 1757 A Simple Math Problem(矩阵快速幂乘法)

本文介绍了一种使用矩阵快速幂解决特定递推问题的方法。通过构建特定矩阵并利用快速幂运算,可以高效地计算出给定递推序列的第k项对m取模的结果。适用于处理大规模数据输入的情况。
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

 

 

把问题转化为求矩阵的n-9次幂就行了;

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int k,MOD;
 6 int a[10];
 7 int f[10];
 8 struct Matrix
 9 {
10     int m[10][10];
11 }matrix;
12 
13 Matrix Mul(Matrix a,Matrix b)
14 {
15     Matrix res;
16     int i,j,k;
17     for(i=0;i<10;i++)
18     {
19         for(j=0;j<10;j++)
20         {
21             res.m[i][j] = 0;
22             for(k=0;k<10;k++)
23                 res.m[i][j] = (res.m[i][j]+(a.m[i][k]*b.m[k][j]))%MOD;
24         }
25     }
26     return res;
27 }
28 
29 Matrix fastm(Matrix a,int b)
30 {
31     Matrix res;
32     memset(res.m,0,sizeof(res.m));
33         for(int i=0;i<10;i++)
34             res.m[i][i] = 1;
35     while(b)
36     {
37         if(b&1)
38             res = Mul(res,a);
39         a = Mul(a,a);
40         b >>= 1;
41     }
42     return res;
43 }
44 void init()
45 {
46     for(int i=0;i<=9;i++)
47     {
48         f[i]=i;
49     }
50 }
51 int main()
52 {
53     init();
54     while(scanf("%d%d",&k,&MOD)==2)
55     {
56         for(int i=0;i<=9;i++)
57         {
58             scanf("%d",&a[i]);
59         }
60         if(k<10)
61         {
62             printf("%d\n",k%MOD);
63             continue;
64         }
65         
66         memset(matrix.m,0,sizeof(matrix.m));
67         for(int i=0;i<=9;i++)
68           matrix.m[0][i]=a[i];
69         for(int i=1;i<=9;i++)
70             matrix.m[i][i-1] = 1;
71         Matrix ans=fastm(matrix,k-9);
72         Matrix cnt;
73         for(int i=0;i<10;i++)
74         {
75             cnt.m[i][0]=f[9-i];
76         }
77         Matrix p=Mul(ans,cnt);
78         printf("%d\n",p.m[0][0]%MOD);
79     }
80     return 0;
81 }
View Code

 

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值