One of SDUT 2568 fibfibfib

Source: Internet
Author: User

One day in the topic description, Z learned about the Fibonacci series and found that the series had the following rules: F [1] = F [2] = 1; F [N] = F [N-1] + F [N-2]; so Z think it is amazing, want to know whether the number of any two fiber ACCI is greater than 1 of the common number. However, the manual computation is not enough in a limited range, so Z finds you and hopes that you can help compile a program for computation. Task: specify the number of items in the two Fibonacci numbers (that is, the number of each of these two numbers is the number of items), and obtain the maximum common number of these two numbers. For example, input 3 6 and Output 2 (because GCD (F [3], F [6]) = 2 ). Enter multiple groups of data. Each row has two numbers A and B. To the end of the file. 0 <A, B <100007 output A row for each group of data. Ensure that the final result does not exceed 2 ^ 64-1, so the 64-bit unsigned integer is used. Sample input 30 1514 2812 2417 1730 624 2420 1024 1229 18 sample output 299 when I first saw this question, I thought about how to calculate the number of common contracts. Because Fibonacci is in the form of a sum, I want to use a subtraction method to calculate the approximate number. However, I have actually selected it. No, because the number is too large. Then I came up with an enumeration. It is easy to solve this problem by converting it into a large number to calculate the remainder. If it is selected, it will not work, because we need to enumerate more than 60 power from 1 to 2, unless we use binary. Otherwise, it will be terrible, and this question will not be used. I don't know what to do. Finally, I thought about finding a regular number and drew a series of Fibonacci numbers. I found that if a Fibonacci number is n and x, then n and x + x, x + 2 * x, x + 3 * x .... is n. According to this rule, we can make this question [cpp] # include <stdio. h> # include <string. h> # include <math. h> int main () {unsigned long int res1, res2, res, max; int I, j, n, m, s, t, pre; int l, r; while (scanf ("% d", & l, & r )! = EOF) {if (l> r) {t = l; l = r; r = t ;} if (l = 1 | l = 2 | r = 1 | r = 2) {printf ("1 \ n"); continue ;} res1 = 1; res2 = 1; res = 1; if (l = r) {for (I = 3; I <= l; I ++) {res = res1 + res2; res1 = res2; res2 = res;} printf ("% llu \ n", res); continue;} max = 1; pre = 1; for (I = 3; I <= l; I + = pre) {if (l % I = 0) & (r % I = 0 )) {pre = I; max = I ;}} res1 = 1; res2 = 1; res = 1; for (I = 3; I <= max; I ++) {res = res1 + res2; res1 = res2; res2 = res;} printf ("% llu \ n", res);} 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.