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 为奇 ...
随机推荐
- 【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String
我们知道在mybatis的映射中传参数,只能传入一个.通过#{参数名} 即可获取传入的值. Mapper接口文件: public int delete(int id) throws Exception ...
- array_walk与array_map的区别
1.array_walk是用于用户自定义的函数,所以想用array_walk($aIds, "trim");去掉数据元素中的空格是达不到目的的只能用array_walk($aIds ...
- C# 求俩个正整数的最小公倍数和最大公约数
C# 求俩个正整数的最小公倍数和最大公约数 1.公倍数.最小公倍数 两个或多个整数公有的倍数叫做它们的公倍数,其中除0以外最小的一个公倍数就叫做这几个整数的最小公倍数 翻开小学5年级下册PPT 1.1 ...
- nginx的日志配置
本文转自:https://www.cnblogs.com/biglittleant/p/8979856.html 版权归属原作者!!!!!! nginx access日志配置 access_log日志 ...
- PCB画板总结
最近几天完成了第一个PCB电路板.虽然器件不是很多,手动布线了4次才达到自己理想的效果. 但是还是有很多细节只有亲自拿到了自己做的板子,亲自焊接之后,才知道自己哪里不合适. 这是修改了4次之后的最终的 ...
- redis(四)--简单实现Redis缓存中的排序功能
在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发的应 ...
- centOS 安装gitlab-runner
1. curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sud ...
- web项目访问地址前添加小图片
修改HTML 1.head标签添加 <link rel="icon" type="image/x-icon" href="images/icon ...
- css的小知识
---恢复内容开始--- 1.当你发现在制作页面时出现滚动条就需要一个去除滚动条的属性 overflow:hidden: overflow-x:hidden:水平超出隐藏 2. ...
- ML.NET 0.9特性简介
ML.NET 0.9已于上周发布,距离上次0.8版本的发布只有一个多月,此次增加的新特性主要包括特征贡献计算,模型可解释性增强,ONNX转换对GPU的支持,Visual Studio ML.NET项目 ...