HDU1021 Fibonacci Again 规律

Fibonacci Again

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 37002 Accepted Submission(s): 17876

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

这是一道变换了的斐波那契数列,但是你如果写成递归函数的话内存是不够的,因为n的范围很大,如果你把前20项打出来的话,你会发现规律:就是从2开始是yes,每隔4个就是yes,其余是no。按这个规律编程绝对没问题,但是我还是愿意在这里证明一下其正确性。

我们看前七项,并设为ai:

7    11   18   29   47  76   123

a1   a2   a3   a4   a5   a6   a7

易知a3和a7是能被3整除的,其实如果只知道a3能被3整除,就能推出a7也能被3整除:

a7 =     a5    +     a6

= a3 + a4 +  a4 +   a5

= a3 + a4 +  a4 + a3 + a4

= 2*a3   +   3*a4

因为a3能被3整除,所以2*a3能被3整除,  且3*a4肯定也能被3整除,所以a7能被3整除。

现在来证明 a4,a5,a6不能被3整除:

① a4=a2+a3      a2不能,a3能,所以a4不能;

② a5=a3+a4      a3能,a4不能,所以a5不能;

③a6=a4  +  a5

=a4  + a3 + a4

=a3 +  2*a4

a3能,2*a4不能,所以a6不能;

#!:一个整数不能被3整除,那么这个数的2倍也不能被3整除。

运用数学归纳法将其推广到n即可,一样能保证其正确性,在此不再多做证明。

代码就很简单了:

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
	int i,n,k;
	while(scanf("%d",&n)!=EOF)
	{
		if(n%4==2)
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}
时间: 2024-10-25 16:09:28

HDU1021 Fibonacci Again 规律的相关文章

zoj 2060 Fibonacci Again(fibonacci数列规律、整除3的数学特性)

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2060 题目描述: 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. (

HDU 1021[Fibonacci Again]规律

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1021 题目大意:首两项为7,11的斐波那契数列.若第n项能被3整除,输出yes,否则输出no 关键思想:模三加法情况有限,找规律. 代码如下: #include <iostream> using namespace std; int main(){ int n; while(cin>>n) if(n%8==2||n%8==6)cout<<"yes"<

HDU1021 Fibonacci Again

题意: F(0)= 7, F(1) = 11 F(n) = ( F(n-1) + F(n-2) ) (n>=2). 输入n,若F(n)(mod3)为0则输出yes,否则输出no 题解: 如果直接暴力求解,由于n可以达到1,000,000的规模,必定会超时或栈溢出.因此采用找规律的方法. n             0 1 2 3 4 5 6 7 8 9 10 11 12 13 F(n)mod3    1  2 0 2 2 1 0 1 1 2  0  2 2 1 从n=2开始每隔4个出现一次符合条

快速幂 HDU-1021 Fibonacci Again , HDU-1061 Rightmost Digit , HDU-2674 N!Again

1.   Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 77116    Accepted Submission(s): 34896 Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11,

递归方法求解Fibonacci数列

1.Fibonacci数列指的是:1,1,2,3,5,8,13,21,34......Fibonacci的规律是,从第3个数开始,每个数都是它前面两个数的和.那么如何通过编程求解任意第n个数的Fibonacci数呢??? 递归方法是求解该问题的一个渠道. 所谓递归,指的就是函数可以调用自身,这种调用可以是直接的或者间接的. #include<iostream> using namespace std; int Fib(int n); int main() { int n; int answer

hdu1021 数学题 并不是说难,而是数学题那种简单朴素的思想get不到

hdu1021 Fibonacci Again 一个斐波那契数列,求f[n]能否被3 整除. #include<stdio.h> int main() { long n; while(scanf("%ld",&n) != EOF) if (n%8==2 || n%8==6) printf("yes\n"); else printf("no\n"); return 0; } #include <iostream> #

在sqlserver中做fibonacci(斐波那契)规律运算

--利用sqlserver来运算斐波那契规律 declare @number intdeclare @A intdeclare @B intdeclare @C int set @A=1 set @B=2 set @Number=3 select @[email protected][email protected] while(@Number<60) begin    set @[email protected][email protected]  if(@@ERROR<>0)  go

acm hdu p1021 Fibonacci Again java解答 水水 找规律

Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44697    Accepted Submission(s): 21341 Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n)

HDU 1021.Fibonacci Again【规律】【不可直接求】【8月18】

Fibonacci Again 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 &q