HDU 2256 Problem of Precision (矩阵快速幂+ 化简 +向下取整)
来源:互联网 发布:淘宝网店怎么发布商品 编辑:程序博客网 时间:2024/06/10 03:54
Problem of Precision
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1565 Accepted Submission(s): 951
Problem Description
Input
The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n. (1 <= n <= 10^9)
Output
For each input case, you should output the answer in one line.
Sample Input
3125
Sample Output
997841
代码如下:
#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <stdio.h>#include <cmath>#include <string>#include <stack>typedef long long ll;const int MOD=1024;const int N =20;const int MAXN=2;using namespace std;struct Matrix{ll arr[N][N];void init(){memset(arr,0,sizeof(arr));for(int i=0;i<MAXN;i++)arr[i][i]=1;//初始化}void iinit(){ memset(arr,0,sizeof(arr)); arr[0][0]=arr[1][1]=5; arr[0][1]=12; arr[1][0]=2;}}A;Matrix mul(Matrix X,Matrix Y)// 矩阵乘法{Matrix ans;for(int i=0;i<MAXN;i++)for(int j=0;j<MAXN;j++){ans.arr[i][j]=0;for(int k=0;k<MAXN;k++){ans.arr[i][j]+=X.arr[i][k]*Y.arr[k][j];ans.arr[i][j]%=MOD;}}return ans;}Matrix Q_pow(Matrix B,int n)// 矩阵快速幂{Matrix ans;ans.init();while(n){if(n&1)ans=mul(ans,B);n>>=1;B=mul(B,B);}return ans;}Matrix Add(Matrix a,Matrix b) //(a+b)%mod 矩阵加法{ int i,j,k; Matrix ans; for(i=0;i<MAXN;i++) for(j=0;j<MAXN;j++) { ans.arr[i][j]=a.arr[i][j]+b.arr[i][j]; ans.arr[i][j]%=MOD; } return ans;}Matrix Sum(Matrix a,int n)// 矩阵和{int m;Matrix ans,pre;if(n==1) return ans;m=n/2;pre=Sum(a,m);ans=Add(pre,mul(pre,Q_pow(a,m)));if(n&1)ans=Add(ans,Q_pow(a,n));return ans;}int main(){ int T; cin>>T; int n; while(T--) { scanf("%d",&n); Matrix ans; ans.iinit(); if(n==1) { printf("9\n"); continue; } ans=Q_pow(ans,n-1); ll res=0; //cout<<ans.arr[0][0]<<" "<<ans.arr[0][1]<<" "<<endl; res=(ans.arr[0][0]*5+ans.arr[0][1]*2)%MOD; printf("%lld\n",(2*res-1)%MOD); } return 0;}
阅读全文
0 0
- HDU 2256 Problem of Precision (矩阵快速幂+ 化简 +向下取整)
- HDU 2256 Problem of Precision (矩阵快速幂)
- Hdu 2256 Problem of Precision[矩阵快速幂 + 数学]
- HDU 2256 Problem of Precision(矩阵快速幂)
- hdu 2256 Problem of Precision(矩阵快速幂 推公式)
- HDU - 2256 Problem of Precision 矩阵快速幂
- hdu 2256 Problem of Precision 矩阵快速幂
- HDU 2256 Problem of Precision(矩阵快速幂)
- HDU 2256 Problem of Precision(矩阵快速幂 数论 )
- HDU 2256 Problem of Precision(矩阵快速幂)
- HDU.2256 Problem of Precision (矩阵快速幂)
- Problem of Precision(矩阵快速幂)
- [矩阵快速幂]hdu2256 Problem of Precision
- hdu 2256 Problem of Precision(矩阵快速幂,得出递推式有难度)
- HDU 2256 Problem of Precision 解题报告(矩阵快速幂 + 构造)
- HDU 2256 Problem of Precision(矩阵快速幂入门题)
- 【矩阵快速幂-求平方根表达式的值】HDU Problem of Precision 2256
- hdu 2256 Problem of Precision 矩阵
- java 字节输入流和字节输出流综合使用案例
- python学习:input()和raw_input()
- 算法之冒泡排序
- 学习Java第三天
- VS2012下创建一个MFC加法计算器工程示例
- HDU 2256 Problem of Precision (矩阵快速幂+ 化简 +向下取整)
- 数据库三范式
- Java线程优先级的概念
- Java-随机范围内分割大文件
- Toolbar标题居中怎么设置
- Android设备命令打开adb
- AppDomain
- javaWeb项目的classpath说明
- 创建Java线程的3种方式及对比