构造矩阵。

1,当k<=9时,直接输出;

2,当k >9时,先求原矩阵的(k-9)次幂res矩阵,在求幂的过程中对m取余。最后res矩阵再与矩阵F相乘得到矩阵ans,相乘的过程中对m取余。ans矩阵的第一个元素就是答案。

PS.orz,这道题一气呵成。只不过我好像和大家构造矩阵的方向有点点差别。这不重要!能做出题就妥!

 #include<iostream>
#include<cstring>
#define maxn 12
using namespace std;
int f[]={,,,,,,,,,};
struct mat{
int a[maxn][maxn];
};
mat mat_mul(mat x,mat y,int Mod){
mat ans;
memset(ans.a,,sizeof(ans.a));
for (int i=;i<;i++)
for (int j=;j<;j++)
for (int k=;k<;k++){
ans.a[i][j]+=x.a[i][k]*y.a[k][j];
ans.a[i][j]%=Mod;
}
return ans;
}
void mat_pow(mat &res,int k,int Mod){
mat c=res;
k--;
while (k){
if (k&) res=mat_mul(res,c,Mod);
k>>=;
c=mat_mul(c,c,Mod);
}
}
int main(){
mat res;
int k,m;
while (cin >> k >> m){
memset(res.a,,sizeof(res.a));
for (int i=;i<;i++){
cin >> res.a[i][];
res.a[i][i+]=;
}
if (k<=){
cout << f[k]%m << endl;
continue;
}
else mat_pow(res,k-,m);
int ans=;
for (int i=;i<;i++){
ans+=res.a[i][]*f[-i]%m;
}
cout << ans%m << endl;
}
return ;
}

hdoj1757 A Simple Math Problem(矩阵快速幂)的更多相关文章

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

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

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

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

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

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

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

  5. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

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

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

  7. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

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

  9. HDU - 3521 An easy Problem(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...

随机推荐

  1. 680C. Bear and Prime 100 数学

    C. Bear and Prime 100 time limit per test:1 second memory limit per test:256 megabytes input:standar ...

  2. FEATURE_MCT_READERDIRECT问题

    刚才查了一下,好像与一个叫CCID的驱动有关.你把FEATURE_MCT_READEDIRECT定义成0x08,再make一下试试.

  3. [Groovy] 学习Groovy的好网站(内容全面)

    https://www.tutorialspoint.com/groovy/index.htm

  4. 设置powershell ExecutionPolicy

    Get-ExecutionPolicy -List Set-ExecutionPolicy -Scope CurrentUser AllSigned Policies: Restricted/AllS ...

  5. php连接DB2

    在php.ini中添加对DB2的支持 //////////////////////////////////////////////////// ;;;;;;;;;;;;;;;;;;;;;; ; Dyn ...

  6. 命名空间namespace ,以及重复定义的问题解析

    名字空间是用来划分冲突域的,把全局名字空间划分成几个小的名字空间.全局函数,全局变量,以及类的名字是在同一个全局名字空间中,有时为了防止命名冲突,会把这些名字放到不同的名字空间中去. 首先我们看一下名 ...

  7. gj6 深入python的set和dict

    6.1 collections中的abc from collections.abc import Mapping, MutableMapping #dict属于mapping类型 a = {} pri ...

  8. java-Runtime 调用命令

    java是一个跨平台的语言,可以在多种平台上运行相应的程序,但是有些时候进行数据的提取时,可能就需要系统的相关命令,尤其是调用linux命令 这时候就需要使用java的Runtime命令,来执行lin ...

  9. python读取文件另存为

    fr = open(filename_r,encoding='cp852') w2 = open(filename_w,'a')#a代表追加 w代表重写 for line in fr: w2.writ ...

  10. php读取用友u8采购入库单列表及详细

    <?php class erpData { protected static $erp; public function __construct() { $dbhost ="192.1 ...