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. 总结一下前端面试题之Html和CSS

    总结一下关于前端的面试题,今天我们分享关于Html和CSS部分的 面试 (1) 1. 常用那几种浏览器测试?有哪些内核(Layout Engine)? 2. 说下行内元素和块级元素的区别?行内块元素的 ...

  2. POJ 1018 Communication System(树形DP)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  3. swift学习笔记之控制流

    控制流: 1.if语句 let count = { print("yes") }else{ print("no") } 2.switch语句 (1)Swift中 ...

  4. mac 环境配置

    安装homebrew 用于安装各种软件 eg:brew search qq 查看qq安装目录 brew install 复制刚刚查看到的目录安装qq 安装 oh my zsh 自动补全目录跳转 1.安 ...

  5. 百度地图地址查询API使用

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgsAAALxCAIAAABdNHLmAAAgAElEQVR4nOy9/VMbZ5rvnf/hbM7zy3 ...

  6. java高级---->Thread之Exchanger的使用

    Exchanger可以在两个线程之间交换数据,只能是2个线程,他不支持更多的线程之间互换数据.今天我们就通过实例来学习一下Exchanger的用法. Exchanger的简单实例 Exchanger是 ...

  7. Android Activity与Fragment生命周期 对应关系

  8. c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)

    Have you been in need of converting mp3 audios to wav audios?  If so, the skill in this article prov ...

  9. c# 数据库数据与DataGridView表控件的绑定

    public Form1() { InitializeComponent(); //连接数据库 string str = "Data Source=IP;Initial Catalog=数据 ...

  10. shell脚本备份日志

    #!/bin/sh # back tomcat catalina.out cd /home/log_bak #the file DATE=`date '+%Y%m%d-%H%M'` ARCHIVE=$ ...