题目如下:
Fibonacci Again
Time limit: 1 Seconds
Memory limit: 32768K
Total Submit: 3840
Accepted Submit: 1544
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
Problem Source:
ZOJ Monthly, December 2003
刚看题目,觉得很简单,一个递归就可以了:
#include <stdio.h> #include <stdbool.h>
int F(int); bool J(int);
int main(int argc, char *argv[]) { int n; while (scanf("%d", &n)) (J(F(n))) ? puts("yes") : puts("no"); return 0; }
int F(int n) { if (n == 0) return 7; if (n == 1) return 11; return F(n - 1) + F(n - 2); }
bool J(int f) { return ((f % 3) ? false : true); } |
提交,结果提示:Time Limit Exceeded
看来程序效率较低,把递归改为非递归,如下:
#include <stdio.h> #include <stdbool.h>
int F(int); bool J(int);
int main(int argc, char *argv[]) { int n; while (scanf("%d", &n)) (J(F(n))) ? puts("yes") : puts("no"); return 0; }
int F(int n) { int i, temp[2] = {7, 11}; if (n == 0) return 7; if (n == 1) return 11; for (i = 2; i <= n; i ++) if (i % 2) temp[1] = temp[0] + temp[1]; else temp[0] = temp[0] + temp[1]; return temp[n % 2]; }
bool J(int f) { return ((f % 3) ? false : true); } |
再次提交,还是没有AC,于是结果前20个打印出来,看是否有规律,如下:
no no yes no no no yes no no no yes no no no yes no no no yes no |
可见每隔3个"no"有一个"yes",那么是不是每当 n == 4k + 2 (k >= 0) 时都有 F(n) == 3x (x >= 0) 呢?用数学归纳法试证明之:
显然 k == 0 时假设成立。设 k == m - 1 (m > 0) 时成立,则有 F(k) == F(m - 1) == F(4m - 2) == 3x(x > 0);设 F(4m - 1) == t,则F(4m) == 3x + t;F(4m + 1) == 3x + 2t;F(4m + 2) == 6x + 3t == 3y;可知 k == m 时假设也成立。
于是写代码如下:
#include <stdio.h>
int main(int argc, char *argv[]) { int n;
while (scanf("%d", &n) != EOF) (n % 4 == 2) ? puts("yes") : puts("no"); return 0; } |
提交,Accepted