Reading comprehension HDU - 4990
2017-09-06 16:25
197 查看
Reading comprehension HDU - 4990
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
Sample Output 1 5
题意:就是上面的那个程序, 数据范围很大 思路: 妥妥的矩阵快速幂, Fn = Fn-1 + 2Fn-2 + 1;
构造矩阵然后用模板就行。
![](https://img-blog.csdn.net/20170906163143157?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzk0ODIyODA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
代码:
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
题意:就是上面的那个程序, 数据范围很大 思路: 妥妥的矩阵快速幂, Fn = Fn-1 + 2Fn-2 + 1;
构造矩阵然后用模板就行。
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<string> #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<queue> #include<stack> #include<vector> #include<set> #include<algorithm> #define maxn 10010 #define INF 0x3f3f3f3f #define eps 1e-8 #define MOD 1000000007 #define ll long long using namespace std; ll mod; struct Node { ll mat[3][3]; }; Node mul(Node a,Node b) { Node ret; memset(ret.mat,0,sizeof ret.mat); for(int i=0;i<3;++i) for(int j=0;j<3;++j) bcdf { ret.mat[i][j]=0; for(int k=0;k<3;++k) ret.mat[i][j]=(ret.mat[i][j]+a.mat[i][k]*b.mat[k][j]%mod+mod)%mod; } return ret; } Node Pow(Node a,ll n) { Node ret; memset(ret.mat,0,sizeof(ret.mat)); for(int i=0;i<3;++i) ret.mat[i][i]=1; Node tmp=a; while(n) { if(n&1)ret=mul(ret,tmp); tmp=mul(tmp,tmp); n>>=1; } return ret; } int main() { ll n,m; Node A; memset(A.mat,0,sizeof A.mat); A.mat[0][0]=A.mat[0][2]=A.mat[1][0]=A.mat[2][2]=1; A.mat[0][1]=2; Node B; memset(B.mat,0,sizeof B.mat); B.mat[0][0]=1;B.mat[1][0]=0;B.mat[2][0]=1; while(scanf("%lld%lld",&n,&m)!=EOF) { mod=m; Node ans; memset(ans.mat,0,sizeof ans.mat); ans=mul(Pow(A,n-1),B); printf("%lld\n",ans.mat[0][0]); } return 0; }
相关文章推荐
- Reading comprehension HDU - 4990(矩阵快速幂)
- Reading comprehension HDU - 4990(矩阵快速幂 递推)
- Reading comprehension HDU - 4990
- Reading Note: Gated Self-Matching Networks for Reading Comprehension and Question Answering
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- SAT阅读练习题:Reading Comprehension Test 3
- hdu-4990 Reading comprehension
- Attention-over-Attention Neural Network for Reading Comprehension----神经网络在阅读理解上的应用
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- 论文笔记《A Thorough Examination of CNN/Daliy Mail Reading Comprehension Task》
- End-to-End Answer Chunk Extraction and Ranking for Reading Comprehension
- hdu 4990(数学,等比数列求和)
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- hdu 4990 矩阵快速幂
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- 矩阵基础1001 HDU 4990
- HDU 4990 Reading comprehension(矩阵快速幂)
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- hdu 4990 Reading comprehension(BestCoder Round #8 1002)