Street Numbers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2753   Accepted: 1530

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):

         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

题意是一个计算机程序员住在一个门牌号(从1开始计)连续的街道上,每天晚上她都在这个街道上从头走到尾,有一天晚上她把她走过的门牌号相加起来,下一次她走另一条路还是相加门牌号,令她吃惊的是(我都不知道这有什么好吃惊的),两个和是相等的。让打出表house number,last number

题意的意思翻译过来就是1+2+...+x = x+(x+1)+(x+2)...+y

求x,y。

方程就变为x*(x+1)/2 = (x+y)(y-x+1)/2 => y^2+y-2*x^2=0 => (2*y+1)^2-8*x^2=0

做这道题的收获就是通过这道题了解了佩尔方程,它是一个解x^2-d*y^2=1这类方程的方法。

说起来这个佩尔方程也是逗,是费马老人家提出来的,结果欧拉记错了,写在他的书中了。有意思的是,可能是欧拉的影响力太大了,之后大家还是把费马提出的方法叫佩尔方程了,真替费马感到不值啊(之后再一想想,我有什么资格替费马感到不值。。。人家一个大定理青史留名,我一个程序员还在做POJ的题。。。做得还挺好玩)。

咳咳,反正佩尔方程的意思就是x^2-d*y^2=1的第一个解x0,y0已知的话,其余的值有一个递推公式了:

X(n)=X(n-1)*x0+d*Y(n-1)*y0

Y(n)=X(n-1)*y0+Y(n-1)*x0

知道了这个之后,程序就好写了。以后记住解x^2-d*y^2=1的方程有一个简便算法~

代码:

#include<iostream>
#include<iomanip>
#pragma warning(disable:4996)
using namespace std; int main()
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout); long long i,x0=3,y0=1,last_x=3,last_y=1,x,y;
for(i=1;i<=10;i++)
{
x=last_x*x0+8*last_y*y0;
y=last_x*y0+last_y*x0; cout<<setw(10)<<y<<setw(10)<<(x-1)/2<<endl; last_x=x;
last_y=y;
}
system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1320:Street Numbers的更多相关文章

  1. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  2. POJ 1142:Smith Numbers(分解质因数)

                                   Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...

  3. POJ 1320 Street Numbers 【佩尔方程】

    任意门:http://poj.org/problem?id=1320 Street Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  4. POJ 1320 Street Numbers 解佩尔方程

    传送门 Street Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2529   Accepted: 140 ...

  5. POJ 1320 Street Numbers(佩尔方程)

    Street Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3078   Accepted: 1725 De ...

  6. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  7. POJ 1320 Street Numbers Pell方程

    http://poj.org/problem?id=1320 题意很简单,有序列 1,2,3...(a-1),a,(a+1)...b  要使以a为分界的 前缀和 和 后缀和 相等 求a,b 因为序列很 ...

  8. Street Numbers POJ - 1320(佩尔方程式)

    题意:就是从n到1再从1到n的各个数字之和为sum1, 然后从n到m,再从m到n的各个数字之和为sum2,求,(n,m)的前10组解. 思路: 直接建模,利用等差数列的求和公式计算一个公式(2n+1) ...

  9. POJ 1995:Raising Modulo Numbers 快速幂

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5532   Accepted: ...

随机推荐

  1. redis api-list

  2. Linux设备树学习

    1.概念 设备树用于实现驱动代码与设备信息相分离.驱动代码只负责处理驱动的逻辑而关于设备的具体信息存放到设备树文件中.(dts文件,编译后为dtb文件).一个dts文件对应一个ARM的machine, ...

  3. Linux centosVMware Vim介绍、vim颜色显示和移动光标、vim一般模式下移动光标、vim一般模式下复制、剪切和粘贴

    一.Vim介绍 vim 是一款功能强大的文本编辑器,是vi的升级版,带有颜色显示, 默认有三种模式:一般模式, 命令模式,  编辑模式   安装Vim [root@davery ~]# vim /et ...

  4. 分享Burp Suite遇到的各种坑

    1.性质问题 价格昂贵 专业版高达399美元/每年,免费版有功能限制:https://portswigger.net/buy/pro,构想中的工具应该是免费开源的. 破解版存在安全隐患 https:/ ...

  5. SpringBoot Date类型插入数据库始终比正确时间早一天问题解决办法

    bug描述 昨天的Date插入不进去问题解决后,一直没发现其实插入的时间一直比正确的时间早一天 输出sql语句,发现insert语句还是对的,不知道为什么插入数据库之后结果就早了一天 https:// ...

  6. C# 篇基础知识10——多线程

    1.线程的概念 单核CPU的计算机中,一个时刻只能执行一条指令,操作系统以“时间片轮转”的方式实现多个程序“同时”运行.操作系统以进程(Process)的方式运行应用程序,进程不但包括应用程序的指令流 ...

  7. mysql dump 完全备

    创建表: MariaDB [xuegod]> create database xuegod; MariaDB [xuegod]> use xuegod; MariaDB [xuegod]& ...

  8. 实验吧-杂项-WTF?(python 01代码转图片)

    比较新的题型了吧. code为base64码,转码出来是01代码,直接蒙圈,查阅相关wp才知道是转图片的. 复制到编辑器里可以看到一共65536个数字,开方是256,于是这就是一个方形的图片了–> ...

  9. Hive的原理和基本用法

    一.Hive的概述 1.Hive的定义 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL进行数据读取.写入和管理. 2.Hive的架构图 hive ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:为图片添加圆角 (IE8 不支持)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...