题意:当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的更多相关文章

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

  2. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

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

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  4. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  5. HDU 1757 A Simple Math Problem (矩阵乘法)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  7. 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) + …… ...

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

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

随机推荐

  1. Linux--本地yum库

    Linux配置本地yum云: 第一步:把cd 挂载到目录树 mount /dev/cdrom /mnt/media 第二步:增加/etc/yum.repo.d/local.repo touch /et ...

  2. 关于“创业者与VC见面的10个不成文细节点”

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Will Wang链接:http://www.zhihu.com/question/19641135/answer/50974 ...

  3. iOS的category和protocol

    很多时候我们需要扩展一下现有的类,增加一点功能.如果有源码,修改一下即可,如果是第三方的库,就要麻烦一些.在C++中我们使用类继承的方法来实现,在ObjectiveC中当然也可以这么做,不过Objec ...

  4. Udacity-Artificial Intelligence for Robotics 课程笔记

    Lesson 1 Localization 蒙特卡洛机器人定位模型 sense 贝叶斯模型 move 全概率公式 localization练习 # The function localize take ...

  5. hdu 5115 Dire Wolf(区间dp)

    Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful w ...

  6. Android Handler Leak

    转自:Android中使用Handler引发的内存泄露 在Activity中,经常会用到自定义的Handler来处理主线程收到的Message,但是ADT20以后,直接定义的如下定义的内部会有提示说这 ...

  7. Java 基本日期类使用——格式化(二)

    Java日期格式化主要有以下几种方式:java.text.DateFormat以及其子类java.text.SimpleDateFormat; DateFormat 是日期/时间格式化子类的抽象类,它 ...

  8. Devpexpress 打印预览问题

    devexpress 12 之前报表打印: XtraReports rp1 = new XtraReports(); rp1.ShowPreview(): 即可预览报表: devexpress 13 ...

  9. Linux学习之查看是否安装软件

    1.rpm包安装的,可以用 rpm -qa 看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字" 2.以deb包安装的,可以用 dpkg -l ...

  10. windows迁移linux问题集锦[ZZ]

    http://blog.csdn.net/m_star_jy_sy/article/details/8482202 1)‘_wcsicmp’在此作用域中尚未声明 #ifdef WIN32#define ...