Description
给定一个n次方程,求n个解
Solution
首先范围不大所以枚举答案。我们知道任意多项式都能因式分解成(x-a)(x-b)(x-c)…的形式,这样每次用找到的解做多项式除法就可以了。还挺好写
Code
#include <cstdio>
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)
#define ll long long
using namespace std;
int a[21], n;
bool check(int x) {
ll w=1;
ll ans=0;
rep(i, 0, n) {
ans += w * a[i];
w *= x;
}
return !ans;
}
void divi(int x) {
printf("%d ",-x);
drp(i, n, 1) {
a[i - 1] -= a[i] * x;
}
rep(i, 0, n - 1) {
a[i] = a[i + 1];
}
n--;
}
int main() {
scanf("%d", &n);
rep(i, 0, n) {
scanf("%d", &a[i]);
}
rep(i, 1, 20) {
while (check(i) && n) {
divi(-i);
}
}
return 0;
}