题目链接

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

题意

初始的ans = 0

给出 n, m

for i in 1 -> n

如果 i 为奇数

ans = (ans * 2 + 1) % m

反之

ans = ans * 2 % m

思路

如果我们只计算 偶数项 那么递推公式就是

ans[n] = 4 * ans[n - 2] + 2

如果 n 是偶数 那么刚好 就按这个公式推 第 n / 2 项

如果 n 是奇数 那么就是 第 【 n / 2 项 】 * 2 + 1

可以推知的矩阵为

然后矩阵快速幂

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8; const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
//const int MOD = 1e4; int MOD; struct Matrix
{
ll a[2][2];
Matrix () {}
Matrix operator * (Matrix const &b)const
{
Matrix res;
CLR(res.a, 0);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
res.a[i][j] = (res.a[i][j] + this->a[i][k] * b.a[k][j]) % MOD;
return res;
}
}; Matrix pow_mod(Matrix base, int n)
{
Matrix ans;
CLR(ans.a, 0);
ans.a[0][1] = 1;
while (n > 0)
{
if (n & 1)
ans = ans * base;
base = base * base;
n >>= 1;
}
return ans;
} int main()
{
Matrix base;
base.a[0][0] = 4;
base.a[0][1] = 0;
base.a[1][0] = 2;
base.a[1][1] = 1;
int n;
while (~scanf("%d%d", &n, &MOD))
{
Matrix ans = pow_mod(base, n / 2);
if (n & 1)
printf("%lld\n", (ans.a[0][0] * 2 + 1) % MOD);
else
printf("%lld\n", ans.a[0][0] % MOD);
}
}

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 简单矩阵快速幂

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

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

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

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

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

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

随机推荐

  1. 数据库访问的弹性化---WebLogic和Oracle RAC的整合:Active GridLink

        1.  什么是Active GridLink Data Source 从Oracle WebLogic Server 10.3.4版本开始引进了一种单数据源实现来支持Oracle RAC集群. ...

  2. oracle手动修改listener.ora和tnsnames.ora之后竟然无效

    oracle手动修改listener.ora和tnsnames.ora之后竟然无效 花式重启都没有生效,需要使用Net Configuration Assistant来进行刷一下,重新生成的监听还是一 ...

  3. GroupBox与Panel控件

    1.GroupBox控件常常用于逻辑地组合一组控件,如RadioButton 及 CheckBox控件,显示一个框架,其上有一个标题. 2.Panel 可以包含多个控件,以便将这些控件编为一组,以便方 ...

  4. nginx源代码分析--监听套接字的创建 套接字的监听 HTTP请求创建连接

    作为一个webserver,那么肯定是有监听套接字的,这个监听套接字是用于接收HTTP请求的,这个监听套接字的创建是依据配置文件的内容来创建的,在nginx.conf文件里有多少个地址就须要创建多少个 ...

  5. 怎样在编译的时候,控制删除apk不用的资源?

    1.改动alps/meidatek/config/xxx/ProjectConfig.mk下的MTK_PRODUCT_LOCALS,去掉不用的资源,比方屏幕密度或语言等.  2. nodpi和mipm ...

  6. iOS - 贝塞尔曲线,折线,曲线,波浪线

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZHlsYW5fbHdiXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  7. (一)关于jQuery的网上资源

    jQuery官网: http://jquery.com/ jQuery API: http://jquery.cuishifeng.cn/ w3school学习网站:http://www.w3scho ...

  8. 37:密码截取(回文串manacher算法)

    题目描述:Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解.比如 ...

  9. [Java] 实验4參考代码

    题目.提示.代码.解释都已公布. 提供这些的目的不是要求大家要写得像我写得这样,而是希望大家在实验后看看别人写的代码:     1. 提升理解代码的能力.     2. 不要自满于完毕题目.要明确你的 ...

  10. URL Handle in Swift (一) -- URL 分解

    更新时间: 2018-6-6 在程序开发过程之中, 我们总是希望模块化处理某一类相似的事情. 在 ezbuy 开发中, 我接触到了对于 URL 处理的优秀的代码, 学习.改进.记录下来.希望对你有所帮 ...