HDU - 4990 Reading comprehension 【矩阵快速幂】
题目链接
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 【矩阵快速幂】的更多相关文章
- 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 ...
- hdu 4990 Reading comprehension 二分 + 快速幂
Description Read the program below carefully then answer the question. #pragma comment(linker, " ...
- hdu4990 Reading comprehension 矩阵快速幂
Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:10240 ...
- HDU.1575 Tr A ( 矩阵快速幂)
HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- HDU 4990 Reading comprehension 简单矩阵快速幂
Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- hdu 2604 Queuing(矩阵快速幂乘法)
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- 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 ...
随机推荐
- [Python] SQLBuilder 演示样例代码
用Python写一个SQLBuilder.Java版能够从 http://www.java2s.com/Code/Java/Database-SQL-JDBC/SQLBuilder.htm 看到. 附 ...
- 8种移动APP导航设计模式对照
当我们确定了移动APP的设计需求和APP产品设计流程之后,開始着手设计APP界面UI或是APP原型图啦.这个时候我们都要面临的第一个问题就是怎样将信息以最优的方式组合起来? 或许我们对照和了解了其它一 ...
- MFC的运行机理分析+ASC码问题
Win32程序是从WinMain开始执行的,但是MFC把WinMain给封装了,不会出现在你的代码里面,他已经编译到mfc80.dll了(VS2005)了. 在MFC中的执行顺序是这样的,首先Wind ...
- fastJson 转换日期格式
第一种方法: JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd"; String str = JSON.toJSONString(user,Seria ...
- Cloudera
官方文档: http://www.cloudera.com/content/cloudera/en/documentation/core/latest/ 博客教程 http://www.wangyon ...
- IOS中公布应用程序,进度条一直不走怎么处理
在IOS中公布应用程序非常是喜闻乐见. 近期1周.我更新了6次版本号.可是时不时的会卡住,进度条不走. 最后总结了几个原因. 1.在公布前你要确认自己的证书是否配置正确 2.DNS域名server有没 ...
- caffe编译的问题 找不到opencv的 tiff库文件
解决办法: sudo su cmake .. make -j8 make pycaffe make install 问题解决. 看起来是权限问题导致.
- HDFS源码分析之UnderReplicatedBlocks(二)
UnderReplicatedBlocks还提供了一个数据块迭代器BlockIterator,用于遍历其中的数据块.它是UnderReplicatedBlocks的内部类,有三个成员变量,如下: // ...
- iOS打包(ipa包)
1.打开XCode打开project文件.选择Product,再点击Archive. 2.鼠标右键点击Shoe In Finder 3.鼠标右键选择"显示包内容" 4.鼠标左键双击 ...
- vagrant 介绍,安装与使用
可以帮你统一团队成员的开发环境.如果你或者你的伙伴创建了一个Vagrantfile,那么你只需要执行vagrant up就行了,所有的软件都会安装并且配置好.团队成员可以通过相同的Vagrantfil ...