HDU 1021 Fibonacci 2, finding the law

Source: Internet
Author: User

Background: Violence is bound to exceed the data structure range, as the Fibonacci type sequence increases exponentially in the form of explosions. Therefore, the data found that the residual 30 does not affect the results.

Learning: 1. Number theory problems can write several sets of data to find the law, guess the conclusion.

My Code:

#include <stdio.h>int str[1000009] = {7,};int main () {int n;for (int i = 2; I <=1000000; i++) {Str[i] = (str[i -1] + str[i-2])% 30;} while (scanf ("%d", &n)! = EOF) {if (Str[n]% 3 = = 0)  printf ("yes\n"); else printf ("no\n");} return 0;}

Some people also write the recursive {a1≡b1 (mod3) According to the nature of congruence, get A1+b1 (mod3)}:
Str[i] = str[i-1]%3+ str[i-2]%3;

OMS Solution:

Find the law, one at a time to find out the number of three f[0]~f[n:

1,2,0,2,2,1,0,1,1,2,0,2,2,1,0,1,1,2,0,2,2,1,0,1 .... Continuous circulation

Our goal is to find the relationship between 0 and N after the remainder, except the first, 0 and 0 are three numbers, so easy to get as long as n%4==2, then f[n]=0.



HDU 1021 Fibonacci 2, finding the 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.