hdu 4990 Reading comprehension(矩阵乘法)
来源:互联网 发布:苹果版解压缩软件 编辑:程序博客网 时间:2024/06/10 23:44
Problem Description
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;
}
#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;
}
Input
Multi test cases,each line will contain two integers n and m. Process to end of file.
[Technical Specification]
1<=n, m <= 1000000000
[Technical Specification]
1<=n, m <= 1000000000
Output
For each case,output an integer,represents the output of above program.
Sample Input
1 103 100
Sample Output
15
根据上面的代码可以发现就是让求一个数组的第n项
奇数项等于前一项*2+1,偶数项等于前一项*2:
a0 = 0;
a1 = a0*2+1;
a2 = a1*2;
a3 = a2*2+1;
a4 = a3*2;
如果直接构造矩阵求第n项好像不太好求,但是我们发现偶数项或者奇数项之间的公式是固定的,我们就是偶数项为例吧。
可以推出:
a2 = 4*a0 + 2;
a4 = 4*a2 + 2;
这样就可以很轻松的构造出矩阵,如果n是偶数的话,直接就可以求出答案,如果是奇数项,那么就*2+1.
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define LL long longusing namespace std;int mod;struct Matrix{ long long m[2][2]; int n; Matrix(int x) { n = x; for(int i=0;i<n;i++) for(int j=0;j<n;j++) m[i][j] = 0; } Matrix(int _n,int a[2][2]) { n = _n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { m[i][j] = a[i][j]; } }};Matrix operator *(Matrix a,Matrix b){ int n = a.n; Matrix ans = Matrix(n); for(int i=0;i<n;i++) for(int j=0;j<n;j++) for(int k=0;k<n;k++) { ans.m[i][j] += (a.m[i][k]%mod)*(b.m[k][j]%mod)%mod; ans.m[i][j] %= mod; } return ans;}Matrix operator ^(Matrix a,int k){ int n = a.n; Matrix c(n); int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) c.m[i][j] = (i==j); for(;k;k>>=1) { if(k&1) c=c*a; a = a*a; } return c;}int main(void){ int n,m,i,j; while(scanf("%d%d",&n,&m)==2) { mod = m; int a[2][2] = { 4,0, 2,1}; Matrix A(2,a); A = A^(n/2); LL ans = A.m[1][0]; if(n % 2 == 1) ans = (ans*2+1)%mod; cout << ans << endl; } return 0;}
阅读全文
0 0
- hdu 4990 Reading comprehension(矩阵乘法)
- hdu 4990 Reading comprehension(矩阵法)
- HDU 4990 Reading comprehension(构造矩阵)
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- HDU 4990 Reading comprehension(矩阵快速幂优化)
- HDU 4990 Reading comprehension(矩阵快速幂)
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- hdu 4990 Reading comprehension (矩阵快速幂)
- hdu 4990 Reading comprehension(矩阵快速幂)
- [矩阵快速幂] hdu 4990 Reading comprehension
- HDU - 4990 Reading comprehension (矩阵快速幂)
- HDU 4990 Reading comprehension (矩阵快速幂)
- hdu 4990 Reading comprehension 矩阵快速幂
- hdu-4990 Reading comprehension(快速幂+矩阵)
- hdu 4990 Reading comprehension
- hdu 4990 Reading comprehension
- hdu 4990 Reading comprehension
- HDU 4990 Reading comprehension
- C语言——实例009 输出国际象棋棋盘。
- 函数(2)
- git 总结
- 分布式存储-fastdfs+glusterfs- 学习笔记
- Android 6.0动态权限申请
- hdu 4990 Reading comprehension(矩阵乘法)
- 美国国家安全局如何分析通话数据
- Tomcat启动报错:org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalin
- unity3d实现一个魔方旋转展示
- 2016校招真题编程练习——微信红包(腾讯)
- 两道面试题——进制各位数之和
- 【动态规划】最长上升子序列及其相关思想
- 1018. Public Bike Management (30)
- 滑雪 POJ