首页 > 专题系列 > Java解POJ > POJ 2042 Lagrange’s Four-Square Theorem [解题报告] Java
2013
11-10

POJ 2042 Lagrange’s Four-Square Theorem [解题报告] Java

Lagrange’s Four-Square Theorem

问题描述 :

The fact that any positive integer has a representation as the sum of at most four positive squares (i.e. squares of positive integers) is known as Lagrange’s Four-Square Theorem. The first published proof of the theorem was given by Joseph-Louis Lagrange in 1770. Your mission however is not to explain the original proof nor to discover a new proof but to show that the theorem holds for some specific numbers by counting how many such possible representations there are.

For a given positive integer n, you should report the number of all representations of n as the sum of at most four positive squares. The order of addition does not matter, e.g. you should consider 4^2 + 3^2 and 3^2 + 4^2 are the same representation.

For example, let’s check the case of 25. This integer has just three representations 1^2+2^2+2^2+4^2, 3^2 + 4^2, and 5^2. Thus you should report 3 in this case. Be careful not to count 4^2 + 3^2 and 3^2 + 4^2 separately.

输入:

The input is composed of at most 255 lines, each containing a single positive integer less than 2^15, followed by a line containing a single zero. The last line is not a part of the input data.

输出:

The output should be composed of lines, each containing a single integer. No other characters should appear in the output.

The output integer corresponding to the input integer n is the number of all representations of n as the sum of at most four positive squares.

样例输入:

1
25
2003
211
20007
0

样例输出:

1
3
48
7
738

解题代码:

/* @author: */
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
 
    int n, s, t1, t2, t;
    while( sc.hasNext() )
    {
       n=sc.nextInt();
	if( n == 0 ) break;
	s = 0;
	for(int i=(int)Math.sqrt( n/4 ); i*i<=n; i++ )
	{
         t1=n-i*i;
	  for(int j=(int)Math.sqrt( t1/3 ); j<=i && j*j<=t1; j++ )
	  {
	    t2=t1-j*j;
	    for(int k=(int)Math.sqrt(t2/2) ; k<=j && k*k<=t2; k++ )
	    {
		t = (int)Math.sqrt( t2 - k*k );
		if( t <= k && t*t == t2 - k*k )
		   s++;
	    }
	  }
       }
	System.out.printf( "%d\n", s );
    }
   }
}

  1. 思路二可以用一个长度为k的队列来实现,入队后判断下队尾元素的next指针是否为空,若为空,则出队指针即为所求。

  2. 换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
    应该是,不可能小于合并后的第K小值吧