Problem 1241 : Lagrange's Four-Square Theorem

Problem B: Lagrange's Four-Square Theorem
http://rose.u-aizu.ac.jp/onlinejudge/ProblemSet/description.jsp?id=1241

  • 問題概要

自然数の二乗4つで与えられたnは何通りで表現できますか?順番は問わない。

  • 解法

三つきめてあとひとつは整数の二乗の数か確かめる。
愚直に3つのfor文でかいた。

  • ソース
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<sstream>
#include<cassert>
#include<queue>
#include<stack>

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define ALL(C)     (C).begin(),(C).end()
#define pb         push_back
#define mp         make_pair
#define vint       vector<int>
#define FOR(it,o)  for(__typeof((o).begin()) it=(o).begin(); it!=(o).end(); ++it)
#define lli	    long long int
#define ld	    long double

using namespace std;


main(){

  int n;
  while(cin >> n){
    if( n == 0)break;
    int cnt = 0;
    for(int i=1; i*i <= n; i++){
      int nowi = n - i * i;
      if(nowi == 0){
	cnt++;
	continue;
      }
      for(int j=i; j*j + i*i <= n; j++){
	int nowj = nowi; 
	nowj -= j*j;
	if(nowj == 0){
	  cnt++;
	  continue;
	}
	for(int k=j; i*i + j*j + k*k <= n; k++){
	  int nowk = nowj;
	  nowk  -= k*k;
	  if(nowk == 0){
	    cnt++;
	    continue;
	  }
	  int l = (int)sqrt(nowk);
	  if(l*l == nowk && l >= k){
	    cnt++;
	  }
	}
      }
    }
    cout << cnt << endl;
  }
}