HDU-1316 how many fiber? Big Data and binary (c)

Source: Internet
Author: User

First, let's briefly introduce the meaning of the question. The question must be between the given two numbers a, B (a, B <= 10 ^ 100) [, b] calculate the number of onacci instances. Note the following conventions:

F1: = 1
F2: = 2
FN: = fn-1 + fn-2 (n> = 3)

The basic idea is as follows:

1. considering that there are multiple groups of test data and the Fibonacci number is a special array (each item has a certain relationship with the precursor), the table is used, store all the Fibonacci numbers between 1-10 ^ 100.

2. Enter two strings as the upper and lower bounds.

3. Search for the upper and lower bounds in the Fibonacci array and find the number of Fibonacci numbers in the middle.

The Code is as follows:

# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <math. h> # define M 105 char a [M + 2], B [M + 2]; char book [1000] [M + 2]; int CMP (char * S1, char * S2) {for (INT I = 0; I <= m; ++ I) {if (I = m) {return S1 [I]-S2 [I]; // if the last digit is equal, ensure that 0 is returned;} If (S1 [I] = S2 [I]) continue; else {return S1 [I]-S2 [I] ;}}// two functions are used to locate the upper and lower bounds. // The search principle is to subtract one from the array coordinate of the lower bound, and add an int find1 (int I, char * X) {int low = 0, high = I, mid; // define the Left and Right pointers. The middle pointer while (low <= high) {mid = (low + high)/2; int T = CMP (Book [Mid], X ); if (T> 0) High = mid-1; // change the Left and Right bounds and offset (to make the Left and Right pointers staggered) else if (t = 0) return mid-1; elselow = Mid + 1;} return high; // when jumping out, the high variable is on the left} int find2 (int I, char * X) {int low = 0, high = I, mid; while (low <= high) {mid = (low + high)/2; int T = CMP (Book [Mid], x); If (T> 0) high = mid-1; else if (t = 0) return mid + 1; else low = Mid + 1;} return low;} int main () {int P = m, I = 2; // P is used to mark the highest position book [0] [m] = 1, Book [1] [m] = 2; while (Book [I-1] [M-100] <= 1) {for (Int J = m; j> = P; -- J) book [I] [J] = book [I-1] [J] + book [I-2] [J]; for (Int J = m; j> = P; -- J) {int c = book [I] [J]/10; book [I] [J] % = 10; book [I] [J-1] + = C ;} // real-time carry operation if (Book [I] [P-1]> 0) // determines whether the highest bit has changed -- p; // if the next bit of the current highest bit is not zero, then the pointer minus one + + I;} while (scanf ("% S % s", a, B ), A [0]-'0' | B [0]-'0') {int CNT = 0, P; int last1 = strlen (a)-1; int last2 = strlen (B)-1; for (Int J = last1, K = m; j> = 0; -- J, -- K) {A [k] = A [J]-'0'; A [J] = 0; // eliminate interference comparison factors, set zero operation} For (Int J = last2, K = m; j> = 0; -- J, -- K) {B [k] = B [J]-'0'; B [J] = 0;} int L = find1 (I-1, a); int r = find2 (I-1, b); printf ("% d \ n", r-l-1); memset (A, 0, sizeof (a); // clear the data legacy memset for the last operation (B, 0, sizeof (B);} return 0 ;}
Related Article

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.