【CSU-1895】解题报告(数学,矩阵快速幂)

原始题目

1895: Apache is late again

  • Time Limit: 1 Sec
  • Memory Limit: 128 Mb
  • Submitted: 141
  • Solved: 45

Description

Apache is a student of CSU. There is a math class every Sunday morning, but he is a very hard man who learns late every night. Unfortunate, he was late for maths on Monday. Last week the math teacher gave a question to let him answer as a punishment, but he was easily resolved. So the math teacher prepared a problem for him to solve. Although Apache is very smart, but also was stumped. So he wants to ask you to solve the problem. Questions are as follows: You can find a m made (1 + sqrt (2)) ^ n can be decomposed into sqrt (m) + sqrt (m-1), if you can output \(m% 100000007\) otherwise output No.

Input

There are multiply cases. Each case is a line of \(n (|n| \le 10 ^ {18})\)

Output

Line, if there is no such m output No, otherwise output m% 100,000,007.

Sample Input

2

Sample Output

9

Hint

Source

中南大学第十一届大学生程序设计竞赛

题目大意

给定 n,若\({1 + \sqrt {2}} ^ n\),能表示成\(\sqrt {m} + \sqrt {m-1}\),则输出 m,否则输出 No

解题思路

\(n>0\)时,试着写出前几项

\((1+\sqrt{2})^1=\sqrt{2}+\sqrt{1}=\sqrt{1^2+1}+\sqrt{1^2}\)

\((1+\sqrt{2})^2=\sqrt{9}+\sqrt{8}=\sqrt{3^2}+\sqrt{3^2-1}\)

\((1+\sqrt{2})^3=\sqrt{50}+\sqrt{49}=\sqrt{7^2+1}+\sqrt{7^2}\)

\((1+\sqrt{2})^4=\sqrt{289}+\sqrt{288}=\sqrt{17^2}+\sqrt{17^2-1}\)

若只观察含有\(\sqrt{2}\)的项,我们能得到下面的数列 \[a_1=1,a_2=2,a_3=5,a_4=12,\cdots\]

可以看作是一个二阶差分方程,其通项的矩阵表达

\[ a_n \\ a_{n-1} \\ \end{bmatrix}=\begin{bmatrix} 2&1 \\1&0 \end{bmatrix}\begin{bmatrix} a_{n-1}\\ a_{n-2}\end{bmatrix}={\begin{bmatrix} 2&1\\ 1&0\end{bmatrix} }^{n-2}\begin{bmatrix}a_2\\ a_1 \end{bmatrix}\]

给定\(a_1=1\)\(a_2=2\),通过矩阵快速幂计算\(a_n\)

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <queue>
#define mod 100000007 //这里定义模
using namespace std;
const int maxn=1e5+5;
typedef long long ll;
struct Matrix
{
ll a[2][2];
Matrix()
{
memset(a,0,sizeof(a));
}
Matrix operator * (const Matrix y)
{
Matrix ans;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
ans.a[i][j] += (a[i][k]*y.a[k][j])%mod;
return ans;
}
Matrix operator = (const Matrix y)
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
a[i][j]=y.a[i][j];
}
Matrix operator *= (const Matrix y)
{
Matrix ans;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
ans.a[i][j] += (a[i][k]*y.a[k][j])%mod;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
a[i][j]=ans.a[i][j];
}
};

Matrix qpow(ll x)
{
Matrix ans;
ans.a[0][0]=ans.a[1][1]=1; //单位矩阵
Matrix mul;
mul.a[0][0]=2;
mul.a[0][1]=mul.a[1][0]=1;
while(x)
{
if(x&1)
ans *= mul;
mul *= mul;
x>>=1;
}
return ans;
}
ll pow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a%mod);
a=a*a%mod;
b>>=1;
}
return ans;

}

ll n;
int main()
{
while(~scanf("%lld",&n))
{
if(n<0) printf("No\n");
else if(n==0) printf("1\n");
else if(n==1) printf("2\n");
else if(n==2) printf("9\n");
else if(n==3) printf("50\n");
else
{
Matrix m=qpow(n-2);
ll ans=(m.a[0][0]*2%mod+m.a[0][1]%mod)%mod;
if(n&1) printf("%lld\n",ans*ans*2%mod);
else printf("%lld\n",(ans*ans%mod*2+1)%mod);
}
}
}

收获与反思

  • 矩阵快速幂练习
  • 注意n<0时候输出No,因为这个T了好几次。 $$