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. STM32F412应用开发笔记之五:结合W5500实现以太网通讯

    因实际使用需求我们测试一下网络通讯,在NUCLEO-F412ZG测试板上没有以太网部分,我们选择外接一个W5500的实验板.W5500支持SPI接口通讯,DC3.3V供源.而NUCLEO-F412ZG ...

  2. 使用getParts()上传多个文件

    <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo ...

  3. 集合 LinkedList、ArrayList、Set、Treeset

    LinkedList中特有的方法: 1:方法介绍 addFirst(E e) addLast(E e) getFirst() getLast() removeFirst() removeLast() ...

  4. UIStackView before iOS9.0

    我用的Xcode8.1,同伴用的Xcode7.3.1,其上传了几个XIB文件,导致我这边项目一直爆红,爆红信息:"UIStackView before iOS9.0".如图: 网上 ...

  5. QT_地图导航

    //地图显示功能 #ifndef MAPWIDGET_H #define MAPWIDGET_H #include <QGraphicsView> #include <QLabel& ...

  6. iOS-APP提交上架流程(新手必看!2016年3月1日最新版)

    自己的经验总结,有错的话请留言,第一时间更改. 先大概说一下iOSAPP上架的几个步骤(详细步骤见下图): 创建证书请求文件 登录苹果开发者中心生成发布者证书(下载下来要双击一下) 设置APPID(要 ...

  7. Android Studio上NDK/JNI开发环境问题

    基础环境: 操作系统 —— Windows 7 Android Studio —— 1.5.1(android-studio-bundle-141.2456560-windows.exe) NDK — ...

  8. UnrealScript语言基础

    总体特征 (1)大小写不敏感.关键字.宏.变量名.函数名以及类名不区分大小写:变量名可以与关键字同名 (2)局部变量.成员变量未初始化时,会被编译器初始化 (3)没有全局变量.全局函数,所有东西必须写 ...

  9. 使用Word 2013发布cnblogs随笔

    博客园支持Word或者OneNote一键发布文章. 获取cnblogs的URL地址,类似http://rpc.cnblogs.com/metaweblog/your_name 打开word中的管理账户 ...

  10. 表单验证<AngularJs>

    常用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,只要在输入字段元素上添加HTML5标记required即可: <input type="text" requir ...