当前位置: 代码迷 >> 综合 >> HDU-1021-Fibonacci Again
  详细解决方案

HDU-1021-Fibonacci Again

热度:55   发布时间:2023-12-26 01:00:52.0

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

分析:先初始化数组,对于a>=2的情况就是i[a]=(i[a-1]%3+i[a-2]%3)%3,先分部取余再对和取余这样就不会超过int 范围

代码如下

#include <cstdio>
#define maxn 1000005
int i[maxn];
int main()
{i[0]=7;i[1]=11;for(int a = 2;a < maxn;a ++)i[a]=(i[a-1]%3+i[a-2]%3)%3;int n;while(~scanf("%d",&n)){if(i[n])printf("no\n");elseprintf("yes\n");}return 0;
}