ZOJ 2060 A-Fibonacci Again

https://vjudge.net/contest/67836#problem/A

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

 时间复杂度:$O(10^6)$

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 10;
int Fib[maxn];

int main() {
    Fib[0] = 7, Fib[1] = 11;
    for(int i = 2; i < maxn; i ++)
        Fib[i] = (Fib[i - 1] + Fib[i - 2]) % 3; //同余定理
    int n;
    while(~scanf("%d", &n)) {
        if(Fib[n] % 3)
            printf("no
");
        else
            printf("yes
");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zlrrrr/p/9465222.html