hdu - 1757 - A Simple Math Problem
题意:当x < 10时, f(x) = x;
当x >= 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) + + a2 * f(x-3) + …… + a9 * f(x-10);
ai(0<=i<=9) 只能是0或者1 ,给出a0 ~ a9,k和m,计算f(k)%m(k<2*10^9 , m < 10^5)。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757
——>>构造矩阵,快速幂求解。
用excel弄了个~
~
#include <cstdio>
#include <cstring> using namespace std; const int maxn = 10 + 5;
int k, mod, n; struct Mar{ //矩阵
int m[maxn][maxn];
Mar(){
memset(m, 0, sizeof(m));
}
}; Mar operator + (Mar a, Mar b){ //矩阵+
Mar ret;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) ret.m[i][j] = (a.m[i][j] + b.m[i][j]) % mod;
return ret;
} Mar operator * (Mar a, Mar b){ //矩阵*
Mar ret;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int l = 0; l < n; l++) ret.m[i][j] = (ret.m[i][j] + a.m[i][l] * b.m[l][j]) % mod;
return ret;
} Mar pow_mod(Mar a, int n){ //矩阵快速幂
if(n == 1) return a;
Mar x = pow_mod(a, n/2);
x = x * x;
if(n&1) x = x * a;
return x;
} void solve(){
Mar A;
n = 10;
int i, j, ret = 0;
for(i = 0; i < n; i++) scanf("%d", &A.m[0][i]);
if(k < 10) ret = k % mod;
else{
for(i = 1, j = 0; i < n; i++, j++) A.m[i][j] = 1;
A = pow_mod(A, k-9);
for(i = 0, j = 9; i < n; i++, j--) ret = (ret + A.m[0][i] * j) % mod;
}
printf("%d\n", ret);
} int main()
{
while(scanf("%d%d", &k, &mod) == 2) solve();
return 0;
}
hdu - 1757 - A Simple Math Problem的更多相关文章
- HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU 1757 A Simple Math Problem (矩阵乘法)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1757 A Simple Math Problem(矩阵高速幂)
题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...
- 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) + …… ...
- hdu 1757 A Simple Math Problem(矩阵快速幂乘法)
Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...
- hdu 1757 A Simple Math Problem (矩阵快速幂)
Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 ...
随机推荐
- Cordova+Asp.net Mvc+GIS
Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码) 1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过 ...
- Android源码下载
Android源码下载 1.安装git 2.安装repo 从这里 https://dl-ssl.google.com/dl/googlesource/git-repo/repo 下载repo文件 3. ...
- 测试Web服务接口
1. http://www.iteye.com/topic/142034 2. http://www.iteye.com/topic/1123835 3.http://yongguang423.ite ...
- Delphi多线程数据库查询(ADO)
ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用(CoInitialize was not called):所以,在使用任何dbGo对象前,必须手 调用CoIniti ...
- fragment中listview触发事件setOnItemClickListener不好使
<listView/>中// listview点击 ,高度wrap_content改成fill_prarent
- poj1144Network(无向图割点数)
题目请戳这里 题目大意:给一张无向图,求割点数量. 题目分析:tarjan算法求割点.关于无向图割点,这里说的很清楚了.直接建图跑一遍tarjan算法即可. 详情请见代码: #include < ...
- paip.c++ qt 共享库dll的建立
paip.c++ qt 共享库dll的建立 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/at ...
- xshell使用命令总结
这个工具主要是链接linux 并且可以从linux上面下载文件到本地 还有上传本地文件到linux上面 下载首先需要压缩打包命令为: tar -cf am_mailer.tar * sz am_mai ...
- C#中IList<T>与List<T>的区别感想【转】
写代码时对: IList IList11 =new List (); List List11 =new List (); 有所疑惑,于是在网上搜索一下,很受启发,于是收藏下来,但对部分观点不敢苟同,用 ...
- 数据存储(三)--JSON数据处理
JSON是一种轻量级的数据交换格式,具有良好的可读和便于高速编写的特性,从而能够在不同平台间进行数据交换.JSON採用兼容性非常高的文本格式,同一时候也具备类似于C语言体系的行为.JSON能够将Jav ...