题意很简单。就是求x^k-1的因式分解。显然x-1必然是其中之一(x=1, x^k-1=0)。
假设k=mp. 则x^k = (x^p)^m, 同理x^p-1必然是其中之一,即x^p的所有因式一定是x^k的所有因式。
思路就是按照上面的方式,先找到k的约束的多项式,然后求得最后一个因式的系数。
求得所有[2,1001]的因式后。
对因式进行重新排序,并按照格式输出。输出时注意系数为1,阶数为0,阶数为1。
1 /* 3205 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 using namespace std; 20 //#pragma comment(linker,"/STACK:102400000,1024000") 21 22 #define mpii map<int,int> 23 #define sti set<int> 24 #define stpii set<pair<int,int> > 25 #define vi vector<int> 26 #define pii pair<int,int> 27 #define vpii vector<pair<int,int> > 28 #define rep(i, a, n) for (int i=a;i<n;++i) 29 #define per(i, a, n) for (int i=n-1;i>=a;--i) 30 #define pb push_back 31 #define mp make_pair 32 #define fir first 33 #define sec second 34 #define all(x) (x).begin(),(x).end() 35 #define SZ(x) ((int)(x).size()) 36 #define lson l, mid, rt<<1 37 #define rson mid+1, r, rt<<1|1 38 39 const int maxn = 1002; 40 set<stpii> ans[maxn]; 41 int n; 42 int a[maxn], b[maxn]; 43 44 typedef struct ele_t { 45 int d, c; 46 ele_t(int d_=0, int c_=0): 47 d(d_), c(c_) {} 48 friend bool operator< (const ele_t& a, const ele_t& b) { 49 // if (a.d == b.d) 50 // return a.c < b.c; 51 // else 52 // return a.d < b.d; 53 return a.d > b.d; 54 } 55 } ele_t; 56 57 typedef struct node_t { 58 vector<ele_t> ele; 59 // int sz; 60 friend bool operator< (const node_t& a, const node_t& b) { 61 bool ret; 62 int c1, c2; 63 int sza = SZ(a.ele); 64 int szb = SZ(b.ele); 65 int mn = min(sza, szb); 66 ret = sza < szb; 67 68 rep(i, 0, mn) { 69 if (a.ele[i].d == b.ele[i].d) { 70 c1 = abs(a.ele[i].c); 71 c2 = abs(b.ele[i].c); 72 if (c1 == c2) { 73 if (a.ele[i].c == b.ele[i].c) 74 continue; 75 ret = a.ele[i].c < b.ele[i].c; 76 } else { 77 ret = c1 < c2; 78 } 79 break; 80 } else { 81 ret = a.ele[i].d < b.ele[i].d; 82 break; 83 } 84 } 85 86 return ret; 87 } 88 } node_t; 89 90 vector<node_t> nd[maxn]; 91 void init(); 92 void resort(); 93 94 int main() { 95 ios::sync_with_stdio(false); 96 #ifndef ONLINE_JUDGE 97 freopen("data.in", "r", stdin); 98 freopen("data.out", "w", stdout); 99 #endif 100 101 int x; 102 int deg, coe; 103 // set<stpii>::iterator iter, eiter; 104 // stpii::iterator siter, esiter; 105 vector<node_t>::iterator iter, eiter; 106 vector<ele_t>::iterator siter, esiter; 107 bool flag; 108 109 init(); 110 while (scanf("%d",&x)!=EOF && x) { 111 iter = nd[x].begin(); 112 eiter = nd[x].end(); 113 while (iter != eiter) { 114 siter = iter->ele.begin(); 115 esiter = iter->ele.end(); 116 flag = true; 117 putchar('('); 118 while (siter != esiter) { 119 deg = siter->d; 120 coe = siter->c; 121 if (flag) { 122 if (deg == 1) { 123 printf("x"); 124 } else { 125 printf("x^%d", deg); 126 } 127 } else { 128 if (deg == 0) { 129 if (coe > 0) { 130 putchar('+'); 131 } else { 132 putchar('-'); 133 coe = -coe; 134 } 135 printf("%d", coe); 136 } else if (deg == 1) { 137 if (coe > 0) { 138 putchar('+'); 139 } else { 140 putchar('-'); 141 coe = -coe; 142 } 143 if (coe == 1) { 144 printf("x"); 145 } else { 146 printf("%dx", coe); 147 } 148 } else { 149 if (coe > 0) { 150 putchar('+'); 151 } else { 152 putchar('-'); 153 coe = -coe; 154 } 155 if (coe == 1) { 156 printf("x^%d", deg); 157 } else { 158 printf("%dx^%d", coe, deg); 159 } 160 } 161 } 162 flag = false; 163 ++siter; 164 } 165 putchar(')'); 166 ++iter; 167 } 168 putchar('\n'); 169 } 170 171 #ifndef ONLINE_JUDGE 172 printf("time = %d.\n", (int)clock()); 173 #endif 174 175 return 0; 176 } 177 178 void init() { 179 stpii st; 180 stpii::iterator it; 181 set<stpii>::iterator iter, eiter; 182 int i, j, k; 183 int v, mx; 184 int deg, coe; 185 186 // x - 1 187 st.insert(mp(1, 1)); 188 st.insert(mp(0, -1)); 189 for (v=2; v<maxn; ++v) 190 ans[v].insert(st); 191 192 for (v=2; v<maxn; ++v) { 193 194 for (i=2; i*i<=v; ++i) { 195 if (v%i == 0) { 196 j = v / i; 197 for (iter=ans[i].begin(); iter!=ans[i].end(); ++iter) 198 ans[v].insert(*iter); 199 if (j != i) { 200 for (iter=ans[j].begin(); iter!=ans[j].end(); ++iter) 201 ans[v].insert(*iter); 202 } 203 } 204 } 205 206 st.clear(); 207 iter = ans[v].begin(); 208 eiter = ans[v].end(); 209 210 // init a with const 1 211 memset(a, 0, sizeof(a)); 212 a[0] = 1; 213 214 // multiply other elements 215 while (iter != eiter) { 216 memset(b, 0, sizeof(b)); 217 for (it=iter->begin(); it!=iter->end(); ++it) { 218 deg = it->fir; 219 coe = it->sec; 220 for (j=0; j<=v; ++j) { 221 b[j+deg] += coe * a[j]; 222 } 223 } 224 memcpy(a, b, sizeof(a)); 225 ++iter; 226 } 227 228 memset(b, 0, sizeof(b)); 229 // find the max deg 230 for (mx=v; mx>=0; --mx) { 231 if (a[mx]) { 232 break; 233 } 234 } 235 assert(mx >= 0); 236 237 for (j=v; j>0;--j) { 238 if (j == v) { 239 coe = 1; 240 deg = j - mx; 241 } else if (j == 0) { 242 coe = -1-b[j]; 243 deg = j - mx; 244 } else { 245 coe = -b[j]; 246 deg = j - mx; 247 } 248 249 if (coe == 0) 250 continue; 251 252 for (k=mx; k>=0; --k) 253 b[k+deg] += a[k] * coe; 254 255 st.insert(mp(deg, coe)); 256 } 257 ans[v].insert(st); 258 } 259 260 resort(); 261 } 262 263 void resort() { 264 stpii::iterator it, eit; 265 set<stpii>::iterator iter, eiter; 266 int v, m; 267 int i, j, k; 268 int deg, coe; 269 ele_t e; 270 node_t node; 271 272 for (v=2; v<maxn; ++v) { 273 iter = ans[v].begin(); 274 eiter = ans[v].end(); 275 while (iter != eiter) { 276 node.ele.clear(); 277 it = iter->begin(); 278 eit = iter->end(); 279 m = 0; 280 while (it != eit) { 281 e.d = it->fir; 282 e.c = it->sec; 283 node.ele.pb(e); 284 ++m; 285 ++it; 286 } 287 sort(all(node.ele)); 288 nd[v].pb(node); 289 ++iter; 290 } 291 sort(all(nd[v])); 292 } 293 }