Problem:
Problem A:
Ruins
In 1936, a dictator Hiedler who aimed at world domination had a deep obsession with
the Lost Ark
. A
person with this ark would gain mystic power according to legend. To break the ambition of the dictator,
ACM (the Alliance of Crusaders against Mazis) entrusted a secret task to an archeologist Indiana Johns.
Indiana stepped into a vast and harsh desert to find the ark.
Indiana finally found an underground treasure house at a ruin in the desert. The treasure house seems
storing the ark. However, the door to the treasure house has a special lock, and it is not easy to open
the door.
To open the door, he should solve a problem raised by two positive integers
a
and
b
inscribed on the door.
The problem requires him to find the minimum sum of squares of differences for all pairs of two integers
adjacent in a sorted sequence that contains four positive integers
a
1
,
a
2
,
b
1
and
b
2
such that
a
=
a
1
a
2
and
b
=
b
1
b
2
. Note that these four integers does not need to be different. For instance, suppose 33 and
40 are inscribed on the door, he would have a sequence 3, 5, 8, 11 as 33 and 40 can be decomposed into
3 × 11 and 5 × 8 respectively. This sequence gives the sum (5 - 3)
2
+ (8 - 5)
2
+ (11 - 8)
2
= 22, which is
the smallest sum among all possible sorted sequences. This example is included as the first data set in
the sample input.
Once Indiana fails to give the correct solution, he will suffer a calamity by a curse. On the other hand,
he needs to solve the problem as soon as possible, since many pawns under Hiedler are also searching
for the ark, and they might find the same treasure house. He will be immediately killed if this situation
happens. So he decided to solve the problem by your computer program.
Your task is to write a program to solve the problem presented above.
Input
The input consists of a series of data sets. Each data set is given by a line that contains two positive
integers not greater than 10,000.
The end of the input is represented by a pair of zeros, which should not be processed.
Output
For each data set, print a single integer that represents the minimum square sum in a line. No extra
space or text should appear.
Sample Input
33 40
57 144
0 0
Output for the Sample Input
22
94

Submitted solutions:

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;

