A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列
2024-10-20 16:06:33
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
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll mod;
ll op[15];
struct mat
{
ll a[15][15];
};
mat Mul(mat a,mat b)
{
mat ans;
memset(ans.a,0,sizeof(ans.a));
for(int t=0;t<10;t++)
{
for(int j=0;j<10;j++)
{
for(int k=0;k<10;k++)
{
ans.a[t][j]=(ans.a[t][j]+a.a[t][k]*b.a[k][j])%mod;
}
}
}
return ans;
}
mat ans;
ll quick(ll n)
{
mat res;
memset(res.a,0,sizeof(res.a));
for(int t=0;t<10;t++)
{
res.a[0][t]=op[t];
}
res.a[1][0]=1;
res.a[2][1]=1;
res.a[3][2]=1;
res.a[4][3]=1;
res.a[5][4]=1;
res.a[6][5]=1;
res.a[7][6]=1;
res.a[8][7]=1;
res.a[9][8]=1;
while(n)
{
if(n&1)
{
ans=Mul(res,ans);
}
res=Mul(res,res);
n>>=1;
}
return ans.a[0][0];
}
int main()
{
ll n;
while(cin>>n)
{
cin>>mod;
for(int t=0;t<10;t++)
{
scanf("%lld",&op[t]);
}
if(n<10)
{
printf("%lld\n",n%mod);
}
else{
for(int t=9;t>=0;t--)
{
ans.a[9-t][0]=t;
}
printf("%lld\n",quick(n-9)%mod);
}
}
return 0;
}
最新文章
- C#中的BackgroundWorker控件+Delegate.Invoke (委托同步调用)
- 来聊聊apply和call
- CentOS 7下关于systemd的一些唠叨话二:systemd服务脚本的编写
- python参考手册--第2章词汇和语法约定
- java编码转化方案-备用
- jquery JS 左右方向键
- GDAL切割重采样遥感图像
- Hibernate中sessionfactory和session的多线程问题
- Yii2框架ACF(AccessControl Filter)的使用
- JavaWeb学习笔记五 会话技术Cookie&;Session
- 安装了精简版的windows 的电脑如何修复?参照的程序集没有安装在系统上
- [转帖]Sqlserver BCP 的用法
- vc6.0问题
- 006_饿了么大前端总监sofish帮你理清前端工程师及大前端团队的成长问题!
- Shell脚本笔记(五)Shell函数
- python---tornado初识(2)实现登录和发布文章
- 使用 CGContextRef 进行简单内容绘制
- 《DSP using MATLAB》Problem 3.8
- OpenCV与Python之图像的读入与显示以及利用Numpy的图像转换
- 【随笔】win7下安装Apache服务器
热门文章
- 常用Xcode文档位置,修改Xcode项目模板地址总结,以及常用地址,随时更新。
- SpringBoot25 gradle安装、利用gradle创建SrpingBoot项目
- Virtual Machine Definition File 2.2
- opennebula 创建镜像数据块
- 基于 EntityFramework 的数据库主从读写分离架构 - 目录
- Apache apxs命令
- Entity Framework Tutorial Basics(41):Multiple Diagrams
- Mapper配置文件夹
- DPF.Android.Native.Components.v2.8.1 for delphi xe6 使用DPFJAlertDialog遇到的问题
- c# 简单委托例子