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
 
本题类似于HDU_1005:Number Sequence,算是其简化版。根据其思想,我们可以求F(n) = [F(n-1) + F(n-2)] mod 3。容易知道,数列的每项只能在0, 1, 2三个中间取值,因此组合数是9,由鸽巢原理,在10个组合情况中至少出现两对对应相等。换句话说,数列的循环周期至多是9。
可以手算出循环周期,当然亦可以写一个程序来算一下:
#include<stdio.h>
int main(void)
{
int f0 = , f1 = , j = ;
int a[]; for(int i = ; i < ; i++)
{
a[j] = (f0+f1)%;
f0 = f1;
f1 = a[j++];
} for(int i = ; i < ; i++)
printf("%d ", a[i]); return ;
}

输出结果:0 2 2 1 0 1 1 2 0 2。容易看出,循环周期为8,而从第一个数起每隔三个数0便出现一次。

因此,容易写出以下代码:

#include<stdio.h>
int main(void)
{
int n;
while(scanf("%d", &n) != EOF)
{
if((n-)% == )
printf("yes\n");
else
printf("no\n"); } return ;
}

当然,与HDU_1005不同的是,本题的数列的每项是确定的,因此可以打表通过:

#include<stdio.h>
#define MAXN 1000000
int a[MAXN+];
int main(void)
{
a[] = ;
a[] = ;
int n;
for(int i = ; i <= ; i++)
{
a[i] = (a[i-] + a[i-]) % ;
} while(scanf("%d", &n) != EOF)
{
if(a[n] == )
printf("yes\n");
else
printf("no\n");
} return ;
}

或许HDOJ系统测试强度不够吧,这题暴力破解都可以通过:

#include<stdio.h>
int main(void)
{
int f0 = , f1 = , j = , f2;
int a[];
int n;
while(scanf("%d", &n) != EOF)
{
f0 = ;
f1 = ;
for(int i = ; i <= n; i++)
{
f2 = (f0 + f1) % ;
f0 = f1;
f1 = f2;
} if(n == || n == )
printf("no\n");
else if(f2 == )
printf("yes\n");
else
printf("no\n");
} return ;
}

另外,打表的时候要注意,该题不mod3的话,也就是直接存入每项的具体值,然后直接取出求模的方式是不可取的,因为对于1, 1开头的斐波那契数列的第四十余项int类型已经存不下了(可以写个程序测试),这是因为斐波那契数列以接近0.618为底的指数增长。指数可是会爆炸的啊:~

HDU_1021:Fibonacci Again的更多相关文章

  1. 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找

    今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...

  2. #26 fibonacci seqs

    Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...

  3. 关于java的递归写法,经典的Fibonacci数的问题

    经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...

  4. 斐波拉契数列(Fibonacci) 的python实现方式

    第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...

  5. fibonacci数列(五种)

    自己没动脑子,大部分内容转自:http://www.jb51.net/article/37286.htm 斐波拉契数列,看起来好像谁都会写,不过它写的方式却有好多种,不管用不用的上,先留下来再说. 1 ...

  6. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  7. Fibonacci 数列算法分析

    /************************************************* * Fibonacci 数列算法分析 ****************************** ...

  8. 算法系列:Fibonacci

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  9. UVa #11582 Colossal Fibonacci Numbers!

    巨大的斐波那契数 The i'th Fibonacci number f (i) is recursively defined in the following way: f (0) = 0 and  ...

随机推荐

  1. Hibernate-实体-对象状态-一级缓存-事务-查询

    一 hibernate中的实体规则 1.1 实体类创建的注意事项 持久化类提供无参数构造         --在调用instance()方法时默认调用空参构造 成员变量私有,提供共有get/set方法 ...

  2. Django之ORM多表操作

    1.创建一对多: 1.外键建在多的一方(如:一个出版社可出版多本书,所以建在书的表) 2.创建表: 1.创建外键 2.关联的表名 2.一对多数据的操作 2.1数据的添加: 第一种方法: 第二种方法: ...

  3. Linux 定时任务执行 php artisan

    */ * * * * php /www/wwwroot/project/artisan command:exec postNews 5分钟执行一次

  4. css3正方体效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Hackerrank--Ashton and String(后缀数组)

    题目链接 Ashton appeared for a job interview and is asked the following question. Arrange all the distin ...

  6. WebBrowser修改默认白色背景

      背景:在使用Winform的WebBrowser显示网页的时候,在网页还未加载完成之前会显示白色,刚好网页内容呈现黑色,这样视觉效果上就十分差,想把这层白色的去掉. 试了很久之后发现根本去不掉,应 ...

  7. poi操作word,简单写docx

    参考博客: https://www.cnblogs.com/guilty/p/3977016.html 在HWPF中换行符是"\013",在XWPF中是run.addBreak() ...

  8. spring boot 的 ApplicationContext 及 getbean

    在spring中,我们通过如下代码取得一个spring托管类: ApplicationContext ac = new FileSystemXmlApplicationContext("ap ...

  9. 洛谷P1650 赛马[2017年5月计划 清北学堂51精英班Day1]

    P1650 赛马 题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负者这 ...

  10. js中index()的四种经典用法111

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...