Problem of PrecisionTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1050 Accepted Submission(s): 617
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
Sample Output
|
醉了。。。
构建好了矩阵,求出了两个系数X和Y,测试数据都过不了,纠结半小时。
原来不能利用X + Y * sqrt(6) 求解,我太天真了。。。o(╯□╰)o
题目说的很明白了,就不说题意了。
思路给个图就明白了
构建矩阵:
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#define MOD 1024
struct Matrix
{
int a[3][3];
};
Matrix ori, res;
void init()
{
memset(ori.a, 0, sizeof(ori.a));
memset(res.a, 0, sizeof(res.a));
for(int i = 0; i < 2; i++)
res.a[i][i] = 1;
ori.a[0][0] = 5, ori.a[0][1] = 2;
ori.a[1][0] = 12, ori.a[1][1] = 5;
}
Matrix muitl(Matrix x, Matrix y)
{
Matrix z;
memset(z.a, 0, sizeof(z.a));
for(int i = 0; i < 2; i++)
{
for(int k = 0; k < 2; k++)
{
if(x.a[i][k] == 0) continue;
for(int j = 0; j < 2; j++)
z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]) % MOD) % MOD;
}
}
return z;
}
int F[3], ans[3];
void solve(int n)
{
while(n)
{
if(n & 1)
res = muitl(ori, res);
ori = muitl(ori, ori);
n >>= 1;
}
for(int i = 0; i < 2; i++)
{
ans[i] = 0;
for(int k = 0; k < 2; k++)
ans[i] = (ans[i] + (F[k] * res.a[k][i]) % MOD) % MOD;
}
//double X = ans[0], Y = ans[1];
// double temp = X + Y * sqrt(6);
printf("%d\n", (2*ans[0] - 1) % MOD);
}
int main()
{
int t;
int N;
F[0] = 5, F[1] = 2;
scanf("%d", &t);
while(t--)
{
scanf("%d", &N);
init();
solve(N-1);
}
return 0;
}