vector<long long int> divisor(long long int n) {
	vector<long long int> res;
	for (long long int i = 1; i * i <= n; ++i) {
		if (n % i == 0) {
			res.push_back(i);
			if (i * i != n) res.push_back(n / i);
		}
	}
	sort(begin(res), end(res));
	return res;
}
int main() {
	while (true) {
		int a,b;cin>>a>>b;
		if(!a)break;
		auto ad(divisor(a));
		auto bd(divisor(b));
		long long int ans=1e18;
		for (auto aa : ad) {
			for (auto bb : bd) {
				vector<long long int>nums{ aa,a / aa,bb,b / bb };
				sort(nums.begin(),nums.end());
				long long int nans=0;
				for(int i=0;i<3;++i)nans+=(nums[i+1]-nums[i])*(nums[i+1]-nums[i]);

				ans=min(ans,nans);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3116
final_rank: 18

code_snippet: #include<bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
#define R(i,a,n) for(int i=a;i>n;i--)
using namespace std ;
int main(){
  int a,b;
  while(cin>>a>>b,a){
    int aa[5001][2],bb[5001][2],c=0,cc=0;
    bool ac[10001],bc[10001];
    r(i,0,10001)ac[i]=0;
    r(i,0,10001)bc[i]=0;
    r(i,1,a+1){
      if(!ac[i]&&a%i==0)R(j,a,0){
	if(i*j==a){
	  aa[c][0]=i;
	  ac[i]=1;ac[j]=1;
	  aa[c++][1]=j;
	  break;
	}
      }
    }
    r(i,1,b+1){
      if(!bc[i]&&b%i==0)R(j,b,0){
	if(i*j==b){
	  bb[cc][0]=i;
	  bc[i]=1;bc[j]=1;
	  bb[cc++][1]=j;
	  break;
	}
      }
    }//cout<<bb[0][0]<<bb[0][1];
    long long l[7];
    long long m=999999999999;
    r(i,0,c){
      r(j,0,cc){
	int t[4];
	t[0]=aa[i][0];t[1]=aa[i][1];
	t[2]=bb[j][0];t[3]=bb[j][1];
	sort(t,t+4);
	l[0]=t[1]-t[0];
	l[1]=t[2]-t[1];
	l[2]=t[3]-t[2];
	m=min(m,l[0]*l[0]+l[1]*l[1]+l[2]*l[2]);
      }
    }
    //if(m==100000000)m=0;
    cout<<m<<endl;
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3172
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
  while(1){
    ll A,B;
    cin>>A>>B;
    if(!A&&!B) break;

    ll sum = 1LL<<55;
    for(int i=1;i*i<=A;i++){
      for(int j=1;j*j<=B;j++){
	if(A%i||B%j) continue;
	vector<int> v(4);
	v[0] = i;
	v[1] = A/i;
	v[2] = j;
	v[3] = B/j;
	sort(v.begin(),v.end());
	ll t = 0;
	for(int i=1;i<4;i++)t+=(v[i]-v[i-1])*(v[i]-v[i-1]);
	sum = min(sum,t);
      }
    }
    cout<<sum<<endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3044
final_rank: 17

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vi a,b;
		for(int i=1;i*i<=n;i++)if(n%i==0)a.pb(i);
		for(int i=1;i*i<=m;i++)if(m%i==0)b.pb(i);
		int mi=inf;
		rep(i,a.size())rep(j,b.size()){
			vi t;
			t.pb(a[i]);t.pb(n/a[i]);t.pb(b[j]);t.pb(m/b[j]);
			sort(all(t));
			int c=t[1]-t[0];
			int d=t[2]-t[1];
			int e=t[3]-t[2];
			mi=min(mi,c*c+d*d+e*e);
		}
		cout<<mi<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1220
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int,int>P;
#define X first
#define Y second
#define pb push_back
vector<P>V[10001];

void V_set(){
	for(int i=1;i<=10000;i++){
		for(int j=1;j*j<=i;j++){
			if(i%j==0){
				V[i].pb(P(j,i/j));
			}
		}
	}
}

int cal(P a,P b){
	int d[4]={a.X,a.Y,b.X,b.Y};
	sort(d,d+4);
	int t=0;
	for(int i=0;i<3;i++){
		t+=(d[i+1]-d[i])*(d[i+1]-d[i]);
	}
	return t;
}

int main(){
	
	V_set();
	
	int a,b;
	while(cin>>a>>b,a){
		int ans=999999999;
		for(int i=0;i<V[a].size();i++){
			for(int j=0;j<V[b].size();j++){
				ans=min(ans,cal(V[a][i],V[b][j]));
			}
		}
		cout<<ans<<endl;
	}
	
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2052
final_rank: 15

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }



vector<pair<long long, long long> > divisor(long long n) {
    vector<pair<long long, long long> > res;
    for (long long i = 1LL; i*i <= n; ++i) {
        if (n%i == 0LL) {
            long long temp = n/i;
            res.push_back(make_pair(i, temp));
            //if (i != temp) res.push_back(make_pair(temp, i));
        }
    }
    return res;
}

long long sq(long long a) { return a*a; }

int a, b;

int main() {
    while (cin >> a >> b) {
        if (a == 0 && b == 0) break;
        
        long long res = 1LL<<60;
        vector<pll> va = divisor(a), vb = divisor(b);
        for (int i = 0; i < va.size(); ++i) {
            for (int j = 0; j < vb.size(); ++j) {
                vll vec(4);
                vec[0] = va[i].first, vec[1] = va[i].second, vec[2] = vb[j].first, vec[3] = vb[j].second;
                sort(ALL(vec));
                
                ll tmp = sq(vec[1] - vec[0]) + sq(vec[2] - vec[1]) + sq(vec[3] - vec[2]);
                chmin(res, tmp);
            }
        }
        
        cout << res << endl;
    }
    
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1212
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
#define p(a,b) (b-a)*(b-a)
#define rep(i,n) for(int i = 0 ; i < n ; i++)

int compute(int a,int b,int c,int d){
  int e[4] = {a,b,c,d};
  int res = (1<<29);

  rep(i,4){
    rep(j,4){
      if(i == j) continue;
      rep(k,4){
	if(i == k || j == k) continue;
	rep(l,4){
	  if(i == l || j == l || k == l) continue;
	  res = min(res,p(e[j],e[i])+p(e[k],e[j])+p(e[l],e[k]));
	}
      }
    }
  }
 
  return res;
}

int main(){
  int a,b;

  while(cin >> a >> b , a | b){
    vector<P> c,d;
    for(int i = 1 ; i*i <= a ; i++){
      if(a % i == 0){
	c.push_back(P(i,a/i));
      }      
    }

    for(int i = 1 ; i*i <= b ; i++){
      if(b % i == 0){
	d.push_back(P(i,b/i));
      }
    }

    int ans = (1<<29);
    rep(i,(int)c.size()){
      rep(j,(int)d.size()){
	ans = min(ans,compute(c[i].first,c[i].second,d[j].first,d[j].second));
      }
    }

    cout << ans << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1204
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
#define p(a,b) (b-a)*(b-a)
#define rep(i,n) for(int i = 0 ; i < n ; i++)

ll compute(int a,int b,int c,int d){
  int e[4] = {a,b,c,d};
  int res = (1<<29);

  rep(i,4){
    rep(j,4){
      if(i == j) continue;
      rep(k,4){
	if(i == k || j == k) continue;
	rep(l,4){
	  if(i == l || j == l || k == l) continue;
	  res = min(res,p(e[j],e[i])+p(e[k],e[j])+p(e[l],e[k]));
	}
      }
    }
  }
 
  return res;
}

int main(){
  int a,b;

  while(cin >> a >> b , a | b){
    vector<P> c,d;
    for(int i = 1 ; i*i <= a ; i++){
      if(a % i == 0){
	c.push_back(P(i,a/i));
      }      
    }

    for(int i = 1 ; i*i <= b ; i++){
      if(b % i == 0){
	d.push_back(P(i,b/i));
      }
    }

    ll ans = (1<<29);
    rep(i,(int)c.size()){
      rep(j,(int)d.size()){
	ans = min(ans,compute(c[i].first,c[i].second,d[j].first,d[j].second));
      }
    }

    cout << ans << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1196
final_rank: 4

code_snippet: #include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<cassert>
#include<ctime>
#include<set>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define all(n) n.begin(),n.end()
using namespace std;
int a,b;
 
int main(){
  
  while(true){
    cin >> a >> b;
    if(a+b == 0)break;
    int ans = (1<<27);
    assert(a>=0 && b>=0);
    for(int i=1;i<=a/2+1;i++){
      if(a%i != 0)continue;
      vector<int> vec;
      //cout << "a = " << i << "," << a/i << " ";
      vec.push_back(i);vec.push_back((int)(a/i));
      for(int j=1;j<=b/2+1;j++){
    if(b%j != 0)continue;
    //cout << "b = " << j << "," << b/j << endl;
    vec.push_back(j); vec.push_back(b/j);
    sort(all(vec));
    //for(int k=0;k<4;k++){
    //cout << vec[k] << endl;
    //}
    //cout << "---" << endl;
    int sum = 0;
    sum = (vec[1]-vec[0])*(vec[1]-vec[0]) + (vec[2]-vec[1])*(vec[2]-vec[1]) + (vec[3]-vec[2])*(vec[3]-vec[2]);
    //cout << "sum -  " << sum << endl;
    ans = min(ans,sum);
    vec.clear();
    vec.push_back(i); vec.push_back(a/i);
      }
    }
 
    cout << ans << endl;
  }
   
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 1216
final_rank: 11

code_snippet: #include<iostream>
#include<algorithm>

using namespace std;

int A,B,ans;
int data[4];

void solve(){
  for(int i = 1; i <= A; i++)
    if(A%i == 0)
      for(int j = 1; j <= B; j++)
	if(B%j == 0){
	  data[0] = i;
	  data[1] = A/i;
	  data[2] = j;
	  data[3] = B/j;
	  sort(data,data+4);


	  int sum = 0;
	  for(int i = 1; i < 4; i++)
	    sum += (data[i]-data[i-1])*(data[i]-data[i-1]);
	  
	  if(ans == -1) ans = sum; 
	  else ans = min(ans,sum);
	  
	}

  cout << ans << endl;
}

int main(){
  while(cin >> A >> B && A+B){
    ans = -1;
    solve();
  }

  return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 1176
final_rank: 13

code_snippet: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>

using namespace std;

map<int, int> MakePair(int);
int Calculator(int, int, int, int);

int main(){
  int n, m, min, _min;
  map<int, int> data_n, data_m;
  map<int, int>::iterator i, j;

  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    data_n = MakePair(n);
    data_m = MakePair(m);

    for(i=data_n.begin(), min=-1; i!=data_n.end(); ++i){
      for(j=data_m.begin(); j!=data_m.end(); ++j){
	_min = Calculator(i->first, i->second, j->first, j->second);
	if(min < 0 || min > _min) min = _min;
      }
    }
    cout << min << endl;
    data_n.clear();
    data_m.clear();
  }
  return 0;
}

map<int, int> MakePair(int n){
  int i, j;
  map<int, int> data;
  if(n == 1){
    data.insert(make_pair(1, 1));
  }else{
    for(i=1, j=n; i<j; ++i){
      if(n % i == 0){
	j = n/i;
	data.insert(make_pair(i, j));
      }
    }
  }
  return data;
}

int Calculator(int a, int b, int c, int d){
  int x, y, z;
  vector<int> data;
  data.push_back(a);
  data.push_back(b);
  data.push_back(c);
  data.push_back(d);
  sort(data.begin(), data.end());
  x = data[1] - data[0];
  y = data[2] - data[1];
  z = data[3] - data[2];
  return x*x + y*y + z*z;
}
accuracy: 1
time_ms: 10
memory_kb: 1220
final_rank: 7

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

#define f first
#define s second

typedef pair<int, int> P;
typedef pair<int, P> P2;

int main(){
  int a, b;
  while(cin >> a >> b){
    if(a == 0 && b == 0) break;
    P2 tmp;
    vector<P> aa;
    for(int i = 1 ; i <= a ; i++){
      if(a % i == 0){
	aa.push_back(make_pair(i, a/i));
      }
    }
    vector<P> bb;
    for(int i = 1 ; i <= b ; i++){
      if(b % i == 0){
	bb.push_back(make_pair(i, b/i));
      }
    }

    int ans = (1<<29);
    int res[4];
    for(int i = 0 ; i < aa.size() ; i++){
      for(int j = 0 ; j < bb.size() ; j++){
	res[0] = aa[i].f, res[1] = aa[i].s;
	res[2] = bb[j].f, res[3] = bb[j].s;
	sort(res, res+4);
	int t = 0;
	for(int i = 0 ; i < 3 ; i++){
	  t += (res[i]-res[i+1]) * (res[i]-res[i+1]);
	}
	ans = min(ans, t);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1216
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>

using namespace std;

int func(vector<int> num){
  int ans=0;
  for(int i=0;i<num.size()-1;i++)
    ans+=(num[i+1]-num[i])*(num[i+1]-num[i]);
  return ans;
}


int main(void){

  int a,b,tmp;
  vector<int>num;

  while(cin >> a >> b,a|b){
    int res=INT_MAX;
    for(int i=1;i*i<=a;i++)
      for(int j=1;j*j<=b;j++)
	if(a%i==0 && b%j==0){
	  num.clear();
	  num.push_back(a/i);
	  num.push_back(i);
	  num.push_back(b/j);
	  num.push_back(j);
	  sort(num.begin(),num.end());
	  res=min(res,func(num));
	}

    cout << res << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1216
final_rank: 9

code_snippet: #include<iostream>
#include<algorithm>

using namespace std;

int A,B,ans;
int data[4];

void solve(){
  for(int i = 1; i <= A; i++)
    if(A%i == 0)
      for(int j = 1; j <= B; j++)
	if(B%j == 0){
	  data[0] = i;
	  data[1] = A/i;
	  data[2] = j;
	  data[3] = B/j;
	  sort(data,data+4);

	  do{
	    int sum = 0;
	    for(int i = 1; i < 4; i++)
	      sum += (data[i]-data[i-1])*(data[i]-data[i-1]);
	    
	    if(ans == -1) ans = sum; 
	    else ans = min(ans,sum);
	  }while(next_permutation(data,data+4));
	}

  cout << ans << endl;
}

int main(){
  while(cin >> A >> B && A+B){
    ans = -1;
    solve();
  }

  return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 1176
final_rank: 14

code_snippet: #include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<cassert>
#include<ctime>
#include<set>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define all(n) n.begin(),n.end()
using namespace std;
int a,b;

int main(){
 
  while(true){
    cin >> a >> b;
    if(a+b == 0)break;
    int ans = (1<<27);
    assert(a>=0 && b>=0);
    for(int i=1;i<=a/2+1;i++){
      if(a%i != 0)continue;
      vector<int> vec;
      //cout << "a = " << i << "," << a/i << " ";
      vec.push_back(i);vec.push_back((int)(a/i));
      for(int j=1;j<=b/2+1;j++){
	if(b%j != 0)continue;
	//cout << "b = " << j << "," << b/j << endl;
	vec.push_back(j); vec.push_back(b/j);
	sort(all(vec));
	//for(int k=0;k<4;k++){
	//cout << vec[k] << endl;
	//}
	//cout << "---" << endl;
	int sum = 0;
	sum = (vec[1]-vec[0])*(vec[1]-vec[0]) + (vec[2]-vec[1])*(vec[2]-vec[1]) + (vec[3]-vec[2])*(vec[3]-vec[2]);
	//cout << "sum -  " << sum << endl;
	ans = min(ans,sum);
	vec.clear();
	vec.push_back(i); vec.push_back(a/i);
      }
    }

    cout << ans << endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 1212
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

struct P{
	int a, b;
	P(int a_, int b_){ a = a_; b = b_; }
	P(){}
};

const int INF = 1e+8;
vector<P> h[10001];

int main(){
	for(int a=1 ; a <= 10000 ; a++ ){
		for(int b=1 ; b <= 10000 && a * b <= 10000 ; b++ ){
			h[a*b].push_back( P(a,b) );
		}
	}
	int x, y;
	while( cin >> x >> y , x || y ){
		int ans = INF;
		for(int i=0 ; i < h[x].size() ; i++ ){
			for(int j=0 ; j < h[y].size() ; j++ ){
				vector<int> v;
				v.push_back( h[x][i].a );
				v.push_back( h[x][i].b );
				v.push_back( h[y][j].a );
				v.push_back( h[y][j].b );
				sort( v.begin() , v.end() );
				int n=0;
				for(int i=1 ; i < v.size() ; i++ ){
					n += (v[i-1] - v[i]) * (v[i-1] - v[i]);
				}
				ans = min( ans , n );
			}
		}
		cout << ans << endl;
	}
}
accuracy: 1
time_ms: 30
memory_kb: 2624
final_rank: 16

code_snippet: #include <iostream>
#include <algorithm>

using namespace std;

const int INF = 1000000000;

main(){
  int a, b;
  while(cin >> a >> b && (a|b)){
    int ans = INF;
    for(int i=1;i<=a;i++){
      if(a%i != 0) continue;
      for(int j=1;j<=b;j++){
        if(b%j != 0) continue;
        int tmp[4] = {i, a/i, j, b/j};
        sort(tmp, tmp+4);
        int sum = 0;
        for(int i=1;i<4;i++){
          sum += (tmp[i] - tmp[i-1]) * (tmp[i] - tmp[i-1]);
        }
        ans = min(ans, sum);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 868
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define REP(i,n) for (int i=0;i<(int)n; ++i)


typedef pair<int,int> pii;
int main() {
  int a, b;
  while(cin >> a>>b,a||b) {
    vector<pii> v1, v2;
    for (int i=1; i*i<=a; ++i) {
      if (a%i == 0) {
        v1.push_back(pii(i, a/i));
      }
    }
    for (int i=1; i*i<=b; ++i) {
      if (b%i == 0) {
        v2.push_back(pii(i, b/i));
      }
    }
    int res = 1<<29;
    REP(i,v1.size()) {
      REP(j, v2.size()) {
        vector<int> tmp;
        tmp.push_back(v1[i].first);
        tmp.push_back(v1[i].second);
        tmp.push_back(v2[j].first);
        tmp.push_back(v2[j].second);
        sort(tmp.begin(), tmp.end());
        int aa = 0;
        REP(k,3)
          aa += (tmp[k+1]-tmp[k]) * (tmp[k+1]-tmp[k]);
        res = min(res, aa);
      }
    }
    cout << res << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 908
final_rank: 1

code_snippet: #include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int main() {
    int a,b;
    while(cin>>a>>b, a|b) {
        set<int> fa,fb;
        for(int i=1; i*i<=a; ++i)
            if(a%i == 0) fa.insert(i), fa.insert(a/i);

        for(int i=1; i*i<=b; ++i)
            if(b%i == 0) fb.insert(i), fb.insert(b/i);

        int ans = 1<<29,t;
        int ar[4];

        typedef set<int>::iterator sit;
        for(sit i = fa.begin(); i != fa.end(); ++i)
            for(sit j = fa.begin(); j != fa.end(); ++j)
                for(sit k = fb.begin(); k != fb.end(); ++k)
                    for(sit l = fb.begin(); l != fb.end(); ++l) {
                        if((*i)*(*j) != a || (*k)*(*l) != b) continue;
                        ar[0] = *i,ar[1] = *j, ar[2] = *k,ar[3] = *l,t = 0;
                        sort(ar, ar+4);
                        for(int m=1; m<4; ++m)
                            t += (ar[m]-ar[m-1])*(ar[m]-ar[m-1]);
                        ans = min(ans, t);
                    }

        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 3340
memory_kb: 912
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int inf = 99999999;

int func(int a[4])
{
	int t=0;
	
	for(int i = 0; i < 3; i++){
		t += ((a[i+1]-a[i])*(a[i+1]-a[i]));
	}
	
	return t;
}

int main()
{
	int n, m;
	
	while(cin>>n>>m && n && m){
		vector<int> vn, vm;
		
		for(int i = 1; i <= n; i++){
			if(n%i == 0){
				vn.push_back(i);
			}
		}
		for(int i = 1; i <= m; i++){
			if(m%i == 0){
				vm.push_back(i);
			}
		}
		int s = inf;
		for(int i = 0; i < vn.size(); i++){
			for(int j = 0; j < vm.size(); j++){
				int a[4];
				a[0] = vn[i];
				a[1] = n/vn[i];
				a[2] = vm[j];
				a[3] = m/vm[j];
				sort(a, a+4);
				s = min(s, func(a));
			}
		}
		
		cout << s << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 908
final_rank: 2