题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1757

题目大意:

求递推式第k项模m

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 .

解题思路:

构建矩阵

直接用矩阵快速幂模板求解

注意,小于10的时候不能直接输出k,要输出k%m

 #include<bits/stdc++.h>
using namespace std;
const int maxn = + ;
int MOD;
struct Mat
{
int a[maxn][maxn];
int n, m;//n为行数,m为列数
Mat(int n, int m):n(n), m(m)
{
memset(a, , sizeof(a));
}
void init()
{
for(int i = ; i < n; i++)a[i][i] = ;//初始化成单位矩阵
}
void output()
{
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
};
Mat mul(Mat a, Mat b)//矩阵乘法
{
Mat tmp(a.n, b.m);//矩阵乘法结果矩阵行数为a的行数,列数为b的列数
for(int i = ; i < a.n; i++)
{
for(int j = ; j < b.m; j++)
{
for(int k = ; k < a.m; k++)//a.m == b.n(乘法的前提条件)
{
tmp.a[i][j] += (a.a[i][k] * b.a[k][j] % MOD);
tmp.a[i][j] %= MOD;
}
}
}
return tmp;
}
Mat pow(Mat a, int n)
{
Mat tmp(a.n, a.m);
tmp.init();
while(n)
{
if(n & )tmp = mul(tmp, a);
n /= ;
a = mul(a, a);
}
return tmp;
}
int main()
{
int n, m;
while(cin >> n >> m)
{
Mat a(, );
Mat b(, );
//对a和b进行初始化
for(int i = ; i < ; i++)cin >> a.a[][i];
for(int i = ; i < ; i++)a.a[i][i - ] = ;
for(int i = ; i < ; i++)b.a[i][] = - i;
MOD = m;
//a.output();
//b.output();
if(n < )
{
cout<<(n % m)<<endl;
}
else
{
Mat ans = pow(a, n - );
//ans.output();
ans = mul(ans, b);
cout<<ans.a[][]<<endl;
}
}
}

hdu-1757 A Simple Math Problem---矩阵快速幂模板题的更多相关文章

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

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

  2. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

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

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

  4. HDU1757 A Simple Math Problem 矩阵快速幂

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

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

  6. luoguP3390(矩阵快速幂模板题)

    链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...

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

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

  9. 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. ERROR 1010 (HY000): Error dropping database (can't rmdir './nsd', errno: 39)

    在删除数据库的时候报标题所示错误 mysql> drop database test; ERROR 1010 (HY000): Error dropping database (can't rm ...

  2. Fiddler-抓Android和IOS包

    知识:Fiddler能捕获IOS设备发出的请求,比如IPhone, IPad, MacBook. 等等苹果的设备.  同理,也可以截获Andriod,Windows Phone的等设备发出的HTTP/ ...

  3. 虚拟机上安装Cell节点(12.1.2.3.3)

    安装介质下载 打开firefox,输入:https://edelivery.oracle.com 点击"Sign In",输入帐号.密码,登陆edelivery网站.       ...

  4. 什么是obj文件?

    百度百科: 程序编译时生成的中间代码文件.目标文件,一般是程序编译后的二进制文件,再通过链接器(LINK.EXE)和资源文件链接就成可执行文件了.OBJ只给出了程序的相对地址,而可执行文件是绝对地址. ...

  5. 【补档】Pycharm的一些配置

    新建和创建文件就不必说了吧~ 运行这里原本是灰色的,我们点旁边的三角形 点绿色+号,python Name:随便写 Script:选择一个py文件~,然后OK,就可以运行了

  6. Python时间与日期操作(datetime、time、calendar)

    相关模块 模块 说明 time time是一个仅包含与日期和时间相关的函数和常量的模块,在本模块中定义了C/C++编写的几个类.例如,struct_time类 datetime datetime是一个 ...

  7. java——helloword

    第一次接触Java,感觉乱乱的,需要捋清楚一些概念再安装java. 首先,什么是JDK,什么是JRE呢? JRE:JAVA运行环境(Java Runtime Envirnment) JDK:Java开 ...

  8. ACdream 1236 Burning Bridges 割边 + 去重边

    题目就是求一副图的割边,然后对于那些有重复的边的,不能算做割边. 思路就是每次加入一条边的时候,判断这条边是否存在过,存在过的话,就把那条边设为inf,表示不能作为割边.于是有了这样的代码 #incl ...

  9. Docker 镜像制作 CentOS+JDK+Tomcat

    [root@localhost createImages]# ls apache-tomcat-.tar.gz server-jre-8u121-linux-x64.tar.gz [root@loca ...

  10. eclipse加上电脑全黑主题的设置(win10)

    eclipse加上电脑全黑主题的设置(win10) 前几天在找设置win10的边框颜色时,发现的这个高对比的功能,现在已经用了好几天了,自己感觉是真的好用,所有才分享出来,相比所谓网上的豆沙绿,果然感 ...