Hdu1021 Fibonacci again Law

Source: Internet
Author: User
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 descriptionthere are another kind of Maid: F (0) = 7, F (1) = 11, F (n) = f (n-1) + f (n-2) (N> = 2 ).

Inputinput consists of a sequence of lines, each containing an integer n. (n <1,000,000 ).

Outputprint the word "yes" if 3 divide evenly into F (n ).

Print the word "no" if not.

Sample Input
012345

Sample output
nonoyesnonono
This is a transformed Fibonacci series, but if you write a recursive function, the memory is not enough because the range of N is large. If you type the first 20 items, you will find that the rule is yes from 2, yes from 4, and no from the rest. There is absolutely no problem in programming according to this rule, but I still want to prove its correctness here. Let's look at the first seven items and set them to AI:

7 11 18 29 47 76 123

A1 A2 A3 A4 A5 A6 A7

Yi Zhi A3 and A7 can be divisible by 3. In fact, if you only know that A3 can be divisible by 3, you can launch A7 and be divisible by 3: a7 = A5 + A6 = A3 + A4 + A4 + A5 = A3 + A4 + A4 + A3 + A4 = 2 * A3 + 3 * A4 because A3 can be divisible by 3, SO 2 * A3 can be divisible by 3, and 3 * A4 can certainly be divisible by 3, so A7 can be divisible by 3.
Now we can prove that A4, A5, and A6 cannot be divisible by 3: ① A4 = a2 + A3 A2 can't, A3 can, so A4 can't; ② A5 = A3 + A4 A3 can, no A4, so A5 cannot; ③ A6 = A4 + A5 = A4 + A3 + A4 = A3 + 2 * a4a3 can, 2 * A4 can't, So A6 cannot ;#! : An integer cannot be divisible by three, so the integer cannot be divisible by three times.
We can use mathematical induction to extend it to N, which guarantees its correctness and will not prove it more here.
The code is simple:
#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");elseprintf("no\n");}return 0;}






Hdu1021 Fibonacci again Law

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.