HDU1757:A Simple Math Problem(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=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 .
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.
f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10)
构造的矩阵是:
|0 1 0 ......... 0| |f0| |f1 |
|0 0 1 0 ...... 0| |f1| |f2 |
|...................1| * |..| = |...|
|a9 a8 .......a0| |f9| |f10|
然后根据矩阵的结合律,可以先把构造的矩阵的K次幂求出来。最后直接求第一个数。
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct ma
{
int a[][];
} init,res;
int K;
int mod,b[],f[];
ma Mul(ma x,ma y)
{
ma tmp;
for(int i=; i<; i++)
for(int j=; j<; j++)
{
tmp.a[i][j]=;
for(int k=; k<; k++)
tmp.a[i][j]=(tmp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
}
return tmp;
}
ma Pow(ma x,int K)
{
ma tmp;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
tmp.a[i][j]=(i==j);
}
while(K!=)
{
if(K&)
tmp=Mul(tmp,x);
K>>=;
x=Mul(x,x);
}
return tmp;
}
int main()
{
while(scanf("%d%d",&K,&mod)!=EOF)
{
for(int i=; i<=; i++)
{
scanf("%d",&init.a[][-i]);
}
if(K<=)
{
printf("%d\n",K);
continue;
}
for(int i=; i<; i++)
f[i]=i;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
init.a[i][j]=(i==j-);
}
res=Pow(init,K);
int ans=;
for(int j=; j<; j++)
{
ans=(ans+res.a[][j]*j)%mod;
}
printf("%d\n",ans); }
return ;
}
加深印象,写了两次。
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct ma
{
int a[][];
} init,res;
int K;
int mod,b[],f[];
ma Mul(ma x,ma y)
{
ma tmp;
for(int i=; i<; i++)
for(int j=; j<; j++)
{
tmp.a[i][j]=;
for(int k=; k<; k++)
tmp.a[i][j]=(tmp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
}
return tmp;
}
ma Pow(ma x,int K)
{
ma tmp;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
tmp.a[i][j]=(i==j);
}
while(K!=)
{
if(K&)
tmp=Mul(tmp,x);
K>>=;
x=Mul(x,x);
}
return tmp;
}
int main()
{
while(scanf("%d%d",&K,&mod)!=EOF)
{
for(int i=; i<=; i++)
{
scanf("%d",&init.a[][-i]);
}
if(K<=)
{
printf("%d\n",K);
continue;
}
for(int i=; i<; i++)
f[i]=i;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
init.a[i][j]=(i==j-);
}
res=Pow(init,K-);
int ans=;
for(int j=; j<; j++)
{
ans=(ans+(res.a[][j])*f[j])%mod;
}
printf("%d\n",ans); }
return ;
}
HDU1757: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 ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- 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_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- hdu-1757 A Simple Math Problem---矩阵快速幂模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)
GTY's math problem Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- A Simple Math Problem 矩阵打水题
A Simple Math Problem Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x & ...
- hdu1757 A Simple Math Problem
Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x > ...
随机推荐
- mysql数据库中,通过mysqldump工具仅将某个库的所有表的定义进行转储
需求描述: 在研究mysqldump工具的使用,想的是如何将某个库下的,或者某个表的表的定义(表结构创建语句)进行转储 操作过程: 1.通过--no-data参数,就可以将某个库的表定义进行转储 [m ...
- ubuntu压缩
.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...
- oracle_存储过程_没有参数_根据配置自动创建申请单以及写日志事务回滚
CREATE OR REPLACE PROCEDURE A_MEAS_MIINSP_PLAN_CREATEASvs_msg VARCHAR2(4000);p_PERIODTYPE number; -- ...
- HDU 4497 GCD and LCM (分解质因数)
链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4497 假设G不是L的约数 就不可能找到三个数. L的全部素因子一定包括G的全部素因子 而且次方数 ...
- JVM优化(一)-- 入门
一.JVM的概念 JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现 ...
- U3D关于message的使用
Message相关有3条指令: SendMessage ("函数名",参数,SendMessageOptions) //GameObject自身的Script BroadcastM ...
- mybatis 返回值类型是Map
<select id="selectByMemberKey" resultType="java.util.HashMap"> SELECT memb ...
- CentOS-6.3安装配置Nginx--【测试已OK】
安装说明 系统环境:CentOS-6.3软件:nginx-1.2.6.tar.gz安装方式:源码编译安装 安装位置:/usr/local/nginx 下载地址:http://nginx.org/en/ ...
- piblog 0.2
在一个Web App中,所有的数据,包括用户的信息,日志,评论等,都存在数据库中.在piblog中使用MySQL作为数据库.Web App中由很多地方需要使用数据库.访问数据库需要创建数据库连接.游标 ...
- 重装Delphi10.2的IDE必要设置
重装Delphi10.2的IDE必要设置: 1,Tools->Options Editor Options->Display 右侧的 Right margin: 设为200 这个设置是为右 ...