问题四 拉格朗日第四定律问题
(Input File: lagrange.in; Output: Standard Output)
任何正整数都可以表示为最多不超过4个正整数的平方之和,这是众所周知的拉格朗日的Four-Square定理。这个定理首先由Joseph-Louis Lagrange在1770年证明。你的任务不是解释原初的证明,也不是发现新的证明,而是计数对某个数这个定理成立的表达式有多少种。
对一个给定的正整数n,你要报告将n表示为最多四个正整数平方和的不同表示的种数。注意,表达式中,加法的顺序是无关紧要的,即你必须把32 + 42与42 +32看作是同一种表示。例如,看25,这个数有三种表示:12+22+22+42,32 + 42, 与52,那么你就应该报告3。注意不要重复计数32 + 42与42 +32。
Input
输入最多有255行,每行是一个小于215的正整数,最后一行是一个0,表示输入的结束。
Output
输出也由多行组成,每行是单个整数。输出中没有其他字符出现。
对于每个输入整数n,输出n可表示为最多4个正整数平方之和的表示种数。
Sample Input
1
25
2003
211
20007
0
Output for the Sample Input
1
3
48
7
738
import
java.io.
*
;
class Test4
... {
static int cout=0;
static int total=0;
static int sum=0;
public static void main(String[] args) throws IOException
...{
BufferedReader bf=new BufferedReader(new FileReader("lagrange.in"));
String str;
int n;
while(!(str=bf.readLine()).equals("0"))
...{
n=Integer.parseInt(str);
go(n,1);
System.out.println(cout);
cout=0;
}
}
public static void go(int n,int top)
...{
for(int i=top;i<=215;i++)
...{
if(sum>n)return;
if(sum==n)...{cout++;return;}
if(total==4)return;
sum=sum+i*i;
total++;
go(n,i);
total--;
sum=sum-i*i;
}
}
}
class Test4
... {
static int cout=0;
static int total=0;
static int sum=0;
public static void main(String[] args) throws IOException
...{
BufferedReader bf=new BufferedReader(new FileReader("lagrange.in"));
String str;
int n;
while(!(str=bf.readLine()).equals("0"))
...{
n=Integer.parseInt(str);
go(n,1);
System.out.println(cout);
cout=0;
}
}
public static void go(int n,int top)
...{
for(int i=top;i<=215;i++)
...{
if(sum>n)return;
if(sum==n)...{cout++;return;}
if(total==4)return;
sum=sum+i*i;
total++;
go(n,i);
total--;
sum=sum-i*i;
}
}
}