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
104
题目解析:
前面已经写了一篇博客如何构造矩阵,这道题可以说就是上片博客的简单应用。
矩阵的乘法不满足交换律,但是却满足结合律,如: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[10][10];
} init,res;
int K;
int mod,b[10],f[10];
ma Mul(ma x,ma y)
{ma tmp;for(int i=0; i<10; i++)for(int j=0; j<10; j++){tmp.a[i][j]=0;for(int k=0; k<10; 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=0; i<10; i++){for(int j=0; j<10; j++)tmp.a[i][j]=(i==j);}while(K!=0){if(K&1)tmp=Mul(tmp,x);K>>=1;x=Mul(x,x);}return tmp;
}
int main()
{while(scanf("%d%d",&K,&mod)!=EOF){for(int i=0; i<=9; i++){scanf("%d",&init.a[9][9-i]);}if(K<=9){printf("%d\n",K);continue;}for(int i=0; i<10; i++)f[i]=i;for(int i=0; i<=8; i++){for(int j=0; j<=9; j++)init.a[i][j]=(i==j-1);}res=Pow(init,K);int ans=0;for(int j=0; j<10; j++){ans=(ans+res.a[0][j]*j)%mod;}printf("%d\n",ans);}return 0;
}

加深印象,写了两次。

#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct ma
{int a[10][10];
} init,res;
int K;
int mod,b[10],f[10];
ma Mul(ma x,ma y)
{ma tmp;for(int i=0; i<10; i++)for(int j=0; j<10; j++){tmp.a[i][j]=0;for(int k=0; k<10; 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=0; i<10; i++){for(int j=0; j<10; j++)tmp.a[i][j]=(i==j);}while(K!=0){if(K&1)tmp=Mul(tmp,x);K>>=1;x=Mul(x,x);}return tmp;
}
int main()
{while(scanf("%d%d",&K,&mod)!=EOF){for(int i=0; i<=9; i++){scanf("%d",&init.a[9][9-i]);}if(K<=9){printf("%d\n",K);continue;}for(int i=0; i<10; i++)f[i]=i;for(int i=0; i<=8; i++){for(int j=0; j<=9; j++)init.a[i][j]=(i==j-1);}res=Pow(init,K-9);int ans=0;for(int j=0; j<10; j++){ans=(ans+(res.a[9][j])*f[j])%mod;}printf("%d\n",ans);}return 0;
}

View Code

转载于:https://www.cnblogs.com/zhangmingcheng/p/4132899.html

HDU1757:A Simple Math Problem(矩阵快速幂)相关推荐

  1. HDU - 1757 A Simple Math Problem(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:实现公式: f(x)=x,x<10 f(x)=a0*f(x-1)+a1*f(x-2)+--+a9*f(x-10) 题目给出a0~a9,一个n和一个m,要求输出f(n ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 在吴神的帮助下才明白如何构造矩阵,还是好弱啊. 此处盗一张图 1 #include <io ...

  3. DUToj1085 Water Problem(矩阵快速幂)

    Problem I: Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java ...

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

  5. HDU 4291 A Short problem 矩阵快速幂 循环节

    题解思路: 构造矩阵,矩阵乘法计算还是t; 需要找循环节;   (注意因为是复合函数,不可以在里面取mod) 暴力跑只有可以找到g(222222224)%1e9==g(0)%1e9; 所以 g(g(n ...

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

  7. 【HDU - 1757】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) = ...

  8. 43行代码AC——HDU 1757 A Simple Math Problem(矩阵快速幂,附快速幂讲解)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 代码(去掉空行43行) #include<iostream> #inclu ...

  9. HDOJ 1757 A Simple Math Problem(矩阵快速幂)

    2018-5-24 简单的矩阵快速幂问题,重点是如何找到对应关系. f(10) a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 f(9) f(9) 1 0 0 0 0 0 0 0 0 0 ...

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

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + -- ...

最新文章

  1. Java XML解析器
  2. DL-4 深度学习中的batch_size、epoch、iteration的区别
  3. 喜大普奔!Github 移动端上架!
  4. 才26岁!94年小姐姐,已任985名校副教授!
  5. LAMP环境搭建过程中出现的问题——基于VM虚拟CentOS-5.6
  6. html3d变形,深入理解CSS变形transform(3d) - 小火柴的蓝色理想
  7. android 投屏 版本号,安卓设备投屏画质模糊及投屏延迟的调整方法
  8. 服务器打开xlsm文件,XLSM 文件扩展名: 它是什么以及如何打开它?
  9. 解决进不去BIOS或U盘启动,windows10如何关闭快速启动
  10. 准确的找到BAT实习机会~我入职了腾讯
  11. 长见识了!一看就会的浏览器帧原理
  12. IOS苹果ipa重签名工具(苹果签名工具,ios签名工具,支持Windows系统和Macos系统)
  13. jackson-databind反序列化漏洞
  14. [经典面试题]排列组合专题
  15. LeetCode刷题: 【914】卡牌分组(求N个数的最大公因数)
  16. SOPHON sail.Decoder无法正常解码rtsp流(使用ffmpeg和opencv可以正常解码)
  17. 恒温恒湿试验箱制冷系统及工作原理
  18. win7下配置FTP服务
  19. 腾讯云从业者资格认证考试模拟题1
  20. abb机器人示教器io信号关联_如何在示教器上配置ABB机器人的输入/输出信号?(多图)...

热门文章

  1. Linux 【系统知识】 - Cgroup 初步了解
  2. html点击按钮动态添加input文本框
  3. PsExec:一个非常实用的【远程运行】工具
  4. python 学习之路开始了
  5. 网站开发中很实用的 HTML5 jQuery 插件
  6. Android快速开发框架Android-query
  7. KCP - A Fast and Reliable ARQ Protocol
  8. 【Java编译】含package的类文件编译
  9. python 操作mysql
  10. Beta版本测试第二天