2172: Fibonacci Again(數論)

2172: Fibonacci Again

時間限制: 1 Sec 內存限制: 16 MB
提交: 183 解決: 52
您該題的狀態:已完成
[提交][狀態][討論版]
題目描述
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
樣例輸入
0
1
2
3
4
5
樣例輸出
no
no
yes
no
no
no
Code

#include<iostream>
using namespace std;
int fib[1000005];
int main(){
	int a;
	fib[0] = 7 % 3;
	fib[1] = 11 % 3;
	for (int i = 2 ; i < 1000001; i ++ ){
		fib[i] =( fib[i-1] % 3  + fib[i-2] % 3 ) % 3;
	} 
	while ( cin >> a){
		if ( fib[a]){
			cout << "no" <<endl;
		}else {
			cout << "yes" <<endl;
		}
	}
	return 0;
}  

總結:
當數太大時要考慮取餘或找規律,不然內存會超限。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章