【CSU-1895】解题报告(数学,矩阵快速幂)
原始题目
1895: Apache is late again
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 |
|
收获与反思
- 矩阵快速幂练习
- 注意n<0时候输出No,因为这个T了好几次。 $$
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!