A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列
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;
}
A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列的更多相关文章
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)
GTY's math problem Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- hdu-1757 A Simple Math Problem---矩阵快速幂模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...
- A Simple Math Problem 矩阵打水题
A Simple Math Problem Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x & ...
- HDU - 3521 An easy Problem(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...
- hdu 6182A Math Problem(快速幂)
You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n. Inp ...
随机推荐
- Java AOP 注解配置与xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Python学习笔记_一个Tkinter示例,使用FileDialog
为了使用Python进行数据分析,编写一个图形界面,选择一个Excel文件(或CSV),然后进行后续处理. 一.本示例涵盖如下知识点: 1.FileDialog的使用 2.退出程序 3.消息提示框的示 ...
- Python学友
独学而无友,则孤陋而寡闻,python学习过程中希望多和学友交流,一起进步. 开源中国 j_hao104 微信公众号: Pythoner每日一报 https://my.oschina.net/jhao ...
- 14.UNION 和 UNION ALL 操作符
union all union UNION 操作符用于合并两个或多个 SELECT 语句的结果集.请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同 ...
- vmware10安装Arch
必须保证机器能够上网! 1.vmware10中创建虚拟机(跟虚拟机中安装其他系统同样的操作). 2.开启上一步中创建的虚拟机. 3选择第一项进入 4自动进入root命令行 5进入 /dev (进行分区 ...
- WindowsService服务安装脚本
安装脚本%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe D:\liuyl\WeiXinService\WindowsSe ...
- 【SQL】- 基础知识梳理(五) - 触发器
触发器的概念 触发器对表进行插入.更新.删除的时候会自动执行的特殊存储过程 触发器的语法 create trigger tgr_name on table_name with encrypion –加 ...
- Spring学习----自动装配@Resource、@Autowired、@Qualifier
直接看下面的代码即可明白: applicationContext.xml里面添加个bean: <!--注解的注入 --> <bean id="student3" ...
- VSCODE 针对调试C语言时一闪而过解决办法
针对调试C语言时一闪而过解决办法 前提: 已经按照 C/C++ 已经安装 MINGW(并配置完成) 原因: 主要是因为tasks的配置没有写对 解决办法: tasks.json { // See h ...
- java 实验2 类
共5道大题 最后一题为自动洗牌发牌系统 1) 编写一个类实现银行帐户的概念.包括的属性有:帐号.储户姓名.地址.存款余额,包括的方法有:存款.取款.查询.计算利息.累加利息等. public cla ...