您的位置:首页 > 大数据 > 人工智能

YT02-简单数学课堂题-1004 Fibonacci Again -(5.31日-烟台大学ACM预备队解题报告)

2015-06-01 15:02 489 查看


Fibonacci Again


Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)


Total Submission(s) : 62 Accepted Submission(s) : 47


Font: Times New Roman | Verdana | Georgia


Font Size: ← →


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

计145-李英杰

但是没有一个数学公式,难死了= =!,我暴力了很多遍还是没有成功,我知道不能用暴力破解,最终才知道有这么个余数公式:

(a+b)%3= (a%3+b%3)%3

有了这个公式,那真是手到擒来了!

这题也体现出ACM与数学那不可分割的关系啊~

一般思路:

#include <iostream>
using namespace std;
int fib[1000001];
int main()
{
    int i,n;
    fib[0]=7,fib[1]=11;//这为斐波那契的前两个数
    for(i=2;i<1000001;i++)
      
  fib[i]=(fib[i-1]%3+fib[i-2]%3)%3;//算出前1000001个斐波那契数是否为3的倍数
while(cin>>n)//输入数
    {

        if(fib
==0)
            cout<<"yes"<<endl; //当为3的倍数时输出yes
        else
            cout<<"no"<<endl; //当不为的倍数时输出no

    }
    return 0;
}


但是仔细思考过后,才发现这道题有种更简单的方法,写出斐波那契数列的多位数后,你会发现能被整除三的数序列加二以后正好为四的倍数

第二种思路:

#include <iostream>
using namespace std;
int main()
{   
 long   n;   
 while(cin>>n)   
 {  
      if(n%4==2)   //当输入数的序列加二   为四的倍数时,则此斐波那契数即为 三的倍数          
         cout<<"yes"<<endl;    
      else
          cout<<"no"<<endl;   
 }    
return 0;
}


我从这道题上想说的是,数学的思想很重要,编程的逻辑思考,我觉得首先要转化为数学问题,用自己能想到的最简单的数学方法来实现编程,所以说数学很重要。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: