HDU4990 Reading comprehension —— 递推、矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4990
Reading comprehension
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2329 Accepted Submission(s): 954
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans*2+1)%m;
else ans=ans*2%m;
}
printf("%d\n",ans);
}
return 0;
}
[Technical Specification]
1<=n, m <= 1000000000
3 100
5
题解:
当n为奇数时,f[n] = 2*f[n-1]+1,f[n-1] = 2*f[n-2],所以:f[n] = f[n-1] + 2*f[n-2] + 1;
当n为偶数时,f[n] = 2*f[n-1],f[n-1] = 2*f[n-2] + 1,所以:f[n] = f[n-1] + 2*f[n-2] + 1;
综上:f[n] = f[n-1] + 2*f[n-2] + 1,构造矩阵:

代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
//const int MOD = 10000007;
const int MAXN = 1e6+; LL MOD;
const int Size = ;
struct MA
{
LL mat[Size][Size];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA mul(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%MOD, ret.mat[i][j] %= MOD;
return ret;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
} MA tmp = {
, , ,
, , ,
, ,
}; int main()
{
LL n, m;
while(scanf("%lld%lld",&n,&m)!=EOF)
{
MOD = m;
if(n<=)
{
printf("%lld\n", n%MOD);
continue;
} MA s = tmp;
s = qpow(s, n-); LL ans = ((2LL*s.mat[][]%MOD + s.mat[][])%MOD+s.mat[][])%MOD;
printf("%lld\n", ans);
}
}
HDU4990 Reading comprehension —— 递推、矩阵快速幂的更多相关文章
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...
- HDU6030 Happy Necklace(递推+矩阵快速幂)
传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...
- 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)
题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...
- LightOJ 1244 - Tiles 猜递推+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...
- [递推+矩阵快速幂]Codeforces 1117D - Magic Gems
传送门:Educational Codeforces Round 60 – D 题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...
- 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)
Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
随机推荐
- windows XP 下的DTRACE 跟踪 学习
https://github.com/prash-wghats/DTrace-win32 1. dtrace_loader.exe -l //to load dtrace drivers 2. C:\ ...
- DozerBeanMapper + 对象转Map方法
1.简介 dozer是一种JavaBean的映射工具,类似于apache的BeanUtils.但是dozer更强大,它可以灵活的处理复杂类型之间的映射.不但可以进行简单的属性映射.复杂的类型映 ...
- Android gradle 相关配置
有时候我们需要重命名输出apk文件名,在Android studio 3.0以前我们是这样写的: applicationVariants.all { variant -> variant.out ...
- xshell登陆腾讯云服务器
2016-12-11 00:17:36 前段时间在同学的介绍下关注了一下腾讯云:然后里面有学生优惠可以拿到免费的域名和云服务器.所以感兴趣就实验了一下,今天中午抢到了“1元特惠的学生包”,里面有免 ...
- Git 的使用Git Bash和Git GUI
使用Github也有一年的时间了,之前一直都是使用的Github客户端,对提交,更新,克隆,合并,分支有一定的了解和实践.一直都想试试命令行的形式,但是感觉可能桌面版的方便就没有做. 可是Github ...
- python 列表结构更新的奇妙问题
使用python + plt 画图遇到了一个奇怪的问题 应该出来的是这样: 结果做出来以后是这样: 为什么画到一起了...... 这个锅python列表背 a=[1,2]b=a 这样 改变b ,a ...
- [转] RabbitMQ介绍
转自: http://lynnkong.iteye.com/blog/1699684 1 什么是RabbitMQ? RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初 ...
- jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径
遇到的问题是这种,在jsp网页中嵌入了本地的图片,由于会用到上传到服务器的图片,所以没有放到项目里面,而是把全部图片单独放到一个文件夹里,然后打算使用绝对路径把要显示的图片显示出来.比方是放在了E盘的 ...
- STP 根桥、根port、指定port是怎样选举的
学习CCNA过程中,对交换机的根桥.跟port以及指定port选举有些迷糊.也度娘了一番,总认为一部分人解释的不够全面精细.通过细致研究终于有了自己的理解,分享给大家,假设纰漏,欢迎指正. STP收敛 ...
- makefile 与android.mk中加信息打印
makefile里面加打印: [table]@echo ' zImage - Compressed kernel image' android.mk里面加信息打印: $(warning TEXT... ...