Fibonacci Again
题目链接
Problem Description
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
Input
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
Output
Print the word “yes” if 3 divide evenly into F(n).
Print the word “no” if not.
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
问F(n)能否被3整除.
寻找规律是件有意思的事情,发现规律是很令人鸡冻的.
n < 1,000,000, F(n)最大时已经不能用long long 表示.
能被3整除的数如123,各位数字相加也能被3整除.所以F(i)可化简为个位数字.
又F(i)的范围为[1, 9], 所以周期肯定在9*9=81之内, 又因为不会有两个9同时出现的情况,所以周期就是80.
计算前80项,然后n%80即可.
1 |
|