Time Limit: 3.000 seconds
Limited to 3.000 seconds
Problem
Problem
A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the street. every evening she walks her dog by leaving her house and randomly turning left or right and walking to the end of the street and back. one night she adds up the street numbers of the houses she passes (excluding her own ). the next time she walks the other way she repeats this and find S, to her astonishment, that the two sums are the same. although this is determined in part by her house number and in part by the number of houses in the street, she nevertheless feels that this is a desirable property for her house to have and decides that all her subsequent houses shocould exhibit.
A Program Engineer, the houses on a certain road in her house are all in the same way. One side, the number increases sequentially from 1. She walks out every night, or left or right until the end of the street returns. One day she calculated the sum of house numbers along the street (excluding her house), and the next day she went in the opposite direction and calculated the sum of house numbers, the result shocked her-the sum of the two house numbers was the same. Although the result was determined by the number of houses along the street and the house number of her house, she still felt this was amazing and decided to use it as a necessary condition for future housing selection.
Write a program to find pairs of numbers that satisfy this condition. To start your list the first two pairs are: (house number, last number ):
Write a program to calculate parameters that meet the preceding conditions. The parameter contains two integers: the first is the house number and the second is the last house number.
6 8
35 49
Input and Output
Input and Output
There is no input for this program. output will consist of 10 lines each containing a pair of numbers, each printed right justified in a field of width 10 (as shown above ).
This program is not input. The first 10 integer pairs that meet the preceding conditions are output. Each integer occupies an exclusive row, and each integer is right aligned with a width of 10 characters (as shown in the preceding example ).
Analysis
Analysis
This question can be done using brute force search,AlgorithmThe efficiency is O (N3), but in fact we can use number theory to generate all the solutions. Here we will introduce it in the simplest mathematical language. If the number of all houses is n and the number of houses is K, the N and K must meet the following requirements:
- 1 + 2 +... + (k-1) = (k + 1) + (K + 2) +... + (n) (1)
Use the summation formula Sn = N (A1 + an)/2 to simplify (1:
- (K-1) [1 + (k-1)]/2 = (n-k) [(k + 1) + N]/2, the two sides multiply by 2
- (K-1) [1 + (k-1)] = (n-k) [(k + 1) + N] (2)
To simplify the formula (2) again, you must:
The question is converted to the first 10 positive integers: N and K that meet the minimum (3) formula. First, move the formula on both sides to get:
- (2n + 1) 2-2 (2 k) 2 = 1 (4)
Order:
(5) to the (4) type, you can get:
Uncertainty equation (6) is a typical Pell Equation. to derive the basic concepts required by number theory for solving this equation, we will not go into details. For more information, see Wikipedia: pell Equation. To facilitate program implementation, recursive formulas should be used for solving the problem. The smallest group of positive integers in the Yi Zhi equation (6) is (X0 = 3, Y0 = 2), and the smallest group of solutions given by the question (n = 8, K = 6) the formula (5) is used to obtain the equation (17, 12 ). Empirical calculations show that (we can also find Through iteration of the formula (x0, y0) below), (17, 12) is exactly the second group of positive integer solutions in formula (6. At this point, the initial values (the first two groups (x, y) are calculated, and the following (x, y) can be obtained using the iteration formula:
- XI + 1 = xix0 + nyiy0
- Yi + 1 = xiy0 + yix0 (7)
Use (7) to iterate out the first 10 groups (x, y), and then use the (5) formula for Inverse calculation:
The first 10 groups of N and K can be obtained. In Algorithm Implementation, note that the previous x and y values are retained. If the iteration of X has been completed before the next y value is calculated, the iteration of Y value will be incorrect. In addition, because this question is not input, you can also generate the answer in advance and output the result directly in the program in the format. This is the fastest speed (but a little shameless ~).
Solution
Answer
#include
# include
using namespace STD; int main (void) {// loop 10 times, calculate each group of data. x0 and Y0 are partial solutions, and X and Y are Iterative Solutions for (INT I = 0, X0 = 3, Y0 = 2, x = x0, y = y0, T; I <10; ++ I, x = T) {T = x * x0 + 2 * y * y0; // use t to temporarily Save the calculated xy = x * y0 + y * x0; // calculate y // use X and Y to calculate N and K and output cout