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

79 篇文章 0 订阅

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[n]==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;
}

我从这道题上想说的是,数学的思想很重要,编程的逻辑思考,我觉得首先要转化为数学问题,用自己能想到的最简单的数学方法来实现编程,所以说数学很重要。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值