Description

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
 

题意:按f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10) (x>=10) ; f(x) = x(x<10)来计算f(x)%m的值。

分析:这题要用递推,并且k值很大,所以需要用矩阵快速幂。

构造的矩阵是:

a0 a1 a2 a3 a4 a5 a6 a7 a8 a9
1                  
  1                
    1              
      1            
        1          
          1        
            1      
              1    
                1  
*
f(x-1)
f(x-2)
f(x-3)
f(x-4)
f(x-5)
f(x-6)
f(x-7)
f(x-8)
f(x-9)
f(x-10)
=
f(x)
f(x-1)
f(x-2)
f(x-3)
f(x-4)
f(x-5)
f(x-6)
f(x-7)
f(x-8)
f(x-9)

写个结构类型代表矩阵,以及矩阵的相乘的函数和矩阵快速幂的函数,注意一下初始化。

#include<stdio.h>
#include<string.h>
int n,k,m;
struct matrix
{
int a[][];
int row,col;
void init(int r,int c){
memset(a,,sizeof(a));
row=r;col=c;
}
} big,f,u;
matrix mul(matrix a,matrix b)
{
matrix c;
c.init(a.row,b.col);
for(int i=; i<a.row; i++)
for(int j=; j<b.col; j++)
{
for(int k=; k<a.col; k++)
c.a[i][j]+=(a.a[i][k]*b.a[k][j])%m;
c.a[i][j]%=m;
}
return c;
}
void init()
{
big.init(,);
f.init(,);
for(int i=; i<; i++)
big.a[i][i-]=;
for(int i=; i<; i++)
f.a[i][]=-i;
}
matrix qpow(matrix a,int k)
{
matrix ans;
ans.init(a.row,a.col);
for(int i=;i<a.row;i++)
ans.a[i][i]=;
while(k)
{
if(k&)ans=mul(ans,a);
a=mul(a,a);
k>>=;
}
return ans;
}
int main()
{
init();
while(~scanf("%d%d",&k,&m))
{
for(int i=; i<; i++)
scanf("%d",&big.a[][i]);
u=mul(qpow(big,k-),f);
printf("%d\n",u.a[][]%m);
}
return ;
}

【HDU 1757】 A Simple Math Problem的更多相关文章

  1. 【hdu 2486】A simple stone game

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  2. 【HDOJ5974】A Simple Math Problem(构造,解方程)

    题意:给定A与B,要求构造出一组X,Y,使得X+Y=A,lcm(X,Y)=B A<=2e4,B<=1e9 思路:A的范围较小,考虑以A为突破口 枚举A的约数k,复杂度O(sqrt(A)) ...

  3. 【HDU 5399】Too Simple

    题 Description Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai th ...

  4. 【hdu 5628】Clarke and math (Dirichlet卷积)

    hdu 5628 Clarke and math 题意 Given f(i),1≤i≤n, calculate \(\displaystyle g(i) = \sum_{i_1 \mid i} \su ...

  5. 【bzoj 3489】A simple rmq problem

    题目 \(kdt\)就是数点神器 我们先扫两遍处理出每个数上一次出现的位置\(pre_i,nxt_i\),之后变成\((i,pre_i,nxt_i)\)这样一个三维空间上的点 就变成了求一个立方体的最 ...

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

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

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

  8. hdu 1757 A Simple Math Problem (乘法矩阵)

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

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

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

随机推荐

  1. 第23章 SEH结构化异常处理(1)_系统SEH机制

    23.1 基础知识 23.1.1 Windows下的软件异常 (1)中断和异常 ①中断是由外部硬件设备或异步事件产生的 ②异常是由内部事件产生的,可分为故障.陷阱和终止三类. (2)两种异常处理机制: ...

  2. NGUI图片字(Bitmap图片转文字)

    用图片字而不是图片 美术和程序的配合,需要程序能够很快抓住问题重点并提出解决方案.美术出的图片字比我们使用的字体更好好看,那么是否要一个个图片去拼成数字呢? NGUI创建图片字 准备材料 美术提供的数 ...

  3. finder怎么才能找到library

    右键Finder——前往目录 输入~/Library

  4. 报错"the geometry has no Z values"处理

    );  //将Z值设置为0 //IPoint point = (IPoint)pGeo; //point.Z = 0; } else            {                IZAwa ...

  5. 08SpringMvc_(1)继承AbstractCommandController的Action[能够以实体的形式,收集客户端参数].(2)日期转换器和编码过滤器

    上一篇文章说过要介绍两个控制器.这篇文章就介绍第二个控制器AbstractCommandController(这个类已经快要被废弃了,有更好的代替者,但还是要好好学这个类).这个控制器的额作用是为了收 ...

  6. 使用SilverLight开发区域地图分析模块

    本人最近接收开发一个代码模块,功能主要是在页面上显示安徽省市地图,并且在鼠标移动到地图某市区域时,显示当前区域的各类信息等,一开始准备用百度地图,高德地图等地图工具进行开发,最后发现都不适合进行此类开 ...

  7. c语言 &取地址运算符的理解

    对于c语言中的&运算符,百度百科是这样定义的:(&p)则是这样一种运算,返回当时声明p 时开辟的地址:但是根据我对代码的观察,我觉得&运算符不只是返回地址的功能: 例如: in ...

  8. list to csv

    import csv # ============================== # list to csv # ============================== a = [1,2, ...

  9. Caffe学习系列(17):模型各层数据和参数可视化

    cifar10的各层数据和参数可视化 .caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1p ...

  10. 信息安全系统设计基础实验五:简单嵌入式 WEB 服务器实验 (20135229,20135234)

    http://www.cnblogs.com/mqy123/p/5002683.html