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. Hello 博客!

    2018-4-9 18:11:05第一次听这个python视频教程 然后让做的博客!放张图!

  2. cf 1132 F

    区间dp.. 每次删一串相邻的一样的,问多少次删光. 感觉想的几乎是一样的怎么比赛时就过不了呢...一定是因为我挂机睡觉了 考虑l和r相等,l和l+1相等,r和r-1相等这三种情况就行了..然后就是裸 ...

  3. 客户端如何访问访问oracle 12c 64位的数据库

    服务器A安装的oracle 12c 64位的数据库,机器B如何访问oracle数据库. oracle客户端必须是用32位的客户端,plsql才能访问 准备: 1.下载instantclient-bas ...

  4. 16-1 ECMA5与ECMA6的函数定义

    ECMA5: function Drag(id){ this.ele = document.getElementById(id); var that = this; this.ele.onmoused ...

  5. java爬取网站信息和url实例

    https://blog.csdn.net/weixin_38409425/article/details/78616688(出自此為博主) 具體代碼如下: import java.io.Buffer ...

  6. 【微信小程序——开发步骤1】

    知识点:   view,image,text编写文本框架 使用弹性盒子动态布局 使用rpx调试分辨率 在wxml中查看默认样式属性 步骤: 1.以如图页面实例说明如何写出微信文本内容 先对页面写出整体 ...

  7. java学习之路--String类方法的应用

    消除字符串两端的空格 1.判断字符串第一个位置是否为空格,如果是继续向下判断,直到不是空格位置,末尾也是这样,往前判断,直到不是空格为止. 2.当开始和末尾都不是空格时,获取字符串. public s ...

  8. 微信小程序之点赞和取消点赞

    wxml代码 <image wx:if="{{collection}}" src="/images/boy.png" bindtap="toCo ...

  9. sqlserver开启远程访问

    1.通过本地连接数据库,选择数据库——右键——属性 2.在连接选项勾选“允许远程连接到此服务器” 3.打开sqlserver配置管理器 4.到sqlserver网络配置——XXX的协议——TCP/IP ...

  10. 范进中Nature——儒林外史新义

    范进中Nature——儒林外史新义 范进发了文章回办公室,实验室一块儿搬砖的挂名作者俱各欢喜.正待烧锅煮方便面,只见他老板胡副教授,手里拿着一包外卖和一瓶红星二锅头,走了进来.范进向他作揖,坐下.胡副 ...