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. NGUI3.x Button事件

    代码调用事件 UI搭建 1.首先创建一个Button,和一个的GameObeject命名为GUIClikEvent 2.方法一:创建一个NGUIClickTest.cs,绑定在Button上 Clic ...

  2. Linux压力测试工具Tsung安装、使用和图形报表生成

    简介 Tsung 是一个压力测试工具,可以测试包括HTTP, WebDAV, PostgreSQL, MySQL, LDAP, and XMPP/Jabber等服务器.针对 HTTP 测试,Tsung ...

  3. Linux搭建PHP+MySQL+Apache环境

    环境: CentOS 6.4 MySQL详细安装可参考:http://www.cnblogs.com/yangxia-test/archive/2012/11/12/2766237.html Apac ...

  4. Go 命令之 godep

    本文参考:http://www.cnblogs.com/me115/p/5528463.html#h20 http://studygolang.com/articles/4385 关于Godep 发现 ...

  5. php基础26:文件与目录1

    <meta charset="utf-8"> <?php //绝对路径 $path = "E:\AppServ\www\php\/33-catalog. ...

  6. 广州传智博客黑马训练营.Net15期

    广州传智博客黑马训练营.Net15期 7 张扬波 MVC大项目 6 张扬波 MVC 3 胡凌浩 HTML&JS 2 基础加强+三层 5 张扬波 企业站点(asp.net)&EF 4 江 ...

  7. Caffe学习系列(16):caffemodel可视化

    通过前面的学习,我们已经能够正常训练各种数据了.设置好solver.prototxt后,我们可以把训练好的模型保存起来,如lenet_iter_10000.caffemodel. 训练多少次就自动保存 ...

  8. Ace 动画应用实例 ------启动欢迎界面

    仔细观察手机应用很多都用到了动画效果 那么咱们也做一个 除了位移没有使用 其他都有 布局随便放张图片 public class SplashActivity extends Activity { pr ...

  9. 流程引擎Activiti系列:如何将kft-activiti-demo-no-maven改用mysql数据库

    kft-activiti-demo-no-maven这个工程默认使用h2数据库,这是一个内存数据库,每次启动之后都要重新对数据库做初始化,很麻烦,所以决定改用mysql,主要做3件事情: 1)在mys ...

  10. [CareerCup] 12.5 Test a Pen 测试一支笔

    12.5 How would you testa pen? 这道题让我们测试一支笔,我们需要问面试官许多问题来理解"who,what,where,when,how and why" ...