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. [已读]Sass与Compass实战

    介绍了Sass基础语法与Compass框架,这个网上参考文档就OK了,另外介绍了compass生成图片精灵和相应的css,貌似现在单纯用sass和compass的挺少,要不grunt,要不FIS,而g ...

  2. Keepalived+Nginx实现Nginx的高可用

    集群规划 主机名 IP VIP Nginx:port KeepAlived主备 KA_NG_01 192.168.30.130 192.168.30.120 8088 MASTER KA_NG_02 ...

  3. LN : leetcode 486 Predict the Winner

    lc 486 Predict the Winner 486 Predict the Winner Given an array of scores that are non-negative inte ...

  4. How to proxy a web site by apache2 in Ubuntu

    Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we ca ...

  5. 1-1 编程基础 GCC程序编译

    GCC简介      Linux系统下的gcc是GNU推出的强大.性能优越的多平台编译器,是GNU的代表作之一.gcc可以在多种硬体平台上编译出可执行程序,其执行效率与一般的编译器相比平局效率要高20 ...

  6. k近邻法(kNN)

    <统计学习方法>(第二版)第3章 3 分类问题中的k近邻法 k近邻法不具有显式的学习过程. 3.1 算法(k近邻法) 根据给定的距离度量,在训练集\(T\)中找出与\(x\)最邻近的\(k ...

  7. CPP-基础:文字常量区

    内存不可写 char* 先看一个例子 ///////////// //代码1 #include <string> main() { char *buf = "good morni ...

  8. 【软件构造】第三章第三节 抽象数据型(ADT)

    第三章第三节 抽象数据型(ADT) 3-1节研究了“数据类型”及其特性 ; 3-2节研究了方法和操作的“规约”及其特性:在本节中,我们将数据和操作复合起来,构成ADT,学习ADT的核心特征,以及如何设 ...

  9. Hibernate-04 延迟加载

    学习任务 延迟加载 Open Session In View模式 延迟加载 延迟加载(lazy load懒加载)是在真正需要数据时才执行SQL语句进行查询,避免了无谓的性能开销. 延迟加载策略的设置分 ...

  10. 数组对象分类个数js

    <script type="text/javascript"> $(function(){ var aaa = [ {"task1":"z ...