Reading comprehension HDU - 4990
#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的更多相关文章
- 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] 故 ...
- hdu-4990 Reading comprehension(快速幂+乘法逆元)
题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification
论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification 目前,阅读理解通常会给出 ...
- Attention-over-Attention Neural Networks for Reading Comprehension论文总结
Attention-over-Attention Neural Networks for Reading Comprehension 论文地址:https://arxiv.org/pdf/1607.0 ...
- hdu 4990(数学,等比数列求和)
Reading comprehension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU4990 Reading comprehension —— 递推、矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4990 Reading comprehension Time Limit: 2000/1000 MS (Java/Others ...
- Cognitive Graph for Multi-Hop Reading Comprehension at Scale(ACL2019) 阅读笔记与源码解析
论文地址为:Cognitive Graph for Multi-Hop Reading Comprehension at Scale github地址:CogQA 背景 假设你手边有一个维基百科的搜索 ...
- 机器阅读理解综述Neural Machine Reading Comprehension Methods and Trends(略读笔记)
标题:Neural Machine Reading Comprehension: Methods and Trends 作者:Shanshan Liu, Xin Zhang, Sheng Zhang, ...
- HDU - 4990 Reading comprehension 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...
随机推荐
- Inside The C++ Object Model(四)
============================================================================4-1. Member 的各种调用方式静态成员函 ...
- poj 1873
哇实验室里正在吵架,爽死了! wf水题.显然二进制枚举,注意剪枝,val>ans的时候剪一下,不然会tle.然后就没惹. 我老人家一开始写了个 感觉非常垃圾,wa了一发又t了一发. 感觉自己可以 ...
- mybatis02--增删改查
1.修改StudentDao public interface StudentDao { /** * 新增学生信息 */ void addStudent(Student student); // 新增 ...
- mysql数据库数据的 备份以及还原
数据库备份的3种方式: 例如:mysqldump -uzx_root -p test>/root/test1.sql
- Access无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开
使用SQL Server导入有密码的Access数据库内容,连接时出现错误提示: Access无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开 参考百度信息,可以点上图中的高级,在 ...
- 通过Navicat远程连接MySQL
参考: http://blog.csdn.net/apple9005/article/details/53033148 问题一:在主机下通过Navicat连接服务器MySql的时候,提示“2003 C ...
- Gym 101194E / UVALive 7901 - Ice Cream Tower - [数学+long double][2016 EC-Final Problem E]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- RabbitMQ的几个概念
VHost:虚拟主机,不同的VHost之间完全隔离,互不干扰.类似我们使用VM创建的多个虚拟机.创建好VHost之后需要我们指定其所有者.创建方式:RabbitMQ管理控制台 - Admin → Vi ...
- 线程安全 Thread Safety Problem scala concurrency 并发
小结: 1.基于java并发模型 Scala concurrency is built on top of the Java concurrency model. 2. 将每个请求放入一个新的线程 T ...
- CentOS中service命令与/etc/init.d的关系以及centos7的变化
缘由 由于个人经常在ubuntu和centos 系统中切换,习惯了以前的 ubuntu中 通过 /etc/init.d/xxx 进行软件服务控制.后来发现centos7中换了服务的控制方式:servi ...