HDU1757:A Simple Math Problem(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=1757
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.
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.
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(矩阵快速幂)相关推荐
- 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 ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 在吴神的帮助下才明白如何构造矩阵,还是好弱啊. 此处盗一张图 1 #include <io ...
- DUToj1085 Water Problem(矩阵快速幂)
Problem I: Water Problem Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java ...
- 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 ...
- HDU 4291 A Short problem 矩阵快速幂 循环节
题解思路: 构造矩阵,矩阵乘法计算还是t; 需要找循环节; (注意因为是复合函数,不可以在里面取mod) 暴力跑只有可以找到g(222222224)%1e9==g(0)%1e9; 所以 g(g(n ...
- 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 * ...
- 【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) = ...
- 43行代码AC——HDU 1757 A Simple Math Problem(矩阵快速幂,附快速幂讲解)
一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 代码(去掉空行43行) #include<iostream> #inclu ...
- 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 ...
- 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) + -- ...
最新文章
- Java XML解析器
- DL-4 深度学习中的batch_size、epoch、iteration的区别
- 喜大普奔!Github 移动端上架!
- 才26岁!94年小姐姐,已任985名校副教授!
- LAMP环境搭建过程中出现的问题——基于VM虚拟CentOS-5.6
- html3d变形,深入理解CSS变形transform(3d) - 小火柴的蓝色理想
- android 投屏 版本号,安卓设备投屏画质模糊及投屏延迟的调整方法
- 服务器打开xlsm文件,XLSM 文件扩展名: 它是什么以及如何打开它?
- 解决进不去BIOS或U盘启动,windows10如何关闭快速启动
- 准确的找到BAT实习机会~我入职了腾讯
- 长见识了!一看就会的浏览器帧原理
- IOS苹果ipa重签名工具(苹果签名工具,ios签名工具,支持Windows系统和Macos系统)
- jackson-databind反序列化漏洞
- [经典面试题]排列组合专题
- LeetCode刷题: 【914】卡牌分组(求N个数的最大公因数)
- SOPHON sail.Decoder无法正常解码rtsp流(使用ffmpeg和opencv可以正常解码)
- 恒温恒湿试验箱制冷系统及工作原理
- win7下配置FTP服务
- 腾讯云从业者资格认证考试模拟题1
- abb机器人示教器io信号关联_如何在示教器上配置ABB机器人的输入/输出信号?(多图)...