[Problem description] m and n are known as integers and meet the following two conditions:
① M, n' {1, 2 ,..., K}, that is, 1 <= m, n <= K
(N2-mn-m2) 2 = 1
Your task is to program a positive integer k (1 <= k <= 109) entered by the keyboard, find a group of M, N that meets the preceding two conditions, and the value of M2 + N2 is the largest. For example, if we input K = 1995 from the keyboard, then the output M = 987, n = 1597.
[Problem Analysis]
Typical mathematical problems ...... We can use condition ② to obtain the correct solution by using the root formula plus the limit condition to solve the equation. However, the data range is 109. This method can only bear 105, and this method must time out.
We can perform a mathematical transformation for this formula:
(N2-mn-m2) 2
= (M2 + nm-n2) 2.
= [(N + M) 2-N (n + M)-N2] 2
= [(N') 2-m'n'-(m') 2] 2
N '= m + n, M' = n. Although the expression does not change in form, we can see from the above mathematical transformation formula that if m and n are a group of solutions that meet the conditions, then m' and n' are a group of solutions that meet the conditions, so they can be solved by iterative methods. Therefore, we can sort all M and N that meet the conditions in ascending order.
Make M = 1, n = 1, and find that the conditions are met, that is, a group of partial solutions to the problem.
Then I wrote a small program to find the solution when k = 100:
Uses Math; VaR M, N: integer; Begin For M: = 1 To 100 Do For N: = 1 To 100 Do If (N ** 2 -M * n-M ** 2 )** 2 = 1 Then Writeln (m, ' ' , N ); End .
The output is as follows:
1 11 22 33 55 88 1313 2121 3434 5555 89
Sort them in ascending order ,......, So I found that this is exactly a series of Fibonacci.
So everything becomes simple.
var K, M, N, t: longint; begin readln (k ); m: = 1 ; N: = 1 ; repeat T: = m + N; If T <= k then begin M: = N; N: = T; end ; until T> K; writeln (M, ' ' , n); end ;
It is also a pleasant short code. What is important to this question is not code, but to convert the algebraic form and to think of the Fibonacci series.
Looking at the standard program, it is similar to mine, so I will not give it again.