`
Simone_chou
  • 浏览: 196319 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Fibonacci Again(数学)

    博客分类:
  • HDOJ
 
阅读更多

Fibonacci Again

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


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

 

     题意:

     斐波那契数为 F [ 0 ] = 7,F [ 1 ] = 11,F [ N ] = F [ N - 1 ] + F [ N - 2 ] ( N >= 2 ),输入 N(<= 1000000),问 F[ N ] 能否被 3 整除。

 

     思路:

     数学。先把结果保存起来再判能不能整除的话,以 N == 1000000 的话 long long 都会爆,所以边加就边求余数,直接数组保存余数结果就好,若为 0 则输出 yes,否则则为 no。

     还有一种方法,找规律,按数组的顺序,余数的结果为 1,2,0,2,2,1,0,1,1,2,0,2,2,1,0……可以发现,当下标(n - 2)能整除 4 的时候能整除3,所以可以通过这个方法直接输出 yes 还是 no。

 

     AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 1000005;

int num[MAX];

void solve() {
        num[0] = 1;
        num[1] = 2;
        for (int i = 2; i < MAX; ++i)
                num[i] = (num[i - 1] + num[i - 2]) % 3;
}

int main () {
        int n;
        solve();
        while (~scanf("%d", &n)) {
                if (num[n]) printf("no\n");
                else printf("yes\n");
        }
        return 0;
}

 

    or:

#include <cstdio>

using namespace std;

int main () {
        int n;
        while (~scanf("%d", &n)) {
                (n - 2) % 4 ? printf("no\n") : printf("yes\n");
        }
        return 0;
}

 

分享到:
评论

相关推荐

    斐波那契数列

    斐波那契数列是数学中一个非常著名的数列,其定义如下:数列的第一项和第二项均为1,从第三项开始,每一项都等于前两项之和。用数学公式表示即为: \[ F(n) = \left\{ \begin{array}{ll} 0 & \quad n = 0 \\ 1 &...

    (lecture_02)简单数学题

    **斐波那契数列(Fibonacci Again)**:在HDOJ_1021题中,涉及到斐波那契数列与整除3的关系。如果两个斐波那契数的和能被3整除,它们自身可能也是3的倍数。可以利用斐波那契数的递推关系来简化问题,避免不必要的...

    ACM杭电入门训练题

    - **Fibonacci Again**: 斐波那契数列题目,要求高效计算斐波那契数列的某个值。 - **Coin Change**: 经典的组合数学题目,要求找出最少硬币数兑换目标金额的方法。 - **Lowest Common Multiple Plus**: 数学运算...

    C语言程序的设计实验指导书.pdf

    5. 选作题:Fibonacci Again,要求实现斐波那契数列的计算,测试递归或动态规划的实现。 实验三涉及循环结构程序设计,包含: 1. 整数的立方和:使用循环计算一个数列的元素立方和,可能用到`for`或`while`循环。 2...

    习题集 acm

    7. **Fibonacci Again**: 与斐波那契数列相关的题目,除了常规的递归和迭代解法,可能还涉及到矩阵快速幂等高级技巧。 8. **A+B for Input-Output Practice (I-VI)**: 这些题目是输入输出的进阶练习,可能会有...

Global site tag (gtag.js) - Google Analytics