(斐波那契数列)

题目描述

There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2). 

输入描述:

Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
       

输出描述:

Print the word "yes" if 3 divide evenly into F(n).
       

Print the word "no" if not.
       
示例1

输入

0
1
2
3
4
5

输出

no
no
yes
no
no
no

做这道题真的是花费了很长的时间,可能是因为很久没有做题了,第一反应就是同余定理,斐波那契数列来一波,但是后来发现这样很麻烦,递归的话,应该会超时。然后又查找以下规律,发现mod3后,余数是:1、2、0、2、2、1、0、1、1、2...就是fn=fn-1+fn-2.只要n%4==2,此时fn就能被3整除,余数为0.

#include<cstdio>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
    long n;
    while(scanf("%ld",&n)!=EOF){
        if(n%4==2)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_32823673/article/details/79957723