hdu 1757 A Simple Math Problem(矩阵快速幂乘法)
Lele now is thinking about a simple function f(x). If x < f(x) = x.
If x >= f(x) = a0 * f(x-) + a1 * f(x-) + a2 * f(x-) + …… + a9 * f(x-);
And ai(<=i<=) can only be or . Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
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<*^ , m < ^ )
In the second line , there are ten integers represent a0 ~ a9.
For each case, output f(k) % m in one line.

把问题转化为求矩阵的n-9次幂就行了;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int k,MOD;
int a[];
int f[];
struct Matrix
{
int m[][];
}matrix; Matrix Mul(Matrix a,Matrix b)
{
Matrix res;
int i,j,k;
for(i=;i<;i++)
{
for(j=;j<;j++)
{
res.m[i][j] = ;
for(k=;k<;k++)
res.m[i][j] = (res.m[i][j]+(a.m[i][k]*b.m[k][j]))%MOD;
}
}
return res;
} Matrix fastm(Matrix a,int b)
{
Matrix res;
memset(res.m,,sizeof(res.m));
for(int i=;i<;i++)
res.m[i][i] = ;
while(b)
{
if(b&)
res = Mul(res,a);
a = Mul(a,a);
b >>= ;
}
return res;
}
void init()
{
for(int i=;i<=;i++)
{
f[i]=i;
}
}
int main()
{
init();
while(scanf("%d%d",&k,&MOD)==)
{
for(int i=;i<=;i++)
{
scanf("%d",&a[i]);
}
if(k<)
{
printf("%d\n",k%MOD);
continue;
} memset(matrix.m,,sizeof(matrix.m));
for(int i=;i<=;i++)
matrix.m[][i]=a[i];
for(int i=;i<=;i++)
matrix.m[i][i-] = ;
Matrix ans=fastm(matrix,k-);
Matrix cnt;
for(int i=;i<;i++)
{
cnt.m[i][]=f[-i];
}
Matrix p=Mul(ans,cnt);
printf("%d\n",p.m[][]%MOD);
}
return ;
}
hdu 1757 A Simple Math Problem(矩阵快速幂乘法)的更多相关文章
- 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_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列
Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 ...
- 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(矩阵快速幂)
题目链接 题意 :给你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 (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
- HDU 1757 A Simple Math Problem( 矩阵快速幂 )
<font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...
随机推荐
- Android性能优化典范【转】
2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...
- Php环境下载(PHPNow)安装
下载 From http://servkit.org/download 安装 解压下载包,双击setup.cmd,按照提示执行安装. 安装成功测试 原来的解压目录
- XMPP通讯开发-1
有关XMPP的相关知识这里就不讲解了,网上有很多,这里我使用的NetBeans+Openire+smack搭建一个以XMPP协议的通讯工具,对于这部分知识我也不是很了解,也是初识吧,可能有些概念会混淆 ...
- RMAN数据库恢复之恢复表空间和数据文件
执行表空间或数据文件恢复时,数据库既可以是MOUNT状态,也可以是OPEN状态.1.恢复表空间在执行恢复之前,如果被操作的表空间未处理OFFLINE状态,必须首先通过ALTER TABLESPACE… ...
- 【枚举+数学】【HDU1271】整数对 难度:五颗星
整数对 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 不规则三角网 Delaunay——TIN
http://blog.csdn.net/u010025211/article/details/25032209 知识点一:平面中判断一个点是否在三角形内部. #include <stdio.h ...
- MYSQL5.6数据库ZIP安装以及VS中使用注意事项
先挂资源,下载地址: http://download.softagency.net/MySQL/Downloads/. 找到MYSQL5.6,两个版本看系统而定我下的是64位的:http://down ...
- inux中tail命令---用于查看文件内容
linux中tail命令---用于查看文件内容 最基本的是cat.more和less.1. 如果你只想看文件的前5行,可以使用head命令,如:head -5 /etc/passwd2. 如果你想查看 ...
- jquery-练习-折叠效果
<!DOCTYPE html > <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...
- PHP性能优化之:配置opcache
启用opcache,并对配置参数进行优化 [opcache] zend_extension = /usr/local/php5/lib/php/extensions/no-debug-zts-2012 ...