zoukankan      html  css  js  c++  java
  • POJ 1320 Street Numbers 解佩尔方程

    Street Numbers
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 2529   Accepted: 1406

    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
    

    Source

     
    题目要求解1+2+3+...+n=(n+1)+....+m.
    要使1+2+3+...+n=(n+1)+....+m.那么n(n+1)/2=(m-n)(n+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-1*x1+d*yn-1*y1
    yn=xn-1*y1+yn-1*x1.

    已知x1=3,y1=1;

    有佩尔方程的迭代公式:x[n]=x[n-1]*x1+d*y[n-1]*y1.   y[n]=y[n-1]*y1+yn-1]*x1

    故有:x[n+1]=3x[n]+8y[n];

               y[n+1]=x[n]+3y[n];

    就可以求出前十组解。
    #include<stdio.h>  
    #include<math.h>  
    int main()  
    {  
        int x1=3,y1=1,d=8,x,y,px=3,py=1;  
        for(int i=1;i<=10;i++)  
        {  
            x=x1*px+d*y1*py;  
            y=y1*px+x1*py;  
            printf("%10d%10d
    ",y,(x-1)/2);  
            px=x;py=y;  
        }  
        return 0;  
    }  

    Pell方程,由费马提出,但后来欧拉误记为佩尔提出,并写入他的著作中。后人多称佩尔方程。

    设d是正整数,且非平方数。
    下面的不定方程称为佩尔(Pell)方程:
    (1)一定有无穷多组正整数解
    这是初等数论中最经典的内容之一。
     
     
     
     
     
  • 相关阅读:
    用python写一个北京市的个税计算器
    排序算法(冒泡、选择)-python代码展示
    封装系统内置功能的函数(字符串)
    福彩习题
    打印等腰三角形
    1.冒泡排序法
    腾讯云:搭建 Node.js 环境
    python开发环境搭建
    基于 Ubuntu 搭建 FTP 文件服务
    TensorFlow — 相关 API
  • 原文地址:https://www.cnblogs.com/kimsimple/p/7342632.html
Copyright © 2011-2022 走看看