A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4307    Accepted Submission(s): 2586

Problem 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
 
Author
linle
 
Source
 
题意:
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(k)。计算结果%m;
代码:
 //普通递推会超时,用矩阵乘法。构造矩阵时右上角的(n-1)*(n-1)矩阵置为单位矩阵,第n行从 右 到 左 填入系数,快速幂之后第n行作为行矩阵乘递推式从 小 到 大 排列的列矩阵得到结果
//或者构造左下角的单位矩阵,第一行从 左 到 右 填入系数,快速幂之后第1行作为行矩阵乘递推式从 大 到 小 排列的列矩阵得到结果
#include<bits\stdc++.h>
using namespace std;
int a[],f[]={,,,,,,,,,};
int k,m;
struct Lu
{
int mp[][];
}L1;
void init()
{
memset(L1.mp,,sizeof(L1.mp));
for(int i=;i<=;i++)
{
L1.mp[i][i+]=;
}
for(int i=;i<=;i++)
{
L1.mp[][i]=a[-i];
}
}
Lu mult(Lu a,Lu b)
{
Lu c;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
c.mp[i][j]=;
for(int k=;k<=;k++)
c.mp[i][j]+=(a.mp[i][k]*b.mp[k][j])%m;
c.mp[i][j]%=m;
}
return c;
}
Lu solve(int x)
{
if(x==)
return L1;
if(x&)
{
Lu q=solve(x-);
return mult(q,L1);
}
else
{
Lu q=solve(x/);
return mult(q,q);
}
}
int main()
{
while(scanf("%d%d",&k,&m)!=EOF)
{
for(int i=;i<=;i++)
scanf("%d",&a[i]);
if(k<)
{
printf("%d\n",k%m);
continue;
}
init();
Lu tem=solve(k-);
int ans=;
for(int i=;i<=;i++)
ans+=(tem.mp[][i]*f[i])%m;
printf("%d\n",ans%m);
}
return ;
}
 //普通递推会超时,用矩阵乘法。
#include<bits\stdc++.h>
using namespace std;
int a[],f[]={,,,,,,,,,};
int k,m;
struct Lu
{
int mp[][];
}L1;
void init()
{
memset(L1.mp,,sizeof(L1.mp));
for(int i=;i<=;i++)
{
L1.mp[i][i-]=;
}
for(int i=;i<=;i++)
{
L1.mp[][i]=a[i];
}
}
Lu mult(Lu a,Lu b)
{
Lu c;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
c.mp[i][j]=;
for(int k=;k<=;k++)
c.mp[i][j]+=(a.mp[i][k]*b.mp[k][j])%m;
c.mp[i][j]%=m;
}
return c;
}
Lu solve(int x)
{
if(x==)
return L1;
if(x&)
{
Lu q=solve(x-);
return mult(q,L1);
}
else
{
Lu q=solve(x/);
return mult(q,q);
}
}
int main()
{
while(scanf("%d%d",&k,&m)!=EOF)
{
for(int i=;i<=;i++)
scanf("%d",&a[i]);
if(k<)
{
printf("%d\n",k%m);
continue;
}
init();
Lu tem=solve(k-);
int ans=;
for(int i=;i<=;i++)
ans+=(tem.mp[][i]*f[i])%m;
printf("%d\n",ans%m);
}
return ;
}

*HDU 1757 矩阵乘法的更多相关文章

  1. hdu 1757 矩阵

    用矩阵表示状态,矩阵乘法的就是状态之间的变换 作一个vector: 要求的就是一个矩阵A,使得上面那个vector乘以A之后变成 解得A= [不知道用逆矩阵能不能直接求出A Ref:http://bl ...

  2. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  3. hdu 1757 矩阵快速幂 **

    一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...

  4. HDU 1757 矩阵快速幂加速递推

    题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...

  5. HDU 1757 矩阵求第n的递推式

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

  6. hdu 1757 矩阵连乘

  7. Hdu 4920矩阵乘法(内存访问的讲究)

    题目链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K ( ...

  8. hdu 3483 矩阵乘法

    这个题目上周对抗赛题目,搞了我好久 对数学这种不是很敏感 其实都不是自己想出来的,看其他的资料和博客的推导 还是有点难度的,反正我是推不出来 通过二项式定理的化简 有两个博客写得比较好 http:// ...

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

随机推荐

  1. [Sass]混合宏

    [Sass]混合宏-声明混合宏 如果整个网站中有几处小样式类似,比如颜色,字体等,在 Sass 可以使用变量来统一处理,那么这种选择还是不错的.但当你的样式变得越来越复杂,需要重复使用大段的样式时,使 ...

  2. [Sass]局部变量和全局变量

    [Sass]局部变量和全局变量 Sass 中变量的作用域在过去几年已经发生了一些改变.直到最近,规则集和其他范围内声明变量的作用域才默认为本地.如果已经存在同名的全局变量,从 3.4 版本开始,Sas ...

  3. Shell 脚本实现随机抽取班级学生

    #/bin/bash function rand(){ min=$ max=$(($-$min+)) num=$(date +%s%N) echo $(($num%$max+$min)) } rnd= ...

  4. ***HTML +CSS 总结与归纳

    一.首先W3C标准 结构.表现.动作  与  html.css.javascript相对应,它本意是结构表现分离,而且按照html规范编写结构. 标签方面: -所有标签都要小写.关闭.并且合理嵌套,i ...

  5. Mac/IOS/linux获取当前时间包含微秒毫秒的代码

    #include <sys/time.h> 1 struct UnityLocalTimeStat { int Year; int Month; int DayOfWeek; int Da ...

  6. 开发日志_Jan.6.2017

    Github Jan.2 接到了汤松岩的GUI框架,开始复习和学习C++的使用方法(之前的开发经历主要使用的Java,对C++和QT环境都需要一个再了解). Jan.3 正式开始工作.开始在Ubunt ...

  7. Asp.net中static变量和viewstate的使用方法(谨慎)

    在.Net平台下进行CS软件开发时,我们经常遇到以后还要用到某些变量上次修改后的值,为了简单起见,很多人都习惯用static来定义这些变量,我也是.这样非常方便,下一次调用某个函数时该变量仍然保存的是 ...

  8. BZOJ2498 : Xavier is Learning to Count

    考虑容斥,通过$Bell(p)$的时间枚举所有等价情况. 对于一种情况,强制了一个等价类里面的数都要相同,其它的可以相同也可以不同. 这方案数显然可以通过多项式乘法求得,乘上容斥系数$(-1)^{p- ...

  9. BZOJ3197 & 组合乱搞

    Description    求\[\sum_{i = 1}^{n}i^m m^i , m \leq 1000 \] 的值.Solution    From Miskcoo's Space:      ...

  10. python基础02 基本数据类型

    摘要:简单的数据类型以及赋值 变量不需要声明 python的变量不需要声明,你可以直接输入: >>>a = 10 那么你的内存里就有了一个变量a, 它的值是10,它的类型是integ ...