*HDU 1757 矩阵乘法
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
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.
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 .
//普通递推会超时,用矩阵乘法。构造矩阵时右上角的(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 矩阵乘法的更多相关文章
- hdu 1757 矩阵
用矩阵表示状态,矩阵乘法的就是状态之间的变换 作一个vector: 要求的就是一个矩阵A,使得上面那个vector乘以A之后变成 解得A= [不知道用逆矩阵能不能直接求出A Ref:http://bl ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- hdu 1757 矩阵快速幂 **
一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...
- 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 ...
- HDU 1757 矩阵求第n的递推式
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 1757 矩阵连乘
- Hdu 4920矩阵乘法(内存访问的讲究)
题目链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K ( ...
- hdu 3483 矩阵乘法
这个题目上周对抗赛题目,搞了我好久 对数学这种不是很敏感 其实都不是自己想出来的,看其他的资料和博客的推导 还是有点难度的,反正我是推不出来 通过二项式定理的化简 有两个博客写得比较好 http:// ...
- 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 ...
随机推荐
- 在Eclipse中集成Ant配置
提要:本文将向你展示如何使用Eclipse设置为Ant所用的属性值和环境变量,并简要分析如何配置Ant编辑器以便从Eclipse内部操作Ant文件. 一. 修改Ant Classpath 在使用一个可 ...
- tp框架实现验证码
今天来看一个小插件. tp框架是怎么实现验证码的. 又到了我们千篇一律的时候了,首先呢,先做一个用来显示的html界面名为:zhuce.html <!DOCTYPE html PUBLIC &q ...
- Android版本与api Level
Platform Version API Level VERSION_CODE Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 ...
- thinkphp 3.2.3 session 丟失問題
之前做的几个 站session在跨页时也不会丢失(都在同一台服务器,所以我排除了服务器配置问题),这次居然很奇怪的发生的,在火狐上有,在ie, 谷哥上没有session,看了很多网上的贴子 其中有一个 ...
- RSA算法学习
package com.test.rsa; /* * 为了选择公钥和私钥,Bob必须执行如下步骤: * 1)选择两个大素数p和q.那么p和q应该多大呢?该值越大,RSA越难于破解,但是执行加密和解密所 ...
- IIS设置默认主页无效
服务器系统:Windows server 2008 R2 IIS版本:7.5 IIS中部署一个dotnet framework 3.5的网站应用程序,设置"默认文档"为:index ...
- 基于SOUI开发的应用展示
本页面列出基于SOUI开发的产品 欢迎使用SOUI的朋友提供资源:setoutsoft#qq.com #->@ 千万级平台后台在线监测客户端 1, 主页:用于显示管理服务端在线情况,左侧栏包括 ...
- 百度UEditor在线编辑器的配置和图片上传
前言 最近在项目中使用了百度UEditor富文本编辑器,配置UEditor过程中遇到了几个问题,在此记录一下解决方案和使用方法,避免以后使用UEditor出现类似的错误. 基本配置 一.下载UEdit ...
- Tomcat 9.0安装配置(转)
http://www.cnblogs.com/saratearing/p/5811866.html
- oracle函数简析
(一).数值型函数(Number Functions) 数值型函数输入数字型参数并返回数值型的值.多数该类函数的返回值支持38位小数点,诸如:COS, COSH, EXP, LN, LOG, SIN, ...