思路:

如图找到推导公式,然后一通乱搞就好了

要开long long,否则红橙作伴

代码:

#include<set>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll long long
const int maxn = 3;
const int MOD = 1000000000+7;
const int INF = 0x3f3f3f3f;
using namespace std;
ll m;
struct Mat{
ll s[maxn][maxn];
void init(){
for(int i = 0;i < maxn;i++)
for(int j = 0;j < maxn;j++)
s[i][j] = 0;
}
}; Mat mul(Mat a,Mat b){
Mat t;
t.init();
for(int i = 0;i < maxn;i++){
for(int j = 0;j < maxn;j++){
for(int k = 0;k < maxn;k++){
t.s[i][j] = (t.s[i][j] + a.s[i][k]*b.s[k][j])%m;
}
}
}
return t;
}
Mat pow_mat(Mat p,int n){
Mat ret;
ret.init();
for(int i = 0;i < maxn;i++)
ret.s[i][i] = 1;
while(n){
if(n & 1) ret = mul(ret,p);
p = mul(p,p);
n >>= 1;
}
return ret;
}
int main(){
ll n;
while(scanf("%lld%lld",&n,&m) != EOF){
Mat A,B,T;
memset(T.s,0,sizeof(T.s));
memset(B.s,0,sizeof(B.s));
T.s[0][0] = T.s[1][0] = T.s[0][2] = T.s[2][2] = 1;
T.s[0][1] = 2;
if(n == 1) printf("%lld\n",1%m);
else if(n == 2) printf("%lld\n",2%m);
else{
B.s[0][0] = 2,B.s[1][0] = 1,B.s[2][0] = 1;
A = pow_mat(T,n - 2);
A = mul(A,B);
printf("%lld\n",A.s[0][0]);
}
}
return 0;
}

HDU 4990 Reading comprehension(矩阵快速幂)题解的更多相关文章

  1. HDU 4990 Reading comprehension 矩阵快速幂

    题意: 给出一个序列, \(f_n=\left\{\begin{matrix} 2f_{n-1}+1, n \, mod \, 2=1\\ 2f_{n-1}, n \, mod \, 2=0 \end ...

  2. hdu 4990 Reading comprehension 二分 + 快速幂

    Description Read the program below carefully then answer the question. #pragma comment(linker, " ...

  3. hdu4990 Reading comprehension 矩阵快速幂

    Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:10240 ...

  4. HDU.1575 Tr A ( 矩阵快速幂)

    HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...

  5. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  6. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  7. HDU 4990 Reading comprehension 简单矩阵快速幂

    Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...

  8. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  9. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  10. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

随机推荐

  1. CentOS oracle Client客户端安装

    CentOS客户端安装方法如下: 1.安装客户端 rpm -ivh /当前目录/oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm rpm - ...

  2. jQuery --- 利用a标签的download属性下载文件!

    最近遇到一个项目,需要有点击下载文件的功能. 由于文件格式是多种的,对于 rar / zip / rtf / doc / xlsx / jpg等. 点击下载有的是直接跳转到后进行下载,但有的是打开进行 ...

  3. 用 Fiddler查看 Android 网络请求

    1. 下载安装 fiddler,尽量到官方网站找最新的版本 备注:安装后,打开的时候如果报错: Machine-wide Progress Telerik Fiddler installation h ...

  4. LayoutSimple简易响应式CSS布局框架

    开发这个css布局的目的是为了少做一些重复的工作,一是前端或多或少会开发一些很小的响应式项目, 二是UI设计的出来的界面总是各种布局各种样式,这个时候如果前端去使用Bootstrap或者Foundat ...

  5. php最全基础,数组,函数,超全局变量,时间,回话,文件,php操作mysql

    共享一份学习php最全基础语法知识的笔记 原文链接:http://www.cnblogs.com/oscn/p/3607757.html:略有修改   http://www.cnblogs.com/l ...

  6. Java类集框架——List接口

    学习目标 掌握List接口与Collection接口的关系. 掌握List接口的常用子类:ArrayList.Vector. 掌握ArrayList与Vector类的区别.    Collection ...

  7. 在唯一密钥属性“fileExtension”设置为“.log”时,无法添加类型为“mimeMap”的重复集合项

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkoAAAFfCAIAAAA+snR7AAAgAElEQVR4nOzdZ1xT18PAcf+1VpZaW6

  8. HOJ Recoup Traveling Expenses(最长递减子序列变形)

    A person wants to travel around some places. The welfare in his company can cover some of the airfar ...

  9. 高频访问IP弹验证码架构图 让被误伤的用户能及时自行解封的策略

    高频访问IP限制 --Openresty(nginx + lua) [反爬虫之旅] - Silbert Monaphia - CSDN博客 https://blog.csdn.net/qq_29245 ...

  10. 如何在window的location使用target

    在页面中window的location跳转时,指定页面在框架中跳转 1. 如果你要让最顶层的框架跳转,就是整个页面,相当于用traget指向顶层 window.top.location   =   & ...