1.题目大意:求使这个方程成立的最小的x的非负整数解,如果不存在这样的解,输出-1.
2。开始看到这个方程,我还想到了费马大定理,想着是不是只要是n可以被表示成某个数的三次方这个方程就是无解的,其他情况是有解的,然后看了一下费马大定理的,发现他说的是正整数,这里要求的是非负整数。所以gg。然后尝试着分解这个方程,这显示是可以做因式分解的嘛。
显然,这是两个整数,然后我们枚举n的因子:a*b=n.构建方程组:
这个方程是可解的,直接消去y得到一个关于x的二元一次方程:
解出x:
首先判断这个数是不是一个整数,判断之后再判断这样的x对应的y是不是个非负数,枚举所有n的因子取个最小即可。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#pragma warning(disable:4996)
int n, cnt;
int a[500];
void init()
{
cnt = 0;
for (int i = 1; i*i <= n; i++)
{
if (n%i == 0)
{
if (i*i != n)
{
a[cnt++] = i;
a[cnt++] = n / i;
}
else
{
a[cnt++] = i;
}
}
}
}
const ll INF = 1e18;
int main()
{
while (scanf("%d", &n)&&n)
{
init();
ll ansx = INF, ansy = INF;
int cn = 0;
for (int i = 0; i < cnt; i++)
{
int tema = a[i];
int temb = n / a[i];
int delt = (int)sqrt(12 * temb - 3 * tema*tema*1.0);
if (delt*delt != (12 * temb - 3 * tema*tema))continue;
if ((3 * tema + delt) % 6 == 0)
{
cn++;
if (ansx > (3 * tema +delt) / 6)
{
ansx = (3 * tema + delt) / 6;
ansy = tema - ansx;
}
}
if ((3 * tema -delt) % 6 == 0)
{
cn++;
if (ansx > (3 * tema-delt) / 6)
{
ansx = (3 * tema - delt) / 6;
ansy = tema - ansx;
}
}
}
if (cn)
{
cout << ansx << " " << ansy << endl;
}
else
{
puts("Impossible");
}
}
}