A Simple Math Problem

一个矩阵快速幂水题,关键在于如何构造矩阵。做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造。比赛的时候手残把矩阵相乘的一个j写成了i,调试了好久才发现。改过来1A。

贴个AC的代码:

const int N=1e5+10;
ll k,m,s[10];
struct mat
{
ll a[10][10];
};
mat mat_mul(mat A,mat B)
{
mat res;
memset(res.a,0,sizeof(res.a));
for(int k=0; k<10; k++)
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
res.a[i][j]=(res.a[i][j]+A.a[i][k]*B.a[k][j])%m;
return res;
}
mat mat_fast_pow(mat c,ll n)
{
mat res;
memset(res.a,0,sizeof(res.a));
for(int i=0; i<10; i++) res.a[i][i]=1;
while(n)
{
if(n&1) res=mat_mul(res,c);
c=mat_mul(c,c);
n=n>>1;
}
return res;
}
ll get_ans()
{
mat c,res;
memset(c.a,0,sizeof(c.a));
memset(res.a,0,sizeof(res.a));
for(int i=9,j=0; i>=0; i--,j++) res.a[j][0]=i; //初始矩阵;
for(int i=0; i<10; i++) c.a[0][i]=s[i]; //构造矩阵
for(int i=1; i<10; i++) c.a[i][i-1]=1;
res=mat_mul(mat_fast_pow(c,k-9),res);
return res.a[0][0]%m;
}
int main()
{
while(~scanf("%I64d%I64d",&k,&m))
{
for(int i=0; i<10; i++) scanf("%I64d",&s[i]);
if(k<10)
{
printf("%I64d\n",s[k]%m);//这里应该是k%m,但这样写居然过了,可见其后台之水。
continue;
}
ll ans=get_ans();
printf("%I64d\n",ans);
}
return 0;
}

前一阵子学了矩阵快速幂,个人感觉不是很难,可能是没有碰到很复杂的题吧,相关的题做的也不是很多,关键还是在于矩阵构造上,其他的就是裸模板了。

HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过的更多相关文章

  1. 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 > ...

  2. A Simple Math Problem(HDU 1757 构造矩阵)

    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-1 ...

  3. hdu6470 矩阵快速幂+构造矩阵

    http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意 \(f[n]=2f[n-2]+f[n-1]+n^3,n \leq 10^{18}\),求f[n] 题 ...

  4. Luogu 3390 【模板】矩阵快速幂 (矩阵乘法,快速幂)

    Luogu 3390 [模板]矩阵快速幂 (矩阵乘法,快速幂) Description 给定n*n的矩阵A,求A^k Input 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵 ...

  5. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. 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 ...

  8. 51nod 1113 矩阵快速幂( 矩阵快速幂经典模板 )

    1113 矩阵快速幂 链接:传送门 思路:经典矩阵快速幂,模板题,经典矩阵快速幂模板. /******************************************************* ...

  9. hdu1757 A Simple Math Problem

    Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x > ...

  10. hdu 6182A Math Problem(快速幂)

    You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n.  Inp ...

随机推荐

  1. Backbone.js入门教程第二版笔记(1)

    1.模块 集合 视图 和事件的一个综合例子 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  2. 基于pymysql模块的增删改查

    上课笔记 重点:(熟练)多表查询创建存储过程原生sql索引原理 pymysql 封装好的客户端cursor 底层就是一个send操作commit 告诉mysql真的要完成修改操作(不然修改不会生效)e ...

  3. viewport实现html页面动态缩放/meta viewport/viewport

    页面默认缩放比例为1,最小宽度为375px,在小于375px出现水平滚动条的时候重新计算显示比例缩小界面, <!DOCTYPE html> <html lang="en&q ...

  4. ios 从相册视频中获取视频截图

    //给image添加个分类 +(UIImage *)getImage:(NSURL: *)videoURL { AVURLAsset *asset = [[AVURLAsset alloc] init ...

  5. RecyclerView 缓存机制学习笔记2

    RecyclerView 初始化所有的视图后,调用 去缓存(StaggeredGridLayoutManager), 而不是初始化一次缓存一次 存储后系统又会去调用tryGetViewHolderFo ...

  6. Keil简介

    最早接触Keil是学习开发8051系列的单片机.Keil C51是Keil公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性.可维护性上有明显的优势,因而易学易 ...

  7. 【HEVC帧间预测论文】P1.6 A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence

    A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence <HEVC标准介绍.HEVC帧间预测论文笔记&g ...

  8. 从零开始部署小型企业级虚拟桌面 -- Vmware Horizon View 6 For Linux VDI

    环境说明 注,本套环境所用机器全部是64位的. 管理服务器载体:安装win7操作系统,通过VMware Workstation安装4台虚拟机,用作vCenter,Connection Server,D ...

  9. 使用Recast.AI创建具有人工智能的聊天机器人

    很多SAP顾问朋友们对于人工智能/机器学习这个话题非常感兴趣,也在不断思考如何将这种新技术和SAP传统产品相结合.Jerry之前的微信公众号文章C4C和微信集成系列教程曾经介绍了Partner如何利用 ...

  10. 原创:Nginx反向代理实战部署

    均衡负载服务器 10.0.0.9 [root@web03 conf]# vim nginx.conf worker_processes  1; events { worker_connections  ...