• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ACM s1124yy
守りたいものが 強くさせること
博客园    首页    新随笔    联系   管理     

HDU 2256 Problem of Precision(矩阵)

Problem of Precision

【题目链接】Problem of Precision

【题目类型】矩阵

&题解:

参考:点这里
这题做的好玄啊,最后要添加一项,之后约等于,但是有double的时候一定不能取余,还是要记住的

&代码:

#include <cstdio>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
using ll=long long;
const int maxn= 1e3 +9;
ll n,M=1024;
typedef vector<ll> vec;
typedef vector<vec> mat;
mat mul(mat &A,mat &B)
{
	mat C(A.size(),vec(B[0].size()));
	for(int i=0;i<A.size();i++)
	for(int k=0;k<B.size();k++)
	for(int j=0;j<B[0].size();j++){
		C[i][j]=(C[i][j]+A[i][k]*B[k][j])%M;
	}
	return C;
}
mat bin_pow(mat A,ll n)
{
	mat B(A.size(),vec(A.size()));
	for(int i=0;i<A.size();i++)
		B[i][i]=1;
	while(n>0){
		if(n&1)
			B=mul(B,A);
		A=mul(A,A);
		n>>=1;
	}
	return B;
}
mat A(2,vec(2));
ll fin[]={5,2};
void init()
{
	A[0][0]=5,A[0][1]=12;
	A[1][0]=2,A[1][1]=5;
}
int main()
{
//	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	freopen("E:1.txt","r",stdin);
	int T;cin>>T;
	while(T--){
		init();
		cin>>n;
		//这块返回来的mat一定要赋值给A啊 不要忘了
		A=bin_pow(A,n-1);
		ll ans=0;
		for(int i=0;i<2;i++){
			ans=(ans+A[0][i]*fin[i])%M;
		}
		cout<<(2*ans-1)%M<<endl;
	}
	return 0;
}
posted @ 2017-03-23 02:13  s1124yy  阅读(187)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3