HDU 1021 Fibonacci Again

来源:互联网 发布:js原型是什么 编辑:程序博客网 时间:2024/05/24 01:54

Fibonacci Again

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


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).//如果f(n)能被三整除,则输出“yes”,

Print the word "no" if not.//否则输出“no”。
 

Sample Input
012345
 

Sample Output
nonoyesnonono
题目比较明确,输入结束标志没明确阐明就用EOF文件尾结束即可。
思路:
首先说显然要避免暴力,
其实如果稍微有点数论的只是的话就可以想到能被三整除就是f(n)≡0(mod3),容易想到用余数来做,再稍进一步计算就可发现它是循环的,这就简单了,经计算发现他是
八个一循环,而在每个循环中都有2个数使得其能被三整除,分别是第二个和第六个,即;1,2,0,2,2,1,0,1,所以:
#include<iostream>using namespace std;int main(){    int n;    while(cin>>n)    {        if(n%8==2||n%8==6)//对八取余判断是否可被三整除            cout<<"yes"<<endl;        else            cout<<"no"<<endl;    }    return 0;}


0 0
原创粉丝点击