Uva 11342 - Three-square
#include<bits/stdc++.h>
#define N 50800
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
struct my
{
LL s, a, b;
};
my ar[N];
int len=0;
void allsquare()
{
int i,j;
for(i=0; i<=224; i++)
{
for(j=i; j<=224; j++)
{
ar[len].a = i;
ar[len].b = j;
ar[len++].s = (i*i) + (j*j);
}
}
//for(i=0;i<100;i++)cout << ar[i].a << " " << ar[i].b << "-> " << ar[i].s << "; ";
}
bool isPerfectSquare(LL n)
{
int qrt = sqrt(n);
if(qrt * qrt == n)
{
return true;
}
else
{
return false;
}
}
void solution(LL k)
{
LL i , df;
int ans[4];
memset(ans, 0 , sizeof(ans));
LL c;
bool f=false;
for(i=0; i<len; i++)
{
df = k - ar[i].s; //cout << ar[i].s << " ";
if(isPerfectSquare(df))
{
f=true;
c = sqrt(df);
ans[0] = ar[i].a; //cout << ar[i].a << " ";
ans[1] = ar[i].b; //cout << ar[i].b << " ";
ans[2] = c;
break;
}
}
if(f)
{
sort(ans, ans+3);
pf("%d %d %d\n",ans[0],ans[1],ans[2]);
}
else
{
pf("-1\n");
}
}
int main()
{
allsquare();
int test;
sf("%d",&test);
while(test--)
{
LL num;
sf("%lld",&num);
solution(num);
}
return 0;
}
#define N 50800
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
struct my
{
LL s, a, b;
};
my ar[N];
int len=0;
void allsquare()
{
int i,j;
for(i=0; i<=224; i++)
{
for(j=i; j<=224; j++)
{
ar[len].a = i;
ar[len].b = j;
ar[len++].s = (i*i) + (j*j);
}
}
//for(i=0;i<100;i++)cout << ar[i].a << " " << ar[i].b << "-> " << ar[i].s << "; ";
}
bool isPerfectSquare(LL n)
{
int qrt = sqrt(n);
if(qrt * qrt == n)
{
return true;
}
else
{
return false;
}
}
void solution(LL k)
{
LL i , df;
int ans[4];
memset(ans, 0 , sizeof(ans));
LL c;
bool f=false;
for(i=0; i<len; i++)
{
df = k - ar[i].s; //cout << ar[i].s << " ";
if(isPerfectSquare(df))
{
f=true;
c = sqrt(df);
ans[0] = ar[i].a; //cout << ar[i].a << " ";
ans[1] = ar[i].b; //cout << ar[i].b << " ";
ans[2] = c;
break;
}
}
if(f)
{
sort(ans, ans+3);
pf("%d %d %d\n",ans[0],ans[1],ans[2]);
}
else
{
pf("-1\n");
}
}
int main()
{
allsquare();
int test;
sf("%d",&test);
while(test--)
{
LL num;
sf("%lld",&num);
solution(num);
}
return 0;
}
Comments
Post a Comment