Street Numbers
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 finds, 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 should exhibit it. 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):
6 8
35 49
Input
There is no input for this program.
Output
Output will consist of 10 lines each containing a pair of numbers, in increasing order with the last number, each printed right justified in a field of width 10 (as shown above).
Sample Input
Sample Output
6 8 35 49
Source
New Zealand 1990 Division I,UVA 138
题目类型:佩尔方程
算法分析:原题要求解的是1 + 2 + 3 +….n - 1 = n + 1 + n + 2 + ….m中的n和m,可以化简得到(2 * m + 1) ^ 2 – 8 * n ^ 2 = 1,令x = 2 * m + 1, y = n,则有x ^ 2- 8 * y ^ 2 = 1的佩尔方程,该方程的最小特解易求,然后使用递推式将前10个解输出即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 1000000 + 66; int main() { int x1 = 3, y1 = 1, xn, yn; for (int i = 1; i <= 10; i++) { xn = 3 * x1 + 8 * y1; yn = x1 + 3 * y1; cout << setw (10) << yn << setw (10) << (xn - 1) / 2 << endl; x1 = xn, y1 = yn; } return 0; } |
- « 上一篇:poj1316
- poj1321:下一篇 »