杭电1021Fibonacci Again
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1021
题目:
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.
Print the word "no" if not.
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
思路:(a+b)%c=a%c+b%c,所以可以把F(0)看做1,F(1)看做2,F(3)看做0,可以看出该数列一定会循环,(且最大循环节为9,因为3*3=9)。。。。。。以此类推.
得到数列 (从0开始)1 2 0 2 2 1 0 1....(后面重复);
所以。。。用n对8取模就好了。
取模公式n=(n-1)%8+1;
ac代码:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cmath> 5 #include <cstring> 6 #include <queue> 7 #include <stack> 8 #include <map> 9 #include <vector> 10 11 #define PI acos((double)-1) 12 #define E exp(double(1)) 13 using namespace std; 14 15 int main (void) 16 { 17 int m; 18 while(scanf("%d",&m)==1) 19 { 20 m=(m-1)%8+1; 21 if(m==2||m==6) 22 cout<<"yes"<<endl; 23 else 24 cout<<"no\n"; 25 } 26 return 0; 27 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。