A Simple Math Problem(HDU 1757 构造矩阵)
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 .

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int k,n;
struct Matrix
{
int mat[][];
}p;
int aa[];
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])%n;
}
}
return c;
}
Matrix mod_pow(Matrix x,int n)
{
Matrix res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<;i++)
res.mat[i][i]=;
while(n)
{
if(n&)
res=mul(res,x);
x=mul(x,x);
n>>=;
}
return res;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d%d",&k,&n)!=EOF)
{
if(k<)
{
printf("%d\n",k);
continue;
}
memset(p.mat,,sizeof(p.mat));
for(int i=;i<;i++)
cin>>p.mat[][i];
for(int i=;i<;i++)
p.mat[i][i-]=;
Matrix ans= mod_pow(p,k-);
/*for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
cout<<ans.mat[i][j]<<" ";
cout<<endl;
}*/
int sum=;
for(int i=;i<;i++)
sum+=ans.mat[][i]*(-i);
printf("%d\n",sum%n);
}
}
A Simple Math Problem(HDU 1757 构造矩阵)的更多相关文章
- 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 ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU 1757 A Simple Math Problem (矩阵乘法)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过
A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...
- hdu - 1757 - A Simple Math Problem
题意:当x < 10时, f(x) = x: 当x >= 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) + + a2 * f(x-3) + …… + a9 ...
- A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- GPIO的8种模式详解
和stm32的GPIO相关的寄存器有 (1)两个32位的配置寄存器(GPIOX_CRL,GPIOX_CRH) 每一个IO占用4位,16个IO占用64位就是两个32为寄存器. 其中CNF[1:0]是用来 ...
- 关于c语言不定参数的研究
一. 学习过程 编写程序如下: 编译连接并用debug加载,观察main函数的内容: Showchar函数的内容: 观察发现,main函数要传递两个参数‘a’和2,在汇编代码中是先将2赋给ax,再将a ...
- 方便john破解linux密码批处理
Title:方便john破解linux密码批处理 -- 2011-11-23 17:31 自定义文件HASH名和字典名批处理(单一文件): @echo offcolor 0asetlocal enab ...
- CSS定位属性Position详解
CSS中最常用的布局类属性,一个是Float(CSS浮动属性Float详解),另一个就是CSS定位属性Position. 1. position:static 所有元素的默认定位都是:position ...
- Qt中 QString 和int, char等的“相互”转换,关键是QString.toLocal8Bit().data();
Qt中 int ,float ,double转换为QString 有两种方法 1.使用 QString::number(); 如: long a = 63; QString s = QString:: ...
- 优化大型复杂SQL
with aa as (select a.agmt_id, sum(c.acct_bal) as card_bal, --借记卡期末存款余额 a.card_open_org, a.OPEN_DATE, ...
- ~/.bashrc的常用alias设置
centos6.5系统中,alias定义在/etc/bashrc,分别写在/etc/profile.d/*.sh中,可以在此目录添加my.sh, alias attrib='chmod'alias c ...
- 【转】 Spinner的常用技巧
原文网址:http://blog.csdn.net/wanli_smile/article/details/6410388 用法 1 :以资源方式,静态展示 Spinner 选项 1. 在资源 ...
- Android录音服务
MediaRecorder Mediarecorder=new MediaRecorder();//创建一个MediaRecorder对象 Mediarecorder.setAudioSource(M ...
- 九度OJ1486 /POJ 1029/2012北京大学研究生复试上机
wa到死!wa到死!这是一个看着简单,坑及其多的题! 坑一:POJ上是单组输入,九度上是多组输入,妈蛋要是研究生复试遇到这种大坑肯定死掉啊!而且对于codeforces比较习惯的 同学肯定会觉得巨坑无 ...