题目大意:
就是现在对于给定的多组数据, 对于每组的n (1 <= n <= 1100) 分解多项式 (x^n - 1)
例如 x^2 - 1 = (x-1)(x+1)
x^3 - 1 = (x-1)(x^2+x+1)
x^4 - 1 = (x-1)(x+1)(x^2+1)
…
输出顺序按照多项式从小到大输出,(多项式大小比较先比较长度, 然后高次项到低次项比较系数的绝对值, 如果绝对值相同则负的较小
大致思路:
首先可以观察规律
用p[i]表示(x^i - 1)分解之后得到的多项式中其独有的一项
那么(x^i - 1)分解之后得到的是P(k1)P(k2)P(k3)…P(kx) * p[i]
其中k1, k2, k3…kx是i的所有约数
例如 p[1] = x - 1, p[2] = x + 1, p[3] = x^2 + x + 1, p[4] = (x^2 + 1),
p[5] = x^4 + x^3 + x^2 + x + 1, p[6] = x^2 + x + 1
而x^6 - 1 = p[1]*p[2]*p[3]*p[6]…
所有的多项式(x^i - 1)都可以分解成这样的形式, 恰好是 i 的所有约数对应的这些独有的多项式的乘积
可以参考这篇论文 :
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.121.3592&rep=rep1&type=pdf
那么知道上面这些之后就可以运处理所有的独有的多项式了(直接模拟多项式除法)
因为所有系数都是整数并且可以保证能整除, 所以除法很容易写
然后对于所有分解出来的式子按照题目给的大小进行排序即可, 复杂度O(n*n*logn)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <ctime>
using namespace std;
typedef long long ll;
const int N = 3e5+5;
struct Node
{
int p[1200] ;
int l ;
Node(){memset(p,0,sizeof(p));l=0;}
Node operator/(Node q)
{
Node ret ;
ret.l = l-q.l;
for(int i = l ; i >= q.l ; i--)
{
int n = p[i]/q.p[q.l];
for(int j = q.l ; j >= 0 ; j--)
p[i-(q.l-j)] -= q.p[j] * n ;
ret.p[i-q.l] = n ;
}
return ret ;
}
void print()
{
if(l == 1)
{
if(p[0] == -1)
printf("(x-1)");
else
printf("(x+1)");
return ;
}
printf("(");
int next = l ;
while(next > 1)
{
if(next == l) printf("x^%d",next );
else
{
if(p[next] > 0)
if(p[next] == 1)
printf("+x^%d",next );
else printf("+%dx^%d",p[next], next );
else if(p[next] < 0)
if(p[next] == -1)
printf("-x^%d",next );
else printf("%dx^%d", p[next],next);
}
next -- ;
}
if(p[1] > 0)
if(p[1] == 1)
printf("+x");
else printf("+%dx",p[1] );
else if(p[1] < 0)
if(p[1] == -1)
printf("-x");
else printf("%dx",p[1] );
if(p[0] > 0) printf("+%d",p[0] );
else if(p[0] < 0) printf("%d",p[0] );
printf(")");
}
}node[1200];
std::vector<int> vec[1200];
bool cmp(int x, int y)
{
if(node[x].l != node[y].l) return node[x].l<node[y].l;
for(int i = node[x].l ; i >= 0 ; i--)
if(abs(node[x].p[i]) != abs(node[y].p[i]))
return abs(node[x].p[i]) < abs(node[y].p[i]);
else if(node[x].p[i] != node[y].p[i])
return node[x].p[i] < 0 ;
}
int main()
{
node[1].p[1] = 1 ;
node[1].p[0] = -1 ;
node[1].l = 1 ;
for(int i = 2 ; i <= 1100 ; i++)
{
node[i].p[i] = 1 ; node[i].p[0] = -1 ;
node[i].l = i ;
node[i] = node[i] / node[1];
vec[i].push_back(1);
for(int j = 2 ; j * j <= i ; j++)
{
if(i % j == 0)
{
node[i] = node[i]/node[j];
vec[i].push_back(j);
if(j * j != i)
{
node[i] = node[i] / node[i/j] ;
vec[i].push_back(i/j);
}
}
}
vec[i].push_back(i);
}
int n ;
while(~scanf("%d",&n) && n)
{
if(n == 1)
{
printf("x-1\n");
continue ;
}
sort(vec[n].begin() , vec[n].end(),cmp);
for(int i = 0 ; i < vec[n].size();i++)
node[vec[n][i]].print();
printf("\n");
}
return 0 ;
}