题目地址: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. PHP MySQL Where 子句 之Where

    WHERE 子句 如需选取匹配指定条件的数据,请向 SELECT 语句添加 WHERE 子句. 语法 SELECT column FROM table WHERE column operator va ...

  2. hdu1106 字符串水题strtok()&&strchr()&&sscanf()+atoi()使用

    字符串的题目 用库函数往往能大大简化代码量 以hdu1106为例 函数介绍 strtok() 原型: char *strtok(char s[], const char *delim); 功能: 分解 ...

  3. 【计算几何初步-判断是否凸多边形】【HDU2108】Shape of HDU

    Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. CentOS 5.7 中文乱码问题解决方案

    一.安装中文支持: # yum install "@Chinese Support" 二.用 yum 安装中文字体 #yum install fonts-chinese.noarc ...

  5. FFmpeg详解

    认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几乎实现了所有当下常见的数据封装格式.多媒体传输协议以及音视频编解码器.因此,对于从事多媒体技术开发的工程师来说,深入研究FFM ...

  6. C#获取本机IP方法,获取本机局域网IP地址方法

    1. private void GetIP() { string hostName = Dns.GetHostName();//本机名 //System.Net.IPAddress[] address ...

  7. 获取设备、APP的一些信息

    获取设备的一些信息: UIDevice *device = [UIDevice currentDevice]; @property(nonatomic,readonly,strong) NSStrin ...

  8. 新博客 Fighting

    C3  Viso绘图 的 几种关系

  9. 由WSDL文件生成WEB service server端C#程序(转)

    一般一个已经实现功能的WEB Server会发布自己的WSDL文件,供客户端生成代理类. 但有时是先有的server与client交互的接口定义(WSDL)文件,然后由server和client端分别 ...

  10. 整理js继承

    实现继承的方法: 一,原型链:利用原型让一个引用类型继承另一个引用类型的属性和方法 function SuperType(){ this.property=true; } SuperType.prot ...