题目地址:HDU 1757

最终会构造矩阵了。事实上也不难,仅仅怪自己笨。。= =!

f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10)

构造的矩阵是:(我代码中构造的矩阵跟这个正好是上下颠倒过来了)

|0 1 0 ......... 0|    |f0|   |f1 |

|0 0 1 0 ...... 0|    |f1|   |f2 |

|...................1| *  |..| = |...|

|a9 a8 .......a0|    |f9|   |f10|

然后依据矩阵的结合律,能够先把构造的矩阵的(k-9)次幂求出来。最后直接求第一个数。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
int mod, a[20];
struct matrix
{
int ma[20][20];
} init, res, s;
matrix Mult(matrix x, matrix y)
{
int i, j, k;
matrix tmp;
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
tmp.ma[i][j]=0;
for(k=0; k<10; k++)
{
tmp.ma[i][j]=(tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod;
}
}
}
return tmp;
}
matrix Pow(matrix x, int k)
{
matrix tmp;
int i, j;
for(i=0; i<10; i++) for(j=0; j<10; j++) tmp.ma[i][j]=(i==j);
while(k)
{
if(k&1) tmp=Mult(tmp,x);
x=Mult(x,x);
k>>=1;
}
return tmp;
}
int main()
{
int k, x, i, j;
while(scanf("%d%d",&k,&mod)!=EOF)
{
if(k<10)
{
printf("%d\n",k%mod);
continue ;
}
for(i=9; i>=0; i--)
{
a[i]=9-i;
}
for(i=0; i<10; i++)
{
scanf("%d",&x);
init.ma[0][i]=x%mod;
}
for(i=1; i<10; i++)
{
for(j=0; j<10; j++)
{
init.ma[i][j]=(i==j+1);
}
}
res=Pow(init,k-9);
/*for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
printf("%d ",res.ma[i][j]);
}
puts("");
}*/
int ans=0;
for(j=0; j<10; j++)
{
ans=(ans+res.ma[0][j]*a[j])%mod;
//printf("%d %d %d\n",res.ma[i][j],a[i],ans);
}
printf("%d\n",ans);
}
return 0;
}

HDU 1757 A Simple Math Problem(矩阵高速幂)的更多相关文章

  1. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

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

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  3. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  4. HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...

  5. hdu 1757 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. HDU1757 A Simple Math Problem 矩阵快速幂

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

  8. HDU 1757 A Simple Math Problem(矩阵快速幂)

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...

  9. hdu 1757 A Simple Math Problem (矩阵快速幂)

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

随机推荐

  1. Unity 关于属性的get/set

    学习Unity的可能多数是C#转过来的, 一进来的时候你会发现Unity编写代码,在一些视频或文章中.基本都没有用过get/set使用, 多数是public string name;这样写的公开字段, ...

  2. C,C#,C++中&&和||,&和|的联系和区别

    本文来自:http://www.cnblogs.com/GT_Andy/archive/2010/03/30/1921805.html 两者计算结果相同(针对各自的运算对象),只是性能上有差别而已. ...

  3. 【计算几何初步-凸包-Graham扫描法-极角序】【HDU1348】 WALL

    Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. css设置

    box-size允许您以特定的方式定义匹配某个区域的特定元素. content-box(默认):宽度和高度分别应用到元素的内容框.在宽度和高度之外绘制元素的内边距和边框.border-box:为元素设 ...

  5. C/c++几个预定义的宏:__DATE__,__TIME__,__FILE__,__LINE__

    一边情况下,C/C++编译器会内置几个宏,这些宏定义不仅可以帮助我们完成跨平台的源码编写,灵活使用也可以巧妙地帮我们输出非常有用的调试信息. ANSI C标准中有几个标准预定义宏(也是常用的): __ ...

  6. mysql单表大小的限制

    mysql单表大小的限制一.MySQL数据库的MyISAM存储 引擎单表大小限制已经不是有MySQL数据库本身来决定(限制扩大到64pb),而是由所在主机的OS上面的文件系统来决定了.在mysql5. ...

  7. 模拟Spring依赖注入

    通过读取xml文件,利用反射动态加载类和方法,其实就是spring的注入机制模拟,可以清晰的看出整个运行流程 1.配置文件 applicationContext.xml <beans> & ...

  8. SquirrelMQ消息队列

    SquirrelMQ是一个快速的消息队列. SquirrelMQ特性: 1. SquirrelMQ使用Slab内存分配算法来降低内存碎片,使用epoll来解决高并发问题.效率比redis要高,使用简单 ...

  9. API例子:用Python驱动Firefox采集网页数据

    1,引言 本文讲解怎样用Python驱动Firefox浏览器写一个简易的网页数据采集器.开源Python即时网络爬虫项目将与Scrapy(基于twisted的异步网络框架)集成,所以本例将使用Scra ...

  10. externn "C"解析

    1.揭密extern "C" extern "C"包含双重含义,从字面上即可得到:首先,被它修饰的目标是 "extern”的:其次,被它修饰的目标是 ...