HDU 1021 Fibonacci Again

Fibonacci Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 58267    Accepted Submission(s): 27275


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
Author
Leojay
分析:有两种方法:第一种是找规律,除4余2的就输出yes。否则no.这种方法确实很神奇,想不到没关系!先给出第一种方法的AC代码
复制代码
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n;
 5     while(scanf("%d",&n)!=EOF)
 6     {
 7         if(n%4==2) printf("yes\n");
 8         else
 9             printf("no\n");
10     }
11     return 0;
12 }
复制代码

第二种方法,直接取对3取模!

由同余式的基本性质:

1)自反性:a = a( mod m)。

以及同余式的四则运算法则:

1)如果 a =b( mod m)且 c = d( mod m),则 a +c = (b + d)( mod m)。

可知,F(n) = F(n) ( mod m) = ( F(n-1) +F(n-2) )( mod m)。

下面给出AC代码:

 

复制代码
 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 int f[1000100];
 5 int main()
 6 {
 7 f[0]=1; f[1]=2;
 8 int n;
 9 while(scanf("%d",&n)!=EOF)
10 {
11 for(int i=2;i<=n;i++)
12 {
13 f[i] = (f[i-1]+f[i-2])%3;
14 }
15 if(f[n]%3==0)
16 printf("yes\n");
17 else
18 printf("no\n");
19 }
20 return 0;
21 }
复制代码

 

 

 

posted @   Angel_Kitty  阅读(424)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示
西雅图
01:09发布
西雅图
01:09发布
9°
东南风
3级
空气质量
相对湿度
60%
今天
小雨
2°/12°
周日
中雨
4°/9°
周一
雨夹雪
3°/9°