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

求f(n)%MOD

Sample Input
10 9999 //n mod
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

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
using namespace std ; int MOD ; struct Matrix
{
int mat[][];
}; Matrix mul(Matrix a,Matrix b) //矩阵乘法
{
Matrix c;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
c.mat[i][j]=;
for(int k=;k<;k++)
{
c.mat[i][j]=(c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%MOD;
}
}
return c;
}
Matrix pow_M(Matrix a,int k) //矩阵快速幂
{
Matrix ans;
memset(ans.mat,,sizeof(ans.mat));
for (int i=;i<;i++)
ans.mat[i][i]=;
Matrix temp=a;
while(k)
{
if(k&)ans=mul(ans,temp);
temp=mul(temp,temp);
k>>=;
}
return ans;
} int main ()
{
//freopen("in.txt","r",stdin) ;
int n ;
while(cin>>n>>MOD)
{
Matrix t ;
int i , j ;
for (i = ; i < ; i++)
cin>>t.mat[][i] ;
if (n < )
{
cout<<n<<endl ;
continue ;
}
for (i = ; i < ; i++)
for (j = ; j < ; j++)
{
if (i ==(j+))
t.mat[i][j] = ;
else
t.mat[i][j] = ;
}
Matrix ans = pow_M(t,n-) ;
int sum = ;
for (i = ; i < ; i++)
{
sum = (sum + ans.mat[][i]*(-i))%MOD ;
}
cout<<sum<<endl ; } return ;
}

hdu 1757 和1005差不多 (矩阵快速幂)的更多相关文章

  1. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  2. hdu 4686 Arc of Dream(矩阵快速幂)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...

  3. HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...

  4. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  5. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  6. hdu 1575 Tr A(矩阵快速幂)

    今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午.废话不说,入正题.该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了 ...

  7. hdu 4565 So Easy!(矩阵+快速幂)

    题目大意:就是给出a,b,n,m:让你求s(n); 解题思路:因为n很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...

  8. hdu 2604 Queuing(动态规划—>矩阵快速幂,更通用的模版)

    题目 最早不会写,看了网上的分析,然后终于想明白了矩阵是怎么出来的了,哈哈哈哈. 因为边上的项目排列顺序不一样,所以写出来的矩阵形式也可能不一样,但是都是可以的 //愚钝的我不会写这题,然后百度了,照 ...

  9. hdu 1575 Tr A(矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term  类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...

随机推荐

  1. liunx必知必会(2)

    一.SSH免密登陆配置 1.相关概念 SSH 为 Secure Shell(安全外壳协议) 的缩写. 很多ftp.pop和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据,别有用 ...

  2. bzoj千题计划305:bzoj2565: 最长双回文串(回文自动机)

    https://www.lydsy.com/JudgeOnline/problem.php?id=2565 正着构造回文自动机 倒过来再构造一个回文自动机 分别求出以位置i开始的和结尾的最长回文串 # ...

  3. sizeof strlen区别于联系

    http://www.cnblogs.com/carekee/articles/1630789.html

  4. python的__mro__与__slot__

    class A(object): def __init__(self): print ' -> Enter A' print ' <- Leave A' class B(A): def _ ...

  5. js给<img>的src赋值

    用js原生方法:document.getElementById("imageId").src = "xxxx.jpg";用Jquery方法:$("#i ...

  6. oracle_集合函数

    查询10和20号部门的员工 SQL> 1. select * from emp where deptno in (10,20); SQL> 2. select * from emp whe ...

  7. 之手算KD-tree

    转自:https://zhuanlan.zhihu.com/p/27453420 本文来源于Machine Learning: Clustering & Retrieval | Courser ...

  8. 详解Jquery选择器

    1.常见的选择器 id,类,标签选择器. $("#a1") $(".myclass") $("div") 2.组合选择器 $("# ...

  9. Android常用逆向工具+单机游戏破解

    android开发环境搭建 我理解的学习路线是首先要掌握和了解常见的工具.搭建环境.然后就是缓慢的积累特征,通过长期的练习使自己进步,通过android逆向课程的学习.常用的工具如下: android ...

  10. openssl版本升级操作记录【转】

    需要部署nginx的https环境,之前是yum安装的openssl,版本比较低,如下:   [root@nginx ~]# yum install -y pcre pcre-devel openss ...