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
首先,考虑一个问题,我们是要解题? 还是要求出结果?
求结果的话当然可以利用循环求得结果,哪怕这个程序运行的再慢都无所谓。
解题的话思路可能不对,因为 1,000,000这个数太大了,不能用MOD求。
一开始写这道题的时候我就按照正常思维写个循环,最后提交的时候发现时间超限。
前面我已经说过,我们是来解题的,所以,换个思路:找规律:
f(0)=7; 7%3==1
f(1)=11; 11%3==2
f(2)=f(1)+f(0)=11+7=18; 18%3=0
f(3)=f(2)+f(1)=18+11=29; 29%3==2
f(4)=f(3)+f(2)=29+18=47; 47%3==2
f(5)=f(4)+f(3)=47+29=76; 76%3==1
f(6)=f(5)+f(4)=76+47=123; 123%3==0
f(7)=f(6)+f(5)=123+76=199; 199%3==1
f(8)=f(7)+f(6)=199+123=322; 322%3==1
f(9)=f(8)+f(7)=322+199=521; 521%3==2
f(10)=f(9)+f(8)=521+322=843; 843%3==0
规律:除了前两个,其余都是4个一循环,出现被3整除的数。
所以,关键语句就出来了:
a%4==2.
代码:
#include<stdio.h>
int main()
{
int a;
while(~scanf("%d",&a))
{
if(a%4==2)
printf("yes\n");
else printf("no\n");
}
return 0;
}