HDU 2256 Problem of Precision 构造矩阵

Problem of Precision

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 948    Accepted Submission(s): 554

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
  
  
3 1 2 5
 
Sample Output
  
  
9 97 841

/*
HDU 2256 构造矩阵 
题解 见上图  
*/
#include<iostream>
#include<stdio.h>
using namespace std;
#define M 1024

struct matrix{
	int map[2][2];
};
matrix mat,ans;

void init()
{
	int i,j;
	mat.map[0][0]=5;
	mat.map[0][1]=12;
	mat.map[1][0]=2;
	mat.map[1][1]=5;
	
	for(i=0;i<2;i++)
		for(j=0;j<2;j++)
			ans.map[i][j]=(i==j);
}

matrix mul(matrix a,matrix b)
{
	int i,j,k;
	matrix c;
	for(i=0;i<2;i++)
		for(j=0;j<2;j++)
		{
			c.map[i][j]=0;
			for(k=0;k<2;k++)
			c.map[i][j]+=a.map[i][k]*b.map[k][j];
			c.map[i][j]%=M;
		}
	return c;
}

void pow(int n)
{
	for(;n;n>>=1)
	{
		if(n&1)
			ans=mul(ans,mat);
		mat=mul(mat,mat);
	}
}

int main()
{
	int t,i,j,n,sum;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		init();
		pow(n);
		sum=(ans.map[0][0]*2-1)%M;
		printf("%d\n",sum);
	}
	return 0;
} 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值