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 ...
随机推荐
- co & thunkify
co 之前在generator中已经介绍过Co了 戳这里 http://www.cnblogs.com/cart55free99/p/4893498.html co一般和thunkify一起使用 能够 ...
- Mining 任务分类
1.预测任务: 根据其它属性的值预测特定属性的值. 2.描述性任务: 发现数据之间潜在的关联关系.
- mongodb use db show dbs
mongodb 常用命令: 在dbs间切换用 use xxxdb 之后再操作就是只针对 xxxdb了: show dbs显示全部数据库 show collections 显示全部集合 mongodb数 ...
- Little Zu Chongzhi's Triangles
Little Zu Chongzhi's Triangles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 ...
- UBUNTU 下如何升级 gcc, g++
正如大家所知道的GCC并不支持"make uninstall". 一种推荐安装方式就是把GCC 安装在你自己指定的一个路径,当你不须要某个GCC版本号的时候你仅仅须要移除相应版本号 ...
- 【jQueryMobile】Helloworld而页面切换
jQuery Mobile它是jQuery 在手机和平板设备的版本号. jQuery Mobile 它不仅会带来重大的移动平台jQuery核心库,而且会发布一个完整统一jQuery搬家UI相框.全球主 ...
- html系列教程--span style 及表格标签 title video
<span> 标签:非块状元素,用于文本描述 <style> 标签:内联样式表标签定义样式信息,必须写明type类型为text/css,建议写在head中,不是必须 demo: ...
- java.util.vector中的vector的详细用法
ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.util.*; /** * 演示Vector的使用.包括Vector的创 ...
- Linux软连接与硬连接 .
http://blog.csdn.net/ningxinghai/article/details/7342338 Linux的软连接相当于window系统的快捷方式,如我们桌面的QQ等. 硬连接相当于 ...
- window.showModalDialog的基本用法
window.showModalDialog的基本用法 showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.show ...