hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2791 Accepted Submission(s): 1659
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.
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.
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
104
//#define LOCAL
#include<cstdio>
#include<cstring>
#define LL __int64
using namespace std;
const int maxn=;
LL k,m;
int aa[maxn],mat[maxn][maxn];
int ans[maxn][maxn]; void init()
{
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(i==)
mat[i][j]=aa[j];
else
if(i==j+)mat[i][j]=;
else mat[i][j]=;
if(i==j)
ans[i][j]=;
else ans[i][j]=;
}
}
} void Matrix(int a[][],int b[][])
{ int c[][];
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
c[i][j]=;
for(int k=;k<;k++)
{
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;
}
}
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
a[i][j]=c[i][j];
}
}
} void pow(LL n)
{
while(n>)
{
if(n&) Matrix(ans,mat);
n>>=1L;
if(n==)break;
Matrix(mat,mat);
}
} int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif while(scanf("%I64d%I64d",&k,&m)!=EOF)
{
for(int i=;i<;i++)
scanf("%d",aa+i);
if(k<) printf("%I64d\n",k%m);
else
{
init();
pow(k-);
int res=;
for(int i=;i<;i++)
res=(res+(-i-)*ans[][i])%m;
printf("%d\n",res);
}
}
return ;
}
hdu------(1757)A Simple Math Problem(简单矩阵快速幂)的更多相关文章
- hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
- HDU 1757 A Simple Math Problem(矩阵快速幂)
题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...
- hdu 1757 A Simple Math Problem (矩阵快速幂)
Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 ...
- HDU 1757 A Simple Math Problem( 矩阵快速幂 )
<font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...
- hdu 1757 A Simple Math Problem(矩阵快速幂乘法)
Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...
- HDU 1757 A Simple Math Problem(矩阵快速幂模板)
题意:题意很简单,不多说了. 思路: |f(10) | |a0 a1 a2 ...a8 a9| |f(9)|| f(9) | | 1 0 0 ... 0 ...
- HDU 1757 A Simple Math Problem(矩阵高速幂)
题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...
- hdu 1757 A Simple Math Problem (矩阵高速幂)
和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...
- HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过
A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...
随机推荐
- 【转载】ADO,OLEDB,ODBC,DAO的区别
原文:ADO,OLEDB,ODBC,DAO的区别 ODBC(Open Database Connectivity,开放数据库互连) 1992年,微软公司开放服务结构(WOSA,Windows Open ...
- Codeforces Round #257 (Div. 2) A题
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Comware 架构理解
网络操作系统 首先什么是网络操作系统: 一种说法是:运行在路由器,网络交换机,防火墙上的特别的操作系统 另一种说法是:部署在局域网或者私有网络,允许网络中的多个计算机共享文件和打印机,因为现在的单机系 ...
- hdu 4165 Pills dp
Pills Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem De ...
- Javascript 简单学习
一直就崇拜JS如此牛叉的来操作Html标签, 抽出时间来学习学习,哈哈. Js是Netscape公司语言, 基于对象和事件驱动. 与Java不同之处: 1:所属公司不同 2:js是基于对象,java是 ...
- [转载] 深入 superviser
模块简介 新人接触线上的时候一般都会碰到supervise这个工具,导师对这个模块的解释一般就是,这个模块是监控进程,当进程挂掉之后,supervise会将进程启动.这样当进程出 现问题,如因出cor ...
- poj1819Disks
链接 题意:从左到右按顺序给你n个圆的半径,把左右两边想象成两堵墙的话,就是左右两边向里挤压,问哪些圆是对最后的宽度不影响. 刚开始理解错了,..以为怎么放圆使宽度最小.. 这样就可以尽量使每个圆向左 ...
- CnPlugin 1.5.400
本软件CnPlugin是PL/SQL Developer工具插件,支持PL/SQL Developer 7.0以上版本.增加了PL/SQL Developer工具本身所没有的一些小功能,功能基本一些已 ...
- 定义函数def
- (Clob的写入和读取-java)更新数据库报错:SQL Error: 1461, SQLState: 72000 ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
原 因:某一个字段本为varchar2(1024),但是实际要插入的值超过varchar2允许的最大长度4000时,oracle自动将该字段值转化为Long类型,然后提示插入操作失败. 解决办法: 1 ...