Description
这里是广袤无垠的宇宙这里是一泻千里的银河
这里是独一无二的太阳系
这里是蔚蓝色的地球
这里,就是这里,是富饶的中国大陆!
这里是神奇的河北大地
这里是美丽的唐山
这里是神话般的唐山一中
这里是Akai曾经的教室
黑板上还留有当年Akai做过的数学作业,其实也并不是什么很困难的题目:
“
给出一个一元n次方程:
a0 + a1x + a 2 x2 +…+ anxn= 0
求此方程的所有有理数解。
” Akai至今还深刻记得当年熬夜奋战求解的时光
他甚至还能记得浪费了多少草稿纸
但是却怎么也想不起来最后的答案是多少了
你能帮助他么?
Input
第一行一个整数n。第二行n+1个整数,分别代表a 0 到a n
Output
第一行输出一个整数t,表示有理数解的个数
接下来t行,每行表示一个解
解以分数的形式输出,要求分子和分母互质,且分母必须是正整数特殊的,如果这个解是一个整数,那么直接把这个数输出
等价的解只需要输出一次
所有解按照从小到大的顺序输出
Sample Input
3
-24 14 29 6
Sample Output
3
-4
-3/2
2/3
HINT
【数据范围】
对于30%的数据,n<=10
对于100%的数据,n <= 100,|a i| <= 2*10^7,an≠ 0
分析
题目中有一点很关键,那就是求有理数解,也即x=q/p的解。由复系数多项式基本定理可以知道,原多项式一定有一个(x-q/p)因式。又考虑到本题所有系数都是整数,原因式等价于(px-q)。也就是说,原方程的任何有理数根都必须对应一个(px-q)这样一个因式。
我们设原多项式为:
f(x) =a0 + a1x + a2x2 +…+ anxn
把所有的有理数解对应的因式分离出来:
f(x)=(p1*x-q1)(p2*x-q2)(p3*x-q3)…(pk*x-qk)*g(x)
其中g(x)是不含有理数根的部分。
由韦达定理我们可以得到:
an = p1*p2*p3*…*pk*u
a0 = q1*q2*q3*…*qk*v
其中u,v分别为g(x)的最高次项系数和常数项。
由上式可以很明显地推出:对于一个有理数解q/p,有p|an,且q|a0。
于是我们先用O(sqrt(a))的时间求出a0和an所有的约数。
然后直接枚举p,q的所有组合,带入原方程检验就行了。
于是又有一个难题来了,如何检验呢??
我们将q/p代入原方程后进行通分,得到下面这个式子:
f(q/p) =[ a0*p^n + a1*q*p^(n-1) + a2*q^2*p*(n-2) + … + an*q^n ] / p^n
分子x= Σai*q^i*p^(n-i)。
我们只需判定x==0的真假,就可以知道f(q/p)是否为0。
用高精度不仅麻烦而且容易超时,于是我们可以采用取模的方法来避免高精度。
我们选用一个较大的质数(我选择的是十亿零七),然后带模从头到尾运算,如果最后答案为0。
我们便可以近似地认为原式为0了(觉得不保险可以多选用几个质数同时检验)。
代码
#include <bits/stdc++.h>
#define N 105
#define M 500005
#define MOD 1000000007
int n;
int tot;
int a[N],p[N],q[N];
int cnt[2];
int fac[2][M];
struct NOTE
{
int x,y;
friend bool operator < (NOTE a, NOTE b)
{
return (double)a.x / (double)a.y < (double)b.x / (double)b.y;
}
}ans[M];
int pow(long long x, int y)
{
long long res = 1;
while (y)
{
if (y & 1)
res = res * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return res;
}
bool cheak(int x,int y)
{
long long ans = 0;
for (int i = 0; i <= n; i++)
(ans += 1LL * a[i] * pow(x,i) % MOD * pow(y,n - i) % MOD) %= MOD;
if (ans)
return 1;
return 0;
}
int gcd(int x,int y)
{
return y == 0 ? x : gcd(y, x % y);
}
int main()
{
scanf("%d",&n);
for (int i = 0; i <= n; i++)
scanf("%d",&a[i]);
int count = 0;
while (a[count] == 0)
count++;
if (count != 0)
{
for (int i = 0, j = count; j <= n; i++, j++)
{
a[i] = a[j];
}
n -= count;
ans[++tot].x = 0;
ans[tot].y = 1;
}
if (n == 0)
{
puts("0");
return 0;
}
int xx = abs(a[0]);
int yy = abs(a[n]);
int x = sqrt(xx);
int y = sqrt(yy);
for (int i = 1; i <= x; i++)
if (xx % i == 0)
{
fac[0][++cnt[0]] = i;
if (i * i <= xx && xx / i != i)
fac[0][++cnt[0]] = xx / i;
}
for (int i = 1; i <= y; i++)
if (yy % i == 0)
{
fac[1][++cnt[1]] = i;
if (i * i <= yy && yy / i != i)
fac[1][++cnt[1]] = yy / i;
}
for (int i = 1; i <= cnt[0]; i++)
for (int j = 1; j <= cnt[1]; j++)
if (gcd(fac[0][i],fac[1][j]) == 1)
{
if (!cheak(fac[0][i],fac[1][j]))
ans[++tot].x = fac[0][i], ans[tot].y = fac[1][j];
if (!cheak(-fac[0][i],fac[1][j]))
ans[++tot].x = -fac[0][i], ans[tot].y = fac[1][j];
}
std::sort(ans + 1, ans + tot + 1);
printf("%d\n",tot);
for (int i = 1; i <= tot; i++)
if (ans[i].y != 1)
printf("%d/%d\n",ans[i].x,ans[i].y);
else printf("%d\n",ans[i].x);
}