hdu 4447

47 篇文章 0 订阅

多项式 计算  模拟 多项式乘法 和 多项式除法就行了,多项式除法可以参考高精度除法


#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值