现在的位置: 首页 > 综合 > 正文

HDU 2256 Problem of Precision 构造矩阵

2018年01月19日 ⁄ 综合 ⁄ 共 1030字 ⁄ 字号 评论关闭

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;
} 

抱歉!评论已关闭.