Google编程竞赛题:计算 (3 + √5)^n 的整数部分末三位数
By Long Luo
袁哥在微博上发布了一道 数学挑战题 :
计算 的整数末三位数,给出能口算或者可以用计算器计算的算法的第一个人,免费给一个价值 1000 元的 A9 投资分享群入群名额。
最开始看到这道题目时,我觉得很简单啊,于是给出了下面的解答:
令 ,两边同取对数, , , ,所以 。
但问题没有这么简单,因为上述解答只在 是正确的, , 就不对了,因为精度不够!
之外,根据评论中其他人给的思路,分析得出这个是周期性的,所以又提交了下面的答案:
但问题仍然没有这么简单,因为即使循环周期 , 取的位数再多仍然会出现精度不够的问题。
袁哥的题解省略了很多东西,很多人可能看不太明白。我重新写了题解,重新整理了思路及缺失的步骤,外加证明,有高一数学水平即可看懂。
后来通过搜索,发现这道题是 Google CodeJam[1] 编程竞赛题,原题如下:
Problem:
In this problem, you have to find the last three digits before the decimal point for the number .
For example, when , , The answer is .
For , , The answer is .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.Output:
For each input case, you should output:Case #X: Y
where X is the number of the test case and Y is the last three integer digits of the number (3 + √5)n. In case that number has fewer than three integer digits, add leading zeros so that your output contains exactly three digits.Limits
Time limit: 30 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100Small dataset (Test set 1 - Visible)
2 ≤ n ≤ 30Large dataset (Test set 2 - Hidden)
2 ≤ n ≤ 2000000000
这道题有好几种方法可以解决!
最核心的就是利用共轭根式[2] , 就是 的共轭。
设 , , 则 .
可以构造一个数列:
可以证明 为整数[3] ,显然 , 构成一个等比数列, 那么 。