Read the program below carefully then answer the question.
#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;
}

矩阵快速幂

 #include<stdio.h>
#include<string.h>
typedef long long ll;
int mod;
struct mat{
int r,c;
ll m[][];
void clear(){
for(int i=;i<=r;i++)memset(m[i],,sizeof(m[i]));
}
}; mat MatMul(mat m1,mat m2){
mat tmp;
tmp.r=m1.r;
tmp.c=m2.c;
int i,j,k;
for(i=;i<=tmp.r;i++){
for(j=;j<=tmp.c;j++){
ll t=;
for(k=;k<=m1.c;k++){
t=(t+(m1.m[i][k]*m2.m[k][j])%mod)%mod;
}
tmp.m[i][j]=t;
}
}
return tmp;
} mat MatQP(mat a,int n){
mat ans,tmp=a;
ans.r=ans.c=a.r;
memset(ans.m,,sizeof(ans.m));
for(int i=;i<=ans.r;i++){
ans.m[i][i]=;
}
while(n){
if(n&)ans=MatMul(ans,tmp);
n>>=;
tmp=MatMul(tmp,tmp);
}
return ans;
} int main(){
mat a;
a.r=;a.c=;
a.m[][]=;
a.m[][]=;
a.m[][]=;
mat t;
t.r=;
t.c=;
t.clear();
t.m[][]=;
t.m[][]=;
t.m[][]=;
t.m[][]=;
t.m[][]=;
int n;
while(scanf("%d%d",&n,&mod)!=EOF){
mat tmp=MatQP(t,n/);
tmp=MatMul(tmp,a);
printf("%lld\n",tmp.m[n%+][]);
}
return ;
}

hdu4990 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 —— 递推、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4990 Reading comprehension Time Limit: 2000/1000 MS (Java/Others ...

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

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

  5. Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)

    ;i<=n;i++) { )ans=(ans*+)%m; %m; } 给定n,m.让你用O(log(n))以下时间算出ans. 打表,推出 ans[i] = 2^(i-1) + f[i-2] 故 ...

  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. hdu4990矩阵快速幂

    就是优化一段代码,用矩阵快速幂(刚开始想到了转移矩阵以为是错的) 在搜题解时发现了一个神奇的网站:http://oeis.org/ 用来找数列规律 的神器.... 规律就是an=an-1+2*an-2 ...

  8. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  9. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

随机推荐

  1. C++解析二

    C++ 类访问修饰符 数据封装是面向对象编程的一个重要特点,它防止函数直接访问类类型的内部成员.类成员的访问限制是通过在类主体内部对各个区域标记 public.private.protected 来指 ...

  2. 【原创】QString 函数 replace()indexOf()、 lastindexOf()

    1.替换函数 示例: QString x = "Say yes!"; QString y = "no"; x.replace(, , y); // x == & ...

  3. 【资料收集】OpenCV入门指南 系列文章

    <OpenCV入门指南>系列文章地址:http://blog.csdn.net/morewindows/article/category/1291764 目录: 第一篇  安装OpenCV ...

  4. C++模板类之pair

    Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型,第一个元素是int型的 ...

  5. Centos7单主机部署 LAMP + phpmyadmin 服务

    LAMP -> centos + apache + mysql + php + phpmyadmin 一:搭建yum仓库: 安装utils: yum -y install yum-utils c ...

  6. 一次Web请求返回406原因与解决方案

    ajax请求,响应信息返回的却是报错406,. 1.断点调试,进入对应处理方法,且得到正确信息返回到解析器.使用的是ssm,前端ftl 2.js将返回错误信息打出来,类似为: 百度406错误出现的原因 ...

  7. ubuntu 搭建ss和使用方法

    一 ubuntu 搭建ssa.安装    sudo apt-get install python-gevent python-pip python-m2crypto    sudo pip insta ...

  8. Java基础复习

    java语言的一个核心:jdk, java development kits---面向开发人员jre, java Runtime Environment---服务器上 java虚拟机---(以字节码为 ...

  9. linux自动更新代码,自动备份数据库,打包应用发布

    切换root用户 sudo su - 1.安装svn,mysql yum install subversion yum install mysql 2.安装 maven 下载:百度云盘地址为 http ...

  10. vue-cli快速构建vue项目模板

    vue-cli 是vue.js的脚手架,用于自动生成vue.js模板工程的. 1.使用npm安装vue-cli 需要先装好vue 和 webpack(前提是已经安装了nodejs,否则连npm都用不了 ...