POJ1320 Street Numbers(佩尔方程)
2015-08-08 20:19
288 查看
Street Numbers
Description
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):
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
xn = xn-1x1 + dyn-1y1
yn = xn-1y1 + yn-1x1
那么
xn+1 = 3xn + 8yn
yn+1 = xn + 3yn
递推有了,迭代就可以了。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2784 | Accepted: 1547 |
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
题目大意:
求解两个不相等的正整数n,m(n<m),使得1 + 2 + .....+ n = (n+1) + .... + m.解题思路:
根据等比数列求和公式可得 n(n+1) / 2 = (m- n) (m+n+1) / 2,即(2m + 1)^2 - 8n ^ 2 = 1,令 x = 2m + 1,y = n,有x ^ 2 - 8y^2 = 1,这是典型的佩尔方程,可知x1 = 3,y1 = 1,由迭代公式可得xn = xn-1x1 + dyn-1y1
yn = xn-1y1 + yn-1x1
那么
xn+1 = 3xn + 8yn
yn+1 = xn + 3yn
递推有了,迭代就可以了。
AC代码:
#include<iostream> using namespace std; int main() { int x,y,x1,y1,px,py,d; x1 = 3; y1 = 1; px = 3; py = 1; d = 8; for(int i=1;i<=10;i++) { x = px * x1 + d * py * y1; y = px * y1 + py * x1; printf("%10d%10d\n",y,(x-1)/2); px = x; py = y; } return 0; }
相关文章推荐
- 《计算机程序设计艺术(第一卷)》的问题
- Spark 性能相关参数配置详解-shuffle篇
- java 代理模式
- mysql存储引擎InnoDB插入数据的过程详解
- HDU 1789 Doing Homework again
- [leetcode 234]Implement Queue using Stacks
- 大数据.......
- oracle表连接——处理连接过程中另外一张表没有相关数据不显示问题
- SDUT 2482 二叉排序树
- 数据分析与数据挖掘类的职位必备技能
- 网络信息安全攻防实验室之基础关
- python正则表达式 re (二)compile
- angularJS(1)
- 两主机互ping的一些例子及其解释
- 菜鸟学JAVA——第一个JAVA程序
- PHP 命名空间
- xcode7-beta版本真机调试
- 你不知道的Google Search
- ASP.NET MVC RegisterRoutes浅解
- [学习笔记]JavaScript基础--运动应用