http://acm.hdu.edu.cn/showproblem.php?pid=1757

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
 
题目解析:
前面已经写了一篇博客如何构造矩阵,这道题可以说就是上片博客的简单应用。
矩阵的乘法不满足交换律,但是却满足结合律,如:A*B*C=A*(B*C);

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(矩阵快速幂)的更多相关文章

  1. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  3. 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 ...

  4. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  5. 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 > ...

  6. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. 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 ...

  8. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  9. 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 & ...

  10. 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 > ...

随机推荐

  1. [转]wcout输出中文却不显示出来

    准备使用UNICODE来写个控制台测试程序发现,cout无法输出UNICODE的中文字符.查找C++标准看到,其提供了wcin.wcout.wcerr.wclog用于处理wchar_t字符的输入输出. ...

  2. MySQL<数据库和表的基本操作>

    数据库和表的基本操作 数据库基础知识 创建数据库 就是在数据库系统中划分一块存储数据的空间 CREATE DATABASE itcast; 查看数据库 SHOW CREATE DATABASE 数据库 ...

  3. js检查浏览器是否处于隐身模式

    网上大部分的文章写隐身模式下 localStorage 对象不可用,直接以 localStorage 能否写入来判断浏览器是否处于隐身模式其实是错的,在隐身模式下localStorage也是能使用的, ...

  4. jquery 获取当前时间加180天

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. Ubuntu13.10:[3]如何开启SSH SERVER服务

    作为最新版本的UBUNTU系统而言,开源,升级全部都不在话下.传说XP已经停止补丁更新了,使用UBUNTU也是一个很好的选择.ubuntu默认安装完成后只有ssh-agent(客户端模式),宾哥百度经 ...

  6. Win7下使用Putty代替超级终端通过COM串口连接开发板方法

    1.如果电脑(笔记本)没有串口接口,则需要使用一个 USB-Serial 转换线,这里使用 prolific usb-serial USB--串口转换线,首先需要在win7上安装对应的 USB--串口 ...

  7. FastCGI中fastcgi_param 详细说明

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param  QUERY ...

  8. iOS实现传递不定长的多个参数

    我们在使用苹果官方的文档的时候会发现可传不定数的参数例如: // [[UIAlertView alloc]initWithTitle:<#(nullable NSString *)#> m ...

  9. java基础---->java中国际化的实现

    应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产.开发这样的程序的过程,就称为国际化.今天,我们就开始学习java中国际化的代码实现. Java国际化主要通过如下3个类 ...

  10. openstack 爬坑日记

    这个问题官方文档应该付全部责任.​ 关于 endpoint regionOne 问题 ​官方文档的所有endpoint 创建命令都是使用的regionOne,但是这个配置项必须和相关的组件ini 文件 ...