HDU 2256 Problem of Precision
2017-09-03 20:57
417 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2256
这个题目是求
![](https://oscdn.geek-share.com/Uploads/Images/Content/201709/62717e0780e38c549c6ef41d8999e9dd)
直接求有些麻烦,但是我们可以转换下成这个形式
![](https://oscdn.geek-share.com/Uploads/Images/Content/201709/75f26285e3aa91e27b1dc39f38e63399)
这样你会发现他可以转化成 an+bn*sqrt(6)的形式
![](https://oscdn.geek-share.com/Uploads/Images/Content/201709/21935088f3004377882a483057511c51)
然后就会发现它可以转化为矩阵了。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201709/65a91b78c83b69737d4c5ada99a686f2)
最后代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int Mod = 1024;
struct martix{
int M[2][2];
void zero(){
memset(M,0,sizeof(M));
}
void unit(){
this->zero();
for(int i = 0; i < 2; i++)
M[i][i] = 1;
}
};
martix operator * (martix A,martix B){
martix res;
res.zero();
for(int i = 0; i < 2; i++)
for(int k = 0; k < 2; k++)
for(int j = 0; j < 2; j++){
res.M[i][j] += (A.M[i][k]*B.M[k][j])%Mod;
res.M[i][j] = (res.M[i][j]+Mod)%Mod;
}
return res;
}
martix operator ^ (martix A,int N){
martix res;
res.unit();
while(N){
if(N & 1)res = res*A;
A = A*A;
N >>= 1;
}
return res;
}
int main(){
int T,n;
scanf("%d",&T);
martix P,F,Ans;
P.zero();
P.M[0][0] = 5;P.M[0][1] = 12;
P.M[1][0] = 2;P.M[1][1] = 5;
F.zero();
F.M[0][0] = 5;
F.M[1][0] = 2;
while(T--){
scanf("%d",&n);
Ans.zero();
Ans = P^(n-1);
Ans = Ans*F;
printf("%d\n",(2*Ans.M[0][0]-1)%Mod);
}
return 0;
}
这个题目是求
直接求有些麻烦,但是我们可以转换下成这个形式
这样你会发现他可以转化成 an+bn*sqrt(6)的形式
然后就会发现它可以转化为矩阵了。
最后代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int Mod = 1024;
struct martix{
int M[2][2];
void zero(){
memset(M,0,sizeof(M));
}
void unit(){
this->zero();
for(int i = 0; i < 2; i++)
M[i][i] = 1;
}
};
martix operator * (martix A,martix B){
martix res;
res.zero();
for(int i = 0; i < 2; i++)
for(int k = 0; k < 2; k++)
for(int j = 0; j < 2; j++){
res.M[i][j] += (A.M[i][k]*B.M[k][j])%Mod;
res.M[i][j] = (res.M[i][j]+Mod)%Mod;
}
return res;
}
martix operator ^ (martix A,int N){
martix res;
res.unit();
while(N){
if(N & 1)res = res*A;
A = A*A;
N >>= 1;
}
return res;
}
int main(){
int T,n;
scanf("%d",&T);
martix P,F,Ans;
P.zero();
P.M[0][0] = 5;P.M[0][1] = 12;
P.M[1][0] = 2;P.M[1][1] = 5;
F.zero();
F.M[0][0] = 5;
F.M[1][0] = 2;
while(T--){
scanf("%d",&n);
Ans.zero();
Ans = P^(n-1);
Ans = Ans*F;
printf("%d\n",(2*Ans.M[0][0]-1)%Mod);
}
return 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(矩阵快速幂)
- 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(矩阵快速幂)
- HDU2256-Problem of Precision(矩阵构造+高速幂)