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. 下载安装JDK,并且配置java环境变量

    如果想使用java语言编写程序,就必须搭建一个java开发环境,需要安装jdk,然后配置环境变量. 一:下载jdk 现在的jdk主流的是sun公司的jdk,当然IBM和其他一些商业公司都开发有自己的j ...

  2. [extjs] ExtJs4.2 Form 表单提交

    基本代码: <script> Ext.onReady(function(){ Ext.create('Ext.form.Panel', { title: '登录', bodyPadding ...

  3. POJ 1384 Piggy-Bank(完全背包)

    Description Before ACM can do anything, a budget must be prepared and the necessary financial suppor ...

  4. Nginx 链接

    Nginx反向代理以及负载均衡配置:http://www.cnblogs.com/Miss-mickey/p/6734831.html

  5. 浅谈Socket长连+多线程

    缘由 不知各位同仁有没有发现,用简单,无外乎就是都是一个流程 1)监听链接 2)校验链接是否是正常链接 3)保存链接至全局静态字典 4)开启新线程监听监听到的线程报文 5)执行对应命令或者发送对应命令 ...

  6. 深刻理解 React (一) ——JSX和虚拟DOM

    版权声明:本文由左明原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/155 来源:腾云阁 https://www.qclou ...

  7. [SCOI2008] 着色方案[高维dp]

    321. [SCOI2008] 着色方案 ★★★   输入文件:color.in   输出文件:color.out   简单对比时间限制:1 s   内存限制:64 MB 题目背景: 有n个木块排成一 ...

  8. 重新学习python类

    之前觉得有java和c++的底子,对python的类没有认真研习,后面到了写项目的时候各种懵逼.终于决定彻底解决python的类问题. 一.python的类定义: class cls_name: de ...

  9. python 进行抓包嗅探

    一.绪论 最近一直想弄一个代理,并且对数据包进行解读,从而完成来往流量的嗅探.于是今天学习了一下如何使用Python抓包并进行解包. 首先要用到两个模块 dpkt(我这边ubuntu16.04 LTS ...

  10. 【BZOJ4149】[AMPPZ2014]Global Warming 单调栈+RMQ+二分

    [BZOJ4149][AMPPZ2014]Global Warming Description 给定一个序列a[1],a[2],...,a[n].请从中选出一段连续子序列,使得该区间最小值唯一.最大值 ...