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; 
}

InputMulti test cases,each line will contain two integers n and m. Process to end of file. 
[Technical Specification] 
1<=n, m <= 1000000000OutputFor each case,output an integer,represents the output of above program.Sample Input

1 10
3 100

Sample Output

1
5

直接利用源程序暴力打出 1,2,5,10,21,42 找出规律 fn = fn-1 + 2*fn-2+1

数据比较大,直接求矩阵快速幂。

推出 转化矩阵为:

1 2 1
1 0 0
0 0 1

 初始矩阵为

1 2 1

 直接上代码:

//Asimple
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define test() cout<<"============"<<endl
#define debug(a) cout << #a << " = " << a <<endl
#define dobug(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl
using namespace std;
typedef long long ll;
const int N=;
//const ll MOD=10000007;
const int INF = ( << );
const double PI=atan(1.0)*;
const int maxn = +;
const ll mod = ;
int n, m, len, ans, sum, v, w, T, num;
int MOD; struct Matrix {
long long grid[N][N];
int row,col;
Matrix():row(N),col(N) {
memset(grid, , sizeof grid);
}
Matrix(int row, int col):row(row),col(col) {
memset(grid, , sizeof grid);
} //矩阵乘法
Matrix operator *(const Matrix &b) {
Matrix res(row, b.col);
for(int i = ; i<res.row; i++)
for(int j = ; j<res.col; j++)
for(int k = ;k<col; k++)
res[i][j] = (res[i][j] + grid[i][k] * b.grid[k][j] + MOD) % MOD;
return res;
} //矩阵快速幂
Matrix operator ^(long long exp) {
Matrix res(row, col);
for(int i = ; i < row; i++)
res[i][i] = ;
Matrix temp = *this;
for(; exp > ; exp >>= , temp = temp * temp)
if(exp & ) res = temp * res;
return res;
} long long* operator[](int index) {
return grid[index];
} void print() { for(int i = ; i <row; i++) {
for(int j = ; j < col-; j++)
printf("%d ",grid[i][j]);
printf("%d\n",grid[i][col-]);
}
}
}; void input(){
ios_base::sync_with_stdio(false);
while( cin >> n >> MOD ) {
Matrix A;
A[][] = A[][] = ;
A[][] = ;
A[][] = A[][] = ;
A = A^n;
Matrix B;
B[][] = B[][] = ;
B[][] = ;
A = A*B;
if( n% ) cout << A[][] << endl;
else cout << A[][] << endl;
}
} int main(){
input();
return ;
}

Reading comprehension HDU - 4990的更多相关文章

  1. 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] 故 ...

  2. hdu-4990 Reading comprehension(快速幂+乘法逆元)

    题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  3. 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification

    论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification 目前,阅读理解通常会给出 ...

  4. Attention-over-Attention Neural Networks for Reading Comprehension论文总结

    Attention-over-Attention Neural Networks for Reading Comprehension 论文地址:https://arxiv.org/pdf/1607.0 ...

  5. hdu 4990(数学,等比数列求和)

    Reading comprehension Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HDU4990 Reading comprehension —— 递推、矩阵快速幂

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

  7. Cognitive Graph for Multi-Hop Reading Comprehension at Scale(ACL2019) 阅读笔记与源码解析

    论文地址为:Cognitive Graph for Multi-Hop Reading Comprehension at Scale github地址:CogQA 背景 假设你手边有一个维基百科的搜索 ...

  8. 机器阅读理解综述Neural Machine Reading Comprehension Methods and Trends(略读笔记)

    标题:Neural Machine Reading Comprehension: Methods and Trends 作者:Shanshan Liu, Xin Zhang, Sheng Zhang, ...

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

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

随机推荐

  1. mysql使用druid监控配置

    近一年公司对druid使用频率越来越高了,感觉有必要了解下druid的监控配置,参考文档:https://blog.csdn.net/netdevgirl/article/details/526098 ...

  2. 接口自动化测试 (三)request.post

    上一节介绍了  requests.get()  方法的基本使用,本节介绍  requests.post()  方法的使用: 本文目录: 一.方法定义 二.post方法简单使用 1.带数据的post 2 ...

  3. java导入、导出Excel文件

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  4. 27.用webpack自搭react和vue框架

    自己搭建react-app vue-cli 前置条件 cnpm i -D webpack webpack-cli webpack-dev-server cnpm i -D css-loader sty ...

  5. Oracle考试题作业

    新建一张学员信息表(student),要求:1. 字段如下:学号(sid),姓名(name),性别(sex),年龄(age),地址(address).2. 分别为字段添加约束:学号为主键,姓名为非空, ...

  6. ArrayList 的代码

    public class user { private String userName; //类的构造方法 public user (String userName ){ this.userName= ...

  7. 在CentOS 7.6上安装VNC Server

    停止并禁用防火墙 systemctl stop firewalld.service systemctl disable firewalld.service 安装vnxserver yum instal ...

  8. GIS优秀博客以及网址收藏,持续更新

    1.gislaozhang arcpy,模型构建器,arcpro 地址:https://blog.csdn.net/gislaozhang?t=1      2.雾语 arcpy,模型构建器,arcg ...

  9. AE实现拖拽

    http://edndoc.esri.com/arcobjects/9.2/net/63391c82-c2e6-4797-b2e6-2c1d92f56f44.htm http://help.arcgi ...

  10. java之threadlocal的使用

    基本介绍 ThreadLocal很多地方叫线程本地变量,或者叫线程本地存储.ThreadLocal为每一个使用该变量的线程都提供一个变量值的副本,是每一个线程都可以独立地改变自己的副本,而不会和其它线 ...