多项式 计算 模拟 多项式乘法 和 多项式除法就行了,多项式除法可以参考高精度除法
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<iostream>
#include<sstream>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
using namespace std;
typedef long long LL;
const int maxn = 1200;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
VI factor[maxn + 12];
void output(const VI& a) {
int i, sz = a.size();
for (i = sz - 1; i >= 0; --i)
cout << a[i] << " ";
cout << endl;
}
void init() {
int i, j;
for (i = 2; i <= maxn; ++i)
for (j = 2 * i; j <= maxn; j += i)
factor[j].push_back(i);
}
VI multi(const VI& a, const VI& b) {
int n = a.size(), m = b.size(), i, j;
VI res(n + m - 1, 0);
for (i = 0; i < n; ++i)
for (j = 0; j < m; ++j)
res[i + j] += a[i] * b[j];
return res;
}
VI divide(const VI& a, const VI& b) {
int k, t, n = a.size(), m = b.size(), i, j;
VI res(n - m + 1), tmp = a;
for (i = n - 1, j = n - m; i >= m - 1 && j >= 0; --i, --j) {
res[j] = tmp[i] / b[m - 1];
for (t = i, k = m - 1; k >= 0; --k, --t)
tmp[t] = tmp[t] - res[j] * b[k];
}
return res;
}
VI p[maxn + 12];
VI g[maxn + 12];
VVI f[maxn + 12];
int a[] = { -1, 1 };
int b[] = { 1, 1 };
void print(const VI& a, bool flag) {
int i, sz = a.size();
if (flag)
putchar('(');
for (i = sz - 1; i >= 0; --i) {
if (!a[i])
continue;
if (i < sz - 1) {
if (a[i] < 0)
putchar('-');
else
putchar('+');
}
if (abs(a[i]) > 1 || i == 0)
printf("%d", abs(a[i]));
if (i > 0)
putchar('x');
if (i > 1)
printf("^%d", i);
}
if (flag)
putchar(')');
}
bool cmp(const VI& a, const VI& b) {
int n = a.size(), m = b.size();
if (n != m)
return n < m;
for (int i = m - 1; i >= 0; --i) {
if (abs(a[i]) != abs(b[i]))
return abs(a[i]) < abs(b[i]);
if (abs(a[i]) == abs(b[i]) && a[i] != b[i])
return a[i] < b[i];
}
return true;
}
int main() {
init();
int i, j, sz;
p[1] = VI(a, a + 2);
g[1] = p[1];
for (i = 2; i <= maxn; ++i) {
g[i].resize(i + 1);
g[i][0] = -1;
g[i][i] = 1;
}
for (i = 2; i <= maxn; ++i) {
sz = factor[i].size();
VI tmp = p[1];
f[i].push_back(p[1]);
for (j = 0; j < sz; ++j) {
tmp = multi(tmp, p[factor[i][j]]);
f[i].push_back(p[factor[i][j]]);
}
p[i] = divide(g[i], tmp);
f[i].push_back(p[i]);
sort(f[i].begin(), f[i].end(), cmp);
sz = f[i].size();
}
f[1].push_back(p[1]);
int n;
while (~scanf("%d", &n) && n) {
sz = f[n].size();
for (i = 0; i < sz; ++i)
print(f[n][i], sz > 1);
puts("");
}
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务