Annotation of OpenXM_contrib2/asir2000/engine/H.c, Revision 1.5
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.5 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/H.c,v 1.4 2001/07/03 01:41:25 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "inline.h"
52: #include "base.h"
53: #include <math.h>
54:
55: void modfctrp(P,int,int,DCP *);
56: void gensqfrum(int,UM,struct oDUM *);
57: void srchum(int,UM,UM,UM);
58: UM *resberle(int,UM,UM *);
59: int substum(int,UM,int);
60: void ddd(int,UM,UM *);
61: void canzas(int,UM,int,UM *,UM *);
62: int divum(int,UM,UM,UM);
63: void randum(int,int,UM);
64: void pwrmodum(int,UM,int,UM,UM);
65: void spwrum(int,UM,UM *,UM,N,UM);
66: void spwrum0(int,UM,UM,N,UM);
67: void mulum(int,UM,UM,UM);
68: void mulsum(int,UM,int,UM);
69: void gcdum(int,UM,UM,UM);
70: void mult_mod_tab(UM,int,UM *,UM,int);
71: void make_qmat(UM,int,UM *,int ***);
72: void null_mod(int **,int,int,int *);
73: void null_to_sol(int **,int *,int,int,UM *);
74: void newddd(int,UM,UM *);
75: int irred_check(UM,int);
76: int berlekamp(UM,int,int,UM *,UM *);
77: int nfctr_mod(UM,int);
78: void minipoly_mod(int,UM,UM,UM);
79: int find_root(int,UM,int *);
80: void showum(UM);
81: void showumat(int **,int);
1.4 noro 82:
83: void henmain2(LUM,UM,UM,UM,UM,int,int,LUM *);
84: void addtolum(int,int,LUM,LUM);
85: void clearlum(int,int,LUM);
86: void henprep2(int,int,int,UM,UM,UM,UM,UM,UM,UM);
87: void henprep(P,ML,ML,ML *,ML *);
88:
1.1 noro 89: #if 1
90: #define Mulum mulum
91: #define Divum divum
92: #define Mulsum mulsum
93: #define Gcdum gcdum
94: #endif
95:
96: #define FCTR 0
97: #define SQFR 1
98: #define DDD 2
99: #define NEWDDD 3
100:
101: LUM LUMALLOC();
102:
103: struct p_pair {
104: UM p0;
105: UM p1;
106: struct p_pair *next;
107: };
108:
109: void lnf_mod(int,int,UM,UM,struct p_pair *,UM,UM);
110:
111: void berle(index,count,f,listp)
112: int index,count;
113: P f;
114: ML *listp;
115: {
116: UM wf,wf1,wf2,wfs,gcd;
117: ML flist;
118: int fn,fn1,fm,m,n,fhd;
119: register int i,j,inv,hd,*ptr,*ptr1;
120:
121: n = UDEG(f);
122: wf = W_UMALLOC(n); wf1 = W_UMALLOC(n); wf2 = W_UMALLOC(n);
123: wfs = W_UMALLOC(n); gcd = W_UMALLOC(n);
124: for ( j = 0, fn = n + 1; (j < count) && (fn > 1); ) {
125: m = sprime[index++];
126: if ( !rem(NM((Q)UCOEF(f)),m) )
127: continue;
128: ptoum(m,f,wf); cpyum(wf,wf1);
129: diffum(m,wf1,wf2); gcdum(m,wf1,wf2,gcd);
130: if ( DEG(gcd) > 0 )
131: continue;
132: hd = COEF(wf)[n]; inv = invm(hd,m);
133: for ( i = n, ptr = COEF(wf); i >= 0; i-- )
134: ptr[i] = ( ptr[i] * inv ) % m;
135: fn1 = berlecnt(m,wf);
136: if ( fn1 < fn ) {
137: fn = fn1; fm = m; fhd = hd;
138: for ( i = n, ptr = COEF(wf), ptr1 = COEF(wfs); i >= 0; i-- )
139: ptr1[i] = ptr[i];
140: }
141: j++;
142: }
143: DEG(wfs) = n;
144: *listp = flist = MLALLOC(fn); flist->n = fn; flist->mod = fm;
145: /* berlemain(fm,wfs,(UM *)flist->c); */
146: if ( fm == 2 )
147: berlemain(fm,wfs,(UM *)flist->c);
148: else
149: newddd(fm,wfs,(UM *)flist->c);
150: for ( i = DEG((UM)(flist->c[0])),
151: ptr = COEF((UM)(flist->c[0])),
152: hd = fhd, m = fm; i >= 0; i-- )
153: ptr[i] = ( ptr[i] * hd ) % m;
154: }
155:
156: int berlecnt(mod,f)
157: register int mod;
158: UM f;
159: {
160: register int i,j,**c;
161: int d,dr,n;
162: UM w,q;
163: int **almat();
164:
165: n = DEG(f); c = almat(n,n);
166: w = W_UMALLOC(mod + n); q = W_UMALLOC(mod + n);
167: for ( i = 1; ( d = ( mod * i ) ) < n; i++ )
168: c[d][i - 1] = 1;
169: DEG(w) = d; COEF(w)[d] = 1;
170: for ( j = d - 1; j >= 0; j-- )
171: COEF(w)[j] = 0;
172: for ( ; ( i < n ) && ( ( dr = divum(mod,w,f,q) ) != -1 ); i++ ) {
173: for ( j = dr; j >= 0; j-- )
174: COEF(w)[j + mod] = c[j][i - 1] = COEF(w)[j];
175: for ( j = mod - 1; j >= 0; j-- )
176: COEF(w)[j] = 0;
177: DEG(w) = dr + mod;
178: }
179: for ( i = 1; i < n; i++ )
180: c[i][i - 1] = ( c[i][i - 1] + mod - 1 ) % mod;
181: return berlecntmain(mod,n,n-1,c);
182: }
183:
184: /* XXX berlecntmain should not be used for large mod */
185:
186: int berlecntmain(mod,n,m,c)
187: register int mod;
188: int n,m;
189: register int **c;
190: {
191: register int *p1,*p2,i,j,k,l,a;
192: int *tmp,inv;
193: int cfs;
194:
195: for ( cfs = 1, j = k = 0; j < m; j++ ) {
196: for ( i = k; ( n > i ) && ( c[i][j] == 0 ); i++ );
197: if ( i == n ) {
198: cfs++; continue;
199: }
200: if ( i != k ) {
201: tmp = c[i]; c[i] = c[k]; c[k] = tmp;
202: }
203: p1 = c[k]; inv = invm((p1[j] + mod) % mod,mod);
204: for ( l = j; l < m; l++ )
205: p1[l] = ( p1[l] * inv ) % mod;
206: for ( i = k + 1; i < n; c[i][j] = 0, i++ )
207: if ( i != k && ( a = -c[i][j] ) )
208: for ( l = j + 1, p2 = c[i]; l < m; l++ )
209: p2[l] = (a*p1[l] + p2[l]) % mod;
210: k++;
211: }
212: return ( cfs );
213: }
214:
215: UM *berlemain(mod,f,fp)
216: register int mod;
217: UM f;
218: UM *fp;
219: {
220: UM wg,ws,wf,f0,gcd,q;
221: int n;
222: register int i;
223:
224: n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
225: if ( DEG(wg) <= 0 ) {
226: f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
227: return ( fp );
228: }
229: f0 = W_UMALLOC(n); cpyum(f,f0);
230: ws = W_UMALLOC(n); wf = W_UMALLOC(n);
231: q = W_UMALLOC(n); gcd = W_UMALLOC(n);
232: for ( i = 0; i < mod; i++ ) {
233: cpyum(f0,wf); cpyum(wg,ws);
234: COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
235: gcdum(mod,wf,ws,gcd);
236: if ( DEG(gcd) > 0 ) {
237: if ( DEG(gcd) < n ) {
238: divum(mod,f0,gcd,q); f0 = q; fp = berlemain(mod,gcd,fp);
239: }
240: break;
241: }
242: }
243: fp = berlemain(mod,f0,fp);
244: return ( fp );
245: }
246:
247: void hensel(index,count,f,listp)
248: int index,count;
249: P f;
250: ML *listp;
251: {
252: register int i,j;
253: int q,n,bound;
254: int *p;
255: int **pp;
256: ML blist,clist,bqlist,cqlist,rlist;
257: UM *b;
258: LUM fl,tl;
259: LUM *l;
260:
261: if ( UDEG(f) == 1 ) {
262: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
263: return;
264: }
265: berle(index,count,f,&blist);
266: if ( blist->n == 1 ) {
267: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
268: return;
269: }
270: gcdgen(f,blist,&clist); henprep(f,blist,clist,&bqlist,&cqlist);
271: n = bqlist->n; q = bqlist->mod;
272: bqlist->bound = cqlist->bound = bound = mignotte(q,f);
273: if ( bound == 1 ) {
274: *listp = rlist = MLALLOC(n);
275: rlist->n = n; rlist->mod = q; rlist->bound = bound;
276: for ( i = 0, b = (UM *)bqlist->c, l = (LUM *)rlist->c; i < n; i++ ) {
277: tl = LUMALLOC(DEG(b[i]),1); l[i] = tl; p = COEF(b[i]);
278: for ( j = 0, pp = COEF(tl); j <= DEG(tl); j++ )
279: pp[j][0] = p[j];
280: }
281: } else {
282: W_LUMALLOC((int)UDEG(f),bound,fl);
283: ptolum(q,bound,f,fl); henmain(fl,bqlist,cqlist,listp);
284: }
285: }
286:
1.4 noro 287: void hensel2(index,count,f,listp)
288: int index,count;
289: P f;
290: ML *listp;
291: {
292: register int i,j;
293: int mod,q,n,bound,dx;
294: int *p;
295: ML blist,clist,bqlist,cqlist,rlist;
296: UM fm,qfm,gm,qgm,hm,qhm,qam,qbm,w;
297: UM *b;
298: LUM fl,tl;
299: LUM *l;
300: int dr,k;
301:
302: dx = UDEG(f);
303: if ( dx == 1 ) {
304: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
305: return;
306: }
307: berle(index,count,f,&blist);
308: n = blist->n;
309: mod = blist->mod;
310:
311: if ( n == 1 ) {
312: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
313: return;
314: }
315:
316: /* find k s.t. mod^k <= 2^27 < mod^(k+1); set q = mod^k */
317: for ( k = 1, q = mod; q <= ((1<<27)/mod); q *= mod, k++ );
318:
319: /* mignotte bound */
320: bound = mignotte(q,f);
321:
322: *listp = rlist = MLALLOC(n);
323: rlist->n = n;
324: rlist->mod = q;
325: rlist->bound = bound;
326:
327: if ( bound == 1 ) {
328: gcdgen(f,blist,&clist);
329: henprep(f,blist,clist,&bqlist,&cqlist);
330:
331: for ( i = 0, b = (UM *)bqlist->c; i < n; i++ ) {
332: COEF(rlist)[i] = tl = LUMALLOC(DEG(b[i]),1);
333: for ( j = 0; j <= DEG(tl); j++ )
334: COEF(tl)[j][0] = COEF(b[i])[j];
335: COEF(rlist)[i] = tl;
336: }
337: } else {
338: /* fl = f mod q */
339: fl = LUMALLOC(dx,bound);
340: ptolum(q,bound,f,fl);
341: /* fm = f mod mod */
342: fm = W_UMALLOC(dx);
343: ptoum(mod,f,fm);
344: /* fm = f mod q */
345: qfm = W_UMALLOC(dx);
346: ptoum(q,f,qfm);
347:
348: gm = W_UMALLOC(dx); qgm = W_UMALLOC(dx);
349: hm = W_UMALLOC(dx); qhm = W_UMALLOC(dx);
350: qam = W_UMALLOC(dx); qbm = W_UMALLOC(dx);
351: w = W_UMALLOC(dx);
352: for ( i = 0; i < n-1; i++ ) {
353: cpyum(COEF(blist)[i],gm);
354: cpyum(fm,w);
355: divum(mod,w,gm,hm);
356:
357: /* find am,bm s.t. qam*qgm+qbm*qhm=1 mod q, qgm=gm mod mod, qhm=hm mod mod */
358: henprep2(mod,q,k,qfm,gm,hm,qgm,qhm,qam,qbm);
359:
360: henmain2(fl,qgm,qhm,qam,qbm,q,bound,&tl);
361: rlist->c[i] = (pointer)tl;
362: cpyum(hm,fm);
363: cpyum(qhm,qfm);
364: }
365: rlist->c[i] = fl;
366: }
367: }
368:
369: /*
370: f = g0*h0 mod m -> f = gk*hk mod m^(bound), f is replaced by hk
371: */
372:
373: void henmain2(f,g0,h0,a0,b0,m,bound,gp)
374: LUM f;
375: UM g0,h0,a0,b0;
376: int m,bound;
377: LUM *gp;
378: {
379: int n,dg,dh,i,k,j,dg1,dh1;
380: UM wu,wr,ws,wt,q,wh1,wg1,wc,wd,we,wz,w1,w2;
381: LUM wb0,wb1,wb2,fk,gk,hk;
382:
383: n = DEG(f); dg = DEG(g0); dh = DEG(h0);
384:
385: W_LUMALLOC(n,bound,wb0);
386: W_LUMALLOC(n,bound,wb1);
387: W_LUMALLOC(n,bound,wb2);
388:
389: wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n);
390: wr = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
391: q = W_UMALLOC(2*n);
392: wh1 = W_UMALLOC(2*n); wg1 = W_UMALLOC(2*n);
393:
394: /* gk = g0 */
395: gk = LUMALLOC(n,bound);
396: DEG(gk) = dg;
397: for ( i = 0; i <= dg; i++ )
398: COEF(gk)[i][0] = COEF(g0)[i];
399:
400: /* hk = h0 */
401: W_LUMALLOC(n,bound,hk);
402: DEG(hk) = dh;
403: for ( i = 0; i <= dh; i++ )
404: COEF(hk)[i][0] = COEF(h0)[i];
405:
406: /* fk = gk*hk */
407: W_LUMALLOC(n,bound,fk);
408: mullum(m,bound,gk,hk,fk);
409:
410: wc = W_UMALLOC(2*n); wd = W_UMALLOC(2*n);
411: we = W_UMALLOC(2*n); wz = W_UMALLOC(2*n);
412:
413: #if 0
414: mulum(m,a0,g0,wc);
415: mulum(m,b0,h0,wd);
416: addum(m,wc,wd,wz);
417: if ( DEG(wz) != 0 || COEF(wz)[0] != 1 )
418: error("henmain2 : cannot happen(extgcd)");
419: #endif
420:
1.5 ! noro 421: #if 1
1.4 noro 422: fprintf(stderr,"bound=%d\n",bound);
1.5 ! noro 423: #endif
1.4 noro 424: for ( k = 1; k < bound; k++ ) {
1.5 ! noro 425: #if 1
1.4 noro 426: fprintf(stderr,".");
1.5 ! noro 427: #endif
1.4 noro 428: /* at this point, f = gk*hk mod y^k */
429:
430: #if 0
431: for ( j = 0; j < k; j++ )
432: for ( i = 0; i <= n; i++ )
433: if ( COEF(f)[i][j] != COEF(f)[i][j] )
434: error("henmain2 : cannot happen(f=fk)");
435: #endif
436:
437: /* wt = (f-gk*hk)/y^k */
438: for ( i = 0; i <= n; i++ )
439: COEF(ws)[i] = COEF(f)[i][k];
440: degum(ws,n);
441: for ( i = 0; i <= n; i++ )
442: COEF(wu)[i] = COEF(fk)[i][k];
443: degum(wu,n);
444: subum(m,ws,wu,wt);
445:
446: /* compute wf1,wg1 s.t. wh1*g0+wg1*h0 = wt */
447: mulum(m,a0,wt,wh1); DEG(wh1) = divum(m,wh1,h0,q);
448: mulum(m,wh1,g0,wc); subum(m,wt,wc,wd); DEG(wd) = divum(m,wd,h0,wg1);
449:
450: /* check */
451: #if 0
452: if ( DEG(wd) >= 0 || DEG(wg1) > dg )
453: error("henmain2 : cannot happen(adj)");
454:
455: mulum(m,wg1,h0,wc); mulum(m,wh1,g0,wd); addum(m,wc,wd,we);
456: subum(m,we,wt,wz);
457: if ( DEG(wz) >= 0 )
458: error("henmain2 : cannot happen(coef)");
459: #endif
460:
461: /* fk += ((wg1*hk+wh1*gk)*y^k+wg1*wh1*y^(2*k) mod m^bound */
462:
463: /* wb0 = wh1*y^k */
464: clearlum(n,bound,wb0);
465: DEG(wb0) = dh1 = DEG(wh1);
466: for ( i = 0; i <= dh1; i++ )
467: COEF(wb0)[i][k] = COEF(wh1)[i];
468:
469: /* wb2 = gk*wb0 mod y^bound */
470: clearlum(n,bound,wb2);
471: mullum(m,bound,gk,wb0,wb2);
472:
473: /* fk += wb2 */
474: addtolum(m,bound,wb2,fk);
475:
476: /* wb1 = wg1*y^k */
477: clearlum(n,bound,wb1);
478: DEG(wb1) = dg1 = DEG(wg1);
479: for ( i = 0; i <= n; i++ )
480: COEF(wb1)[i][k] = COEF(wg1)[i];
481:
482: /* wb2 = hk*wb1 mod y^bound */
483: clearlum(n,bound,wb2);
484: mullum(m,bound,hk,wb1,wb2);
485:
486: /* fk += wb2 */
487: addtolum(m,bound,wb2,fk);
488:
489: /* fk += wg1*wh1*y^(2*k) mod y^bound) */
490: if ( 2*k < bound ) {
491: clearlum(n,bound,wb2);
492: mullum(m,bound,wb0,wb1,wb2);
493: addtolum(m,bound,wb2,fk);
494: }
495:
496: /* gk += wg1*y^k, hk += wh1*y^k */
497: for ( i = 0; i <= DEG(wg1); i++ )
498: COEF(gk)[i][k] = COEF(wg1)[i];
499: for ( i = 0; i <= DEG(wh1); i++ )
500: COEF(hk)[i][k] = COEF(wh1)[i];
501: }
1.5 ! noro 502: #if 1
1.4 noro 503: fprintf(stderr,"\n");
1.5 ! noro 504: #endif
1.4 noro 505: *gp = gk;
506: clearlum(n,bound,f);
507: DEG(f) = dh;
508: for ( i = 0; i <= dh; i++ )
509: for ( j = 0; j < bound; j++ )
510: COEF(f)[i][j] = COEF(hk)[i][j];
511: }
512:
513: void clearlum(n,bound,f)
514: int n,bound;
515: LUM f;
516: {
517: int i;
518:
519: for ( i = 0; i <= n; i++ )
520: bzero(COEF(f)[i],bound*sizeof(int));
521: }
522:
523: /* g += f */
524:
525: void addtolum(m,bound,f,g)
526: int m,bound;
527: LUM f;
528: LUM g;
529: {
530: int n,i;
531:
532: n = DEG(f);
533: for ( i = 0; i <= n; i++ )
534: addpadic(m,bound,COEF(f)[i],COEF(g)[i]);
535: }
536:
1.1 noro 537: void hsq(index,count,f,nindex,dcp)
538: int index,count,*nindex;
539: P f;
540: DCP *dcp;
541: {
542: register int i,j,k;
543: register int **pp,**fpp;
544: register int *px,*py;
545: int **wpp;
546: int n,dr,tmp,m,b,e,np,dt;
547: LUM fpa,wb0,wb1,lcpa,tpa,tlum;
548: struct oDUM *dct;
549: UM wt,wq0,wq,wr,wm,wm0,wa,ws,wb;
550: LUM *llist,*ll;
551: UM *dlist,*l,*c;
552: ML list,fp,cfp;
553: DCP dc;
554:
555: sqfrum(index,count,f,nindex,&dct,&fp);
556: np = fp->n; m = fp->mod;
557: if ( ( np == 1 ) && ( dct[0].n == 1 ) ) {
558: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0; *dcp = dc;
559: return;
560: }
561: for ( i = 0, dt = 0; i < np; i++ )
562: dt = MAX(DEG(dct[i].f),dt);
563: b = mig(m,dt,f); fp->bound = b;
564: if ( np == 1 ) {
565: nthrootchk(f,dct,fp,dcp);
566: return;
567: }
568: list = W_MLALLOC(np); list->n = np; list->mod = m; list->bound = 1;
569: for ( i = 0, ll = (LUM *)list->c; i < np; i++ ) {
570: W_LUMALLOC(DEG(dct[i].f),b,ll[i]);
571: for ( j = 0, px = COEF(dct[i].f), pp = COEF(ll[i]);
572: j <= DEG(ll[i]); j++ )
573: pp[j][0] = px[j];
574: }
575: dtestsql(f,list,dct,&dc);
576: if ( dc ) {
577: *dcp = dc;
578: return;
579: }
580: n = UDEG(f);
581: W_LUMALLOC(n,b,fpa); W_LUMALLOC(0,b,lcpa);
582: W_LUMALLOC(n,b,wb0); W_LUMALLOC(n,b,wb1);
583: W_LUMALLOC(n,b,tpa);
584: wt = W_UMALLOC(n); ws = W_UMALLOC(n);
585: wr = W_UMALLOC(n);
586: wq = W_UMALLOC(2*n); wq0 = W_UMALLOC(n);
587: wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
588: wa = W_UMALLOC(2*n);
589: ptolum(m,b,f,fpa); DEG(lcpa) = 0;
590: for ( i = 0, pp = COEF(lcpa), fpp = COEF(fpa); i < b; i++ )
591: pp[0][i] = fpp[n][i];
592: gcdgen(f,fp,&cfp);
593: llist = (LUM *) ALLOCA(np*sizeof(LUM));
594: dlist = (UM *) ALLOCA(np*sizeof(UM));
595: l = (UM *)fp->c; c = (UM *)cfp->c;
596: for ( i = 0; i < np; i++ ) {
597: W_LUMALLOC(DEG(l[i]),b,llist[i]);
598: for ( j = DEG(l[i]), pp = COEF(llist[i]), px = COEF(l[i]); j >= 0; j-- )
599: pp[j][0] = px[j];
600: if ( ( e = dct[i].n ) != 1 ) {
601: wb = dct[i].f;
602: dlist[i] = W_UMALLOC(DEG(wb)*e); cpyum(l[i],dlist[i]);
603: divum(m,dlist[i],wb,wq); DEG(dlist[i])= DEG(wq);
604: for ( k = 0; k <= DEG(wq); k++ )
605: COEF(dlist[i])[k] = dmb(m,COEF(wq)[k],e,&tmp);
606: }
607: }
608: for ( i = 1; i < b; i++ ) {
609: mullum(m,i+1,lcpa,llist[0],wb0);
610: for ( j = 1; j < np; j++ ) {
611: mullum(m,i+1,llist[j],wb0,wb1);
612: tlum = wb0; wb0 = wb1; wb1 = tlum;
613: }
614: for ( j = n, px = COEF(wt), pp = COEF(fpa), wpp = COEF(wb0);
615: j >= 0; j-- )
616: px[j] = ( pp[j][i] - wpp[j][i] + m ) % m;
617: degum(wt,n);
618: for ( j = n, px = COEF(wq0); j >= 0; j-- )
619: px[j] = 0;
620: for ( j = 1; j < np; j++ ) {
621: mulum(m,wt,c[j],wm); dr = divum(m,wm,l[j],wq);
622: for ( k = DEG(wq), px = COEF(wq0), py = COEF(wq); k >= 0; k-- )
623: px[k] = ( px[k] + py[k] ) % m;
624: for ( k = dr, pp = COEF(llist[j]), px = COEF(wm); k >= 0; k-- )
625: pp[k][i] = px[k];
626: }
627: degum(wq0,n); mulum(m,wq0,l[0],wm);
628: mulum(m,wt,c[0],wm0); addum(m,wm,wm0,wa);
629: for ( j = DEG(wa), pp = COEF(llist[0]), px = COEF(wa); j >= 0; j-- )
630: pp[j][i] = px[j];
631: for ( j = n, px = COEF(wq0); j >= 0; j-- )
632: px[j] = 0;
633: for ( j = 0; j < np; j++ )
634: if ( dct[j].n == 1 )
635: for ( k = 0,
636: pp = COEF(llist[j]),
637: wpp = COEF(((LUM *)list->c)[j]);
638: k <= DEG(llist[j]); k++ )
639: wpp[k][i] = pp[k][i];
640: else {
641: pwrlum(m,i+1,((LUM *)list->c)[j],dct[j].n,tpa);
642: for ( k = 0,
643: pp = COEF(llist[j]),
644: wpp = COEF(tpa);
645: k <= DEG(l[j]); k++ )
646: COEF(wt)[k] = (pp[k][i]-wpp[k][i]+m)%m;
647: degum(wt,DEG(l[j])); dr = divum(m,wt,dlist[j],ws);
648: if ( dr >= 0 ) {
649: *dcp = 0;
650: return;
651: } else
652: for ( k = 0,
653: pp = COEF(((LUM *)list->c)[j]);
654: k <= DEG(ws); k++ )
655: pp[k][i] = COEF(ws)[k];
656: }
657: list->bound = i+1; dtestsql(f,list,dct,&dc);
658: if ( dc ) {
659: *dcp = dc;
660: return;
661: }
662: }
663: *dcp = 0;
664: }
665:
666: void gcdgen(f,blist,clistp)
667: P f;
668: ML blist,*clistp;
669: {
670: register int i;
671: int n,d,mod,np;
672: UM wf,wm,wx,wy,wu,wv,wa,wb,wg,q,tum;
673: UM *in,*out;
674: ML clist;
675:
676: n = UDEG(f); mod = blist->mod; np = blist->n;
677: d = 2*n;
678: q = W_UMALLOC(d); wf = W_UMALLOC(d);
679: wm = W_UMALLOC(d); wx = W_UMALLOC(d);
680: wy = W_UMALLOC(d); wu = W_UMALLOC(d);
681: wv = W_UMALLOC(d); wg = W_UMALLOC(d);
682: wa = W_UMALLOC(d); wb = W_UMALLOC(d);
683: ptoum(mod,f,wf); DEG(wg) = 0; COEF(wg)[0] = 1;
684: *clistp = clist = MLALLOC(np); clist->mod = mod; clist->n = np;
685: for ( i = 0, in = (UM *)blist->c, out = (UM *)clist->c; i < np; i++ ) {
686: divum(mod,wf,in[i],q); tum = wf; wf = q; q = tum;
687: cpyum(wf,wx); cpyum(in[i],wy);
688: eucum(mod,wx,wy,wa,wb); mulum(mod,wa,wg,wm);
689: DEG(wm) = divum(mod,wm,in[i],q); out[i] = UMALLOC(DEG(wm));
690: cpyum(wm,out[i]); mulum(mod,q,wf,wu);
691: mulum(mod,wg,wb,wv); addum(mod,wu,wv,wg);
692: }
693: }
694:
1.4 noro 695: /* find a,b s.t. qa*qg+qb*qh=1 mod q, qg=g mod mod, qh=h mod mod */
696: /* q = mod^k */
697:
698: void henprep2(mod,q,k,f,g,h,qg,qh,qa,qb)
699: int mod,q,k;
700: UM f,g,h,qg,qh,qa,qb;
701: {
702: int n;
703: UM wg,wh,wa,wb;
704: UM wt,ws,wu;
705: ML bl,cl,bql,cql;
706: P ff;
707:
708: n = DEG(f);
709: wg = W_UMALLOC(2*n); wh = W_UMALLOC(2*n);
710: wa = W_UMALLOC(2*n); wb = W_UMALLOC(2*n);
711: cpyum(g,wg); cpyum(h,wh);
712:
713: /* wa*g+wb*h = 1 mod mod */
714: eucum(mod,wg,wh,wa,wb);
715:
716: #if 0
717: /* check */
718: wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
719: mulum(mod,wa,g,wt);
720: mulum(mod,wb,h,ws);
721: addum(mod,wt,ws,wu);
722: if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
723: error("henprep 1");
724: #endif
725:
726: bl = MLALLOC(2); bl->n = 2; bl->mod = mod; bl->c[0] = g; bl->c[1] = h;
727: cl = MLALLOC(2); cl->n = 2; cl->mod = mod; cl->c[0] = wb; cl->c[1] = wa;
728: umtop(CO->v,f,&ff); /* XXX */
729: henprep(ff,bl,cl,&bql,&cql); /* XXX */
730:
731: cpyum(bql->c[0],qg); cpyum(bql->c[1],qh);
732: cpyum(cql->c[0],qb); cpyum(cql->c[1],qa);
733:
734: #if 0
735: /* check */
736: mulum(q,qa,qg,wt);
737: mulum(q,qb,qh,ws);
738: addum(q,wt,ws,wu);
739: if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
740: error("henprep 2");
741: #endif
742: }
743:
1.1 noro 744: /*
1.4 noro 745: henprep(f,blist,clist,&bqlist,&cqlist);
1.1 noro 746: */
747:
748: void henprep(f,blist,clist,bqlistp,cqlistp)
749: P f;
750: ML blist,clist,*bqlistp,*cqlistp;
751: {
752: register int i,j,k,*px,*py,*pz;
753: int n,pmax,dr,tmp,p,p1,mod,np,b,q;
754: UM w,wm,wn,wa,wt,wq,wf,quot,tum,*in,*inc,*out,*outc;
755: ML bqlist,cqlist;
756:
757: n = UDEG(f); p = mod = blist->mod; np = blist->n;
758: /* for ( b = 1, q = mod; q <= (unsigned int)(LBASE / (L)mod); q *= mod, b++ ); */
759: for ( b = 1, q = mod; q <= ((1<<27) / mod); q *= mod, b++ );
760: w = W_UMALLOC(n); ptoum(q,f,w);
761: wm = W_UMALLOC(2*n); wn = W_UMALLOC(2*n);
762: wa = W_UMALLOC(2*n); wt = W_UMALLOC(2*n);
763: wq = W_UMALLOC(2*n); wf = W_UMALLOC(2*n);
764: quot = W_UMALLOC(2*n);
765: *bqlistp = bqlist = MLALLOC(np); *cqlistp = cqlist = MLALLOC(np);
766: for ( i = 0; i < n+2; i++ )
767: COEF(wq)[i] = 0;
768: for ( i = 0,
769: in = (UM *)blist->c, inc = (UM *)clist->c,
770: out = (UM *)bqlist->c, outc = (UM *)cqlist->c;
771: i < np; i++ ) {
772: out[i] = C_UMALLOC(n+1); cpyum(in[i],out[i]);
773: outc[i] = C_UMALLOC(n+1); cpyum(inc[i],outc[i]);
774: }
775: for ( pmax = 1, i = b; i > 0; i-- )
776: pmax *= mod;
777: for ( i = 1; i < b; i++, p = p1 ) {
778: cpyum(out[0],wm);
779: for ( j = 1; j < np; j++ ) {
780: mulum(pmax,wm,out[j],wn);
781: tum = wm; wm = wn; wn = tum;
782: }
783: for ( j = n, px = COEF(w), py = COEF(wm), pz = COEF(wt); j >= 0; j-- ) {
784: tmp = ( ( px[j] - py[j] ) / p ) % mod;
785: pz[j] = ( tmp >= 0? tmp : tmp + mod );
786: }
787: degum(wt,n);
788: for ( j = 1; j < np; j++ ) {
789: mulum(mod,wt,inc[j],wm); dr = divum(mod,wm,in[j],quot);
790: for ( k = DEG(quot); k >= 0; k-- )
791: COEF(wq)[k] = ( COEF(wq)[k] + COEF(quot)[k] ) % mod;
792: for ( k = dr, px = COEF(out[j]), py = COEF(wm); k >= 0; k-- )
793: px[k] += p * py[k];
794: }
795: degum(wq,n); mulum(mod,wq,in[0],wm);
796: mulum(mod,wt,inc[0],wn); addum(mod,wm,wn,wa);
797: for ( j = DEG(wa), px = COEF(out[0]), py = COEF(wa); j >= 0; j-- )
798: px[j] += p * py[j];
799: for ( j = n, px = COEF(wq); j >= 0; j-- )
800: px[j] = 0;
801: p1 = p * mod;
802: for ( j = n, px = COEF(wt); j >= 1; j-- )
803: px[j] = 0;
804: px[0] = 1;
805: for ( j = 0; j < np; j++ ) {
806: cpyum(w,wf);
807: for ( k = DEG(wf), px = COEF(wf); k >= 0; k-- )
808: px[k] %= p1;
809: divum(p1,wf,out[j],quot); mulum(p1,outc[j],quot,wm);
810: for ( k = DEG(wm), px = COEF(wt), py = COEF(wm); k >= 0; k-- )
811: px[k] = ( px[k] - py[k] ) % p1;
812: }
813: degum(wt,n);
814: for ( j = DEG(wt), px = COEF(wt); j >= 0; j-- )
815: px[j] = ((tmp=(px[j]/p)%mod)>= 0?tmp:tmp + mod);
816: for ( j = 0; j < np; j++ ) {
817: mulum(mod,wt,outc[j],wm); dr = divum(mod,wm,in[j],quot);
818: for ( k = dr, px = COEF(outc[j]), py = COEF(wm); k >= 0; k-- )
819: px[k] += p * py[k];
820: degum(outc[j],MAX(DEG(outc[j]),dr));
821: }
822: }
823: bqlist->n = cqlist->n = np;
824: bqlist->mod = cqlist->mod = q;
825: }
826:
827: /*
828: henmain(fl,bqlist,cqlist,listp)
829: */
830:
831: void henmain(f,bqlist,cqlist,listp)
832: LUM f;
833: ML bqlist,cqlist,*listp;
834: {
835: register int i,j,k;
836: int *px,*py;
837: int **pp,**pp1;
838: int n,np,mod,bound,dr,tmp;
839: UM wt,wq0,wq,wr,wm,wm0,wa,q;
840: LUM wb0,wb1,tlum;
841: UM *b,*c;
842: LUM *l;
843: ML list;
844:
845: n = DEG(f); np = bqlist->n; mod = bqlist->mod; bound = bqlist->bound;
846: *listp = list = MLALLOC(n);
847: list->n = np; list->mod = mod; list->bound = bound;
848: W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
849: wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
850: wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
851: wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
852: b = (UM *)bqlist->c; c = (UM *)cqlist->c; l = (LUM *)list->c;
853: for ( i = 0; i < np; i++ ) {
854: l[i] = LUMALLOC(DEG(b[i]),bound);
855: for ( j = DEG(b[i]), pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
856: pp[j][0] = px[j];
857: }
1.5 ! noro 858: #if 0
1.4 noro 859: fprintf(stderr,"bound=%d\n",bound);
1.5 ! noro 860: #endif
1.1 noro 861: for ( i = 1; i < bound; i++ ) {
1.5 ! noro 862: #if 0
1.4 noro 863: fprintf(stderr,".");
1.5 ! noro 864: #endif
1.1 noro 865: mullum(mod,i+1,l[0],l[1],wb0);
866: for ( j = 2; j < np; j++ ) {
867: mullum(mod,i+1,l[j],wb0,wb1);
868: tlum = wb0; wb0 = wb1; wb1 = tlum;
869: }
870: for ( j = n, px = COEF(wt); j >= 0; j-- )
871: px[j] = 0;
872: for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
873: tmp = ( pp[j][i] - pp1[j][i] ) % mod;
874: COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
875: }
876: degum(wt,n);
877: for ( j = n, px = COEF(wq0); j >= 0; j-- )
878: px[j] = 0;
879: for ( j = 1; j < np; j++ ) {
880: mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
881: for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
882: px[k] = ( px[k] + py[k] ) % mod;
883: for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
884: pp[k][i] = px[k];
885: }
886: degum(wq0,n); mulum(mod,wq0,b[0],wm);
887: mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
888: for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
889: pp[j][i] = px[j];
890: for ( j = n, px = COEF(wq0); j >= 0; j-- )
891: px[j] = 0;
892: }
1.5 ! noro 893: #if 0
1.4 noro 894: fprintf(stderr,"\n");
1.5 ! noro 895: #endif
1.1 noro 896: }
897:
898: static double M;
899: static int E;
900:
901: int mignotte(q,f)
902: int q;
903: P f;
904: {
905: int p;
906: unsigned int *b;
907: N c;
908: DCP dc;
909:
910: for ( dc = DC(f), M = 0, E = 0; dc; dc = NEXT(dc) ) {
911: c = NM((Q)COEF(dc)); p = PL(c); b = BD(c);
912: sqad(b[p-1],(p-1)*BSH);
913: }
914: if (E % 2) M *= 2; M = ceil(sqrt(M)); E /= 2;
915: c = NM((Q)COEF(DC(f))); p = PL(c); M *= ((double)BD(c)[p-1]+1.0); E += (p-1) * BSH;
916: return (int)ceil( (0.31*(E+UDEG(f)+1)+log10((double)M)) / log10((double)q) );
917: }
918:
919: int mig(q,d,f)
920: int q,d;
921: P f;
922: {
923: int p;
924: unsigned int *b;
925: N c;
926: DCP dc;
927:
928: for ( dc = DC(f), M = 0, E = 0; dc; dc = NEXT(dc) ) {
929: c = NM((Q)COEF(dc)); p = PL(c); b = BD(c);
930: sqad(b[p-1],(p-1)*BSH);
931: }
932: if (E % 2) M *= 2; M = ceil(sqrt(M)); E /= 2;
933: c = NM((Q)COEF(DC(f))); p = PL(c);
934: M *= (BD(c)[p-1]+1); E += (p-1) * BSH;
935: return (int)ceil( (0.31*(E+d+1)+log10((double)M)) / log10((double)q) );
936: }
937:
938: void sqad(man,exp)
939: unsigned int man;
940: int exp;
941: {
942: int e,sqe;
943: unsigned int t;
944: double man1,d,sqm;
945: int diff;
946:
947: if ( man == BMASK ) {
948: e = BSH; man1 = 1.0;
949: } else {
950: man += 1;
951: for ( e = 0, t = man; t; e++, t >>= 1 );
952: e--; d = (double)(1<<e);
953: man1 = ((double)man)/d;
954: }
955: exp += e; sqm = man1 * man1; sqe = 2 * exp;
956: if ( sqm >= 2.0 ) {
957: sqm /= 2.0; sqe++;
958: }
959: diff = E - sqe;
960: if ( diff > 18 )
961: return;
962: if ( diff < -18 ) {
963: M = sqm; E = sqe;
964: return;
965: }
966: if ( diff >= 0 )
967: M += (sqm / (double)(1<<diff));
968: else {
969: M = ( ( M / (double)(1<<-diff)) + sqm ); E = sqe;
970: }
971: if ( M >= 2.0 ) {
972: M /= 2.0; E++;
973: }
974: }
975:
976: void ptolum(q,bound,f,fl)
977: int q,bound;
978: P f;
979: LUM fl;
980: {
981: DCP dc;
982: int i,j;
983: int **pp;
984: int d,br,s;
985: unsigned int r;
986: int *c;
987: unsigned int *m,*w;
988:
989: for ( dc = DC(f), pp = COEF(fl); dc; dc = NEXT(dc) ) {
990: d = PL(NM((Q)COEF(dc))); m = BD(NM((Q)COEF(dc)));
991: c = pp[QTOS(DEG(dc))]; w = (unsigned int *)W_ALLOC(d);
992: for ( i = 0; i < d; i++ )
993: w[i] = m[i];
994: for ( i = 0; d >= 1; ) {
995: for ( j = d - 1, r = 0; j >= 0; j-- ) {
996: DSAB(q,r,w[j],w[j],r)
997: }
998: c[i++] = (int)r;
999: if ( !w[d-1] )
1000: d--;
1001: }
1002: if ( SGN((Q)COEF(dc)) < 0 )
1003: for (i = 0, br = 0; i < bound; i++ )
1004: if ( ( s = -(c[i] + br) ) < 0 ) {
1005: c[i] = s + q; br = 1;
1006: } else {
1007: c[i] = 0; br = 0;
1008: }
1009: }
1010: }
1011:
1012: void modfctrp(p,mod,flag,dcp)
1013: P p;
1014: int mod,flag;
1015: DCP *dcp;
1016: {
1017: int cm,n,i,j,k;
1018: DCP dc,dc0;
1019: P zp;
1020: Q c,q;
1021: UM mp;
1022: UM *tl;
1023: struct oDUM *udc,*udc1;
1024:
1025: if ( !p ) {
1026: *dcp = 0; return;
1027: }
1028: ptozp(p,1,&c,&zp);
1029: if ( DN(c) || !(cm = rem(NM(c),mod)) ) {
1030: *dcp = 0; return;
1031: }
1032: mp = W_UMALLOC(UDEG(p));
1033: ptoum(mod,zp,mp);
1034: if ( (n = DEG(mp)) < 0 ) {
1035: *dcp = 0; return;
1036: } else if ( n == 0 ) {
1037: cm = dmar(cm,COEF(mp)[0],0,mod); STOQ(cm,q);
1038: NEWDC(dc); COEF(dc) = (P)q; DEG(dc) = ONE;
1039: NEXT(dc) = 0; *dcp = dc;
1040: return;
1041: }
1042: if ( COEF(mp)[n] != 1 ) {
1043: cm = dmar(cm,COEF(mp)[n],0,mod);
1044: i = invm(COEF(mp)[n],mod);
1045: for ( j = 0; j <= n; j++ )
1046: COEF(mp)[j] = dmar(COEF(mp)[j],i,0,mod);
1047: }
1048: W_CALLOC(n+1,struct oDUM,udc);
1049: gensqfrum(mod,mp,udc);
1050: switch ( flag ) {
1051: case FCTR:
1052: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1053: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1054: for ( i = 0,j = 0; udc[i].f; i++ )
1055: if ( DEG(udc[i].f) == 1 ) {
1056: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1057: } else {
1058: bzero((char *)tl,(n+1)*sizeof(UM));
1059: berlemain(mod,udc[i].f,tl);
1060: for ( k = 0; tl[k]; k++, j++ ) {
1061: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1062: }
1063: }
1064: udc = udc1; break;
1065: case SQFR:
1066: break;
1067: case DDD:
1068: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1069: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1070: for ( i = 0,j = 0; udc[i].f; i++ )
1071: if ( DEG(udc[i].f) == 1 ) {
1072: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1073: } else {
1074: bzero((char *)tl,(n+1)*sizeof(UM));
1075: ddd(mod,udc[i].f,tl);
1076: for ( k = 0; tl[k]; k++, j++ ) {
1077: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1078: }
1079: }
1080: udc = udc1; break;
1081: case NEWDDD:
1082: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1083: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1084: for ( i = 0,j = 0; udc[i].f; i++ )
1085: if ( DEG(udc[i].f) == 1 ) {
1086: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1087: } else {
1088: bzero((char *)tl,(n+1)*sizeof(UM));
1089: if ( mod == 2 )
1090: berlemain(mod,udc[i].f,tl);
1091: else
1092: newddd(mod,udc[i].f,tl);
1093: for ( k = 0; tl[k]; k++, j++ ) {
1094: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1095: }
1096: }
1097: udc = udc1; break;
1098: }
1099: NEWDC(dc0); STOQ(cm,q); COEF(dc0) = (P)q; DEG(dc0) = ONE; dc = dc0;
1100: for ( n = 0; udc[n].f; n++ ) {
1101: NEWDC(NEXT(dc)); dc = NEXT(dc);
1102: STOQ(udc[n].n,DEG(dc)); umtop(VR(p),udc[n].f,&COEF(dc));
1103: }
1104: NEXT(dc) = 0; *dcp = dc0;
1105: }
1106:
1107: void gensqfrum(mod,p,dc)
1108: int mod;
1109: UM p;
1110: struct oDUM *dc;
1111: {
1112: int n,i,j,d;
1113: UM t,s,g,f,f1,b;
1114:
1115: if ( (n = DEG(p)) == 1 ) {
1116: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
1117: return;
1118: }
1119: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
1120: f = W_UMALLOC(n); f1 = W_UMALLOC(n); b = W_UMALLOC(n);
1121: diffum(mod,p,t); cpyum(p,s); Gcdum(mod,t,s,g);
1122: if ( !DEG(g) ) {
1123: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
1124: return;
1125: }
1126: cpyum(p,b); cpyum(p,t); Divum(mod,t,g,f);
1127: for ( i = 0, d = 0; DEG(f); i++ ) {
1128: while ( 1 ) {
1129: cpyum(b,t);
1130: if ( Divum(mod,t,f,s) >= 0 )
1131: break;
1132: else {
1133: cpyum(s,b); d++;
1134: }
1135: }
1136: cpyum(b,t); cpyum(f,s); Gcdum(mod,t,s,f1);
1137: Divum(mod,f,f1,s); cpyum(f1,f);
1138: dc[i].f = UMALLOC(DEG(s)); cpyum(s,dc[i].f); dc[i].n = d;
1139: }
1140: if ( DEG(b) > 0 ) {
1141: d = 1;
1142: while ( 1 ) {
1143: cpyum(b,t);
1144: for ( j = DEG(t); j >= 0; j-- )
1145: if ( COEF(t)[j] && (j % mod) )
1146: break;
1147: if ( j >= 0 )
1148: break;
1149: else {
1150: DEG(s) = DEG(t)/mod;
1151: for ( j = 0; j <= DEG(t); j++ )
1152: COEF(s)[j] = COEF(t)[j*mod];
1153: cpyum(s,b); d *= mod;
1154: }
1155: }
1156: gensqfrum(mod,b,dc+i);
1157: for ( j = i; dc[j].f; j++ )
1158: dc[j].n *= d;
1159: }
1160: }
1161:
1162: #if 0
1163: void srchum(mod,p1,p2,gr)
1164: int mod;
1165: UM p1,p2,gr;
1166: {
1167: UM m,m1,m2,q,r,t,g1,g2;
1168: int lc,d,d1,d2,i,j,k,l,l1,l2,l3,tmp,adj;
1169: V v;
1170:
1171: d = MAX(DEG(p1),DEG(p2));
1172: g1 = W_UMALLOC(d); g2 = W_UMALLOC(d);
1173: bzero((char *)g1,(d+2)*sizeof(int)); bzero((char *)g2,(d+2)*sizeof(int));
1174: if ( d == DEG(p1) ) {
1175: cpyum(p1,g1); cpyum(p2,g2);
1176: } else {
1177: cpyum(p1,g2); cpyum(p2,g1);
1178: }
1179: if ( ( d1 = DEG(g1) ) > ( d2 = DEG(g2) ) ) {
1180: j = d1 - 1; adj = 1;
1181: } else
1182: j = d2;
1183: lc = 1;
1184: r = W_UMALLOC(d1+d2); q = W_UMALLOC(d1+d2);
1185: m1 = W_UMALLOC(d1+d2); t = W_UMALLOC(d1+d2);
1186: bzero((char *)r,(d1+d2+2)*sizeof(int)); bzero((char *)q,(d1+d2+2)*sizeof(int));
1187: bzero((char *)m1,(d1+d2+2)*sizeof(int)); bzero((char *)t,(d1+d2+2)*sizeof(int));
1188: m = W_UMALLOC(0); bzero((char *)m,2*sizeof(int));
1189: adj = pwrm(mod,COEF(g2)[DEG(g2)],DEG(g1));
1190: DEG(m) = 0; COEF(m)[0] = invm(COEF(g2)[DEG(g2)],mod);
1191: Mulum(mod,g2,m,r); cpyum(r,g2);
1192: while ( 1 ) {
1193: if ( ( k = DEG(g2) ) < 0 ) {
1194: DEG(gr) = -1;
1195: return;
1196: }
1197: if ( k == j ) {
1198: if ( k == 0 ) {
1199: DEG(m) = 0; COEF(m)[0] = adj;
1200: Mulum(mod,g2,m,gr);
1201: return;
1202: } else {
1203: DEG(m) = 0;
1204: COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
1205: Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,t);
1206: DEG(m) = 0; COEF(m)[0] = dmb(mod,lc,lc,&tmp);
1207: Divum(mod,r,m,q); cpyum(g2,g1); cpyum(q,g2);
1208: lc = COEF(g1)[DEG(g1)]; j = k - 1;
1209: }
1210: } else {
1211: d = j - k;
1212: DEG(m) = 0; COEF(m)[0] = pwrm(mod,COEF(g2)[DEG(g2)],d);
1213: Mulum(mod,g2,m,m1); l = pwrm(mod,lc,d);
1214: DEG(m) = 0; COEF(m)[0] = l; Divum(mod,m1,m,t);
1215: if ( k == 0 ) {
1216: DEG(m) = 0; COEF(m)[0] = adj;
1217: Mulum(mod,t,m,gr);
1218: return;
1219: } else {
1220: DEG(m) = 0;
1221: COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
1222: Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,q);
1223: l1 = dmb(mod,lc,lc,&tmp); l2 = dmb(mod,l,l1,&tmp);
1224: DEG(m) = 0; COEF(m)[0] = l2;
1225: Divum(mod,r,m,q); cpyum(t,g1); cpyum(q,g2);
1226: if ( d % 2 )
1227: for ( i = DEG(g2); i >= 0; i-- )
1228: COEF(g2)[i] = ( mod - COEF(g2)[i] ) % mod;
1229: lc = COEF(g1)[DEG(g1)]; j = k - 1;
1230: }
1231: }
1232: }
1233: }
1234:
1235: UM *resberle(mod,f,fp)
1236: register int mod;
1237: UM f;
1238: UM *fp;
1239: {
1240: UM w,wg,ws,wf,f0,gcd,q,res;
1241: int n;
1242: register int i;
1243:
1244: n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
1245: if ( DEG(wg) <= 0 ) {
1246: f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
1247: return ( fp );
1248: }
1249: f0 = W_UMALLOC(n); cpyum(f,f0);
1250: ws = W_UMALLOC(n); wf = W_UMALLOC(n);
1251: q = W_UMALLOC(n); gcd = W_UMALLOC(n);
1252: res = W_UMALLOC(2*n);
1253: srchum(mod,f,wg,res);
1254: for ( i = 0; i < mod; i++ ) {
1255: if ( substum(mod,res,i) )
1256: continue;
1257: cpyum(f0,wf); cpyum(wg,ws);
1258: COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
1259: Gcdum(mod,wf,ws,gcd);
1260: if ( DEG(gcd) > 0 ) {
1261: if ( DEG(gcd) < n ) {
1262: Divum(mod,f0,gcd,q); f0 = q; fp = resberle(mod,gcd,fp);
1263: }
1264: break;
1265: }
1266: }
1267: fp = resberle(mod,f0,fp);
1268: return ( fp );
1269: }
1270:
1271: int substum(mod,p,a)
1272: int mod;
1273: UM p;
1274: int a;
1275: {
1276: int i,j,s;
1277: int *c;
1278:
1279: if ( DEG(p) < 0 )
1280: return 0;
1281: if ( DEG(p) == 0 )
1282: return COEF(p)[0];
1283: for ( i = DEG(p), c = COEF(p), s = c[i]; i >= 0; ) {
1284: for ( j = i--; (i>=0) && !c[i]; i-- );
1285: if ( i >= 0 )
1286: s = (s*pwrm(mod,a,j-i)%mod+c[i])%mod;
1287: else
1288: s = s*pwrm(mod,a,j)%mod;
1289: }
1290: return s;
1291: }
1292: #endif
1293:
1294: void ddd(mod,f,r)
1295: int mod;
1296: UM f,*r;
1297: {
1298: register int i,j;
1299: int d,n;
1300: UM q,s,t,u,v,w,g,x,m;
1301: UM *base;
1302:
1303: n = DEG(f);
1304: if ( n == 1 ) {
1305: r[0] = UMALLOC(1); cpyum(f,r[0]); r[1] = 0; return;
1306: }
1307: base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1308: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1309: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1310: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1311: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1312: for ( i = 2; i < n; i++ ) {
1313: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1314: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1315: }
1316: v = W_UMALLOC(n); cpyum(f,v);
1317: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1318: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1319: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1320: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1321: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1322: if ( COEF(w)[i] ) {
1323: Mulsum(mod,base[i],COEF(w)[i],s);
1324: addum(mod,s,t,u); cpyum(u,t);
1325: }
1326: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1327: if ( DEG(g) >= 1 ) {
1328: canzas(mod,g,d,base,r+j); j += DEG(g)/d;
1329: Divum(mod,v,g,q); cpyum(q,v);
1330: DEG(w) = Divum(mod,w,v,q);
1331: for ( i = 0; i < DEG(v); i++ )
1332: DEG(base[i]) = Divum(mod,base[i],v,q);
1333: }
1334: }
1335: if ( DEG(v) ) {
1336: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
1337: }
1338: r[j] = 0;
1339: }
1340:
1341: #if 0
1342: void canzas(mod,f,d,base,r)
1343: int mod;
1344: UM f,*base,*r;
1345: {
1346: UM t,s,u,w,g,o,q;
1347: N n1,n2,n3,n4,n5;
1348: UM *b;
1349: int n,m,i;
1350:
1351: if ( DEG(f) == d ) {
1352: r[0] = UMALLOC(d); cpyum(f,r[0]);
1353: return;
1354: } else {
1355: n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)b,n*sizeof(UM));
1356: for ( i = 0, m = 0; i < n; i++ )
1357: m = MAX(m,DEG(base[i]));
1358: q = W_UMALLOC(m);
1359: for ( i = 0; i < n; i++ ) {
1360: b[i] = W_UMALLOC(DEG(base[i])); cpyum(base[i],b[i]);
1361: DEG(b[i]) = Divum(mod,b[i],f,q);
1362: }
1363: t = W_UMALLOC(2*d);
1364: s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
1365: w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
1366: o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = 1;
1367: STON(mod,n1); pwrn(n1,d,&n2); subn(n2,ONEN,&n3);
1368: STON(2,n4); divsn(n3,n4,&n5);
1369: while ( 1 ) {
1370: randum(mod,2*d,t); spwrum(mod,f,b,t,n5,s);
1371: subum(mod,s,o,u); cpyum(f,w); Gcdum(mod,w,u,g);
1372: if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
1373: canzas(mod,g,d,b,r);
1374: cpyum(f,w); Divum(mod,w,g,s);
1375: canzas(mod,s,d,b,r+DEG(g)/d);
1376: return;
1377: }
1378: }
1379: }
1380: }
1381: #else
1382: void canzas(mod,f,d,base,r)
1383: int mod;
1384: UM f,*base,*r;
1385: {
1386: UM t,s,u,w,g,o,q;
1387: N n1,n2,n3,n4,n5;
1388: UM *b;
1389: int n,m,i;
1390:
1391: if ( DEG(f) == d ) {
1392: r[0] = UMALLOC(d); cpyum(f,r[0]);
1393: return;
1394: } else {
1395: n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)b,n*sizeof(UM));
1396: for ( i = 0, m = 0; i < n; i++ )
1397: m = MAX(m,DEG(base[i]));
1398: q = W_UMALLOC(m);
1399: for ( i = 0; i < n; i++ ) {
1400: b[i] = W_UMALLOC(DEG(base[i])); cpyum(base[i],b[i]);
1401: DEG(b[i]) = Divum(mod,b[i],f,q);
1402: }
1403: t = W_UMALLOC(2*d);
1404: s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
1405: w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
1406: o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = 1;
1407: STON(mod,n1); pwrn(n1,d,&n2); subn(n2,ONEN,&n3);
1408: STON(2,n4); divsn(n3,n4,&n5);
1409: while ( 1 ) {
1410: randum(mod,2*d,t); spwrum0(mod,f,t,n5,s);
1411: subum(mod,s,o,u); cpyum(f,w); Gcdum(mod,w,u,g);
1412: if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
1413: canzas(mod,g,d,b,r);
1414: cpyum(f,w); Divum(mod,w,g,s);
1415: canzas(mod,s,d,b,r+DEG(g)/d);
1416: return;
1417: }
1418: }
1419: }
1420: }
1421: #endif
1422:
1423: void randum(mod,d,p)
1424: int mod,d;
1425: UM p;
1426: {
1427: unsigned int n;
1428: int i;
1429:
1430: n = ((unsigned int)random()) % d; DEG(p) = n; COEF(p)[n] = 1;
1431: for ( i = 0; i < (int)n; i++ )
1432: COEF(p)[i] = ((unsigned int)random()) % mod;
1433: }
1434:
1435: void pwrmodum(mod,p,e,f,pr)
1436: int mod,e;
1437: UM p,f,pr;
1438: {
1439: UM wt,ws,q;
1440:
1441: if ( e == 0 ) {
1442: DEG(pr) = 0; COEF(pr)[0] = 1;
1443: } else if ( DEG(p) < 0 )
1444: DEG(pr) = -1;
1445: else if ( e == 1 ) {
1446: q = W_UMALLOC(DEG(p)); cpyum(p,pr);
1447: DEG(pr) = divum(mod,pr,f,q);
1448: } else if ( DEG(p) == 0 ) {
1449: DEG(pr) = 0; COEF(pr)[0] = pwrm(mod,COEF(p)[0],e);
1450: } else {
1451: wt = W_UMALLOC(2*DEG(f)); ws = W_UMALLOC(2*DEG(f));
1452: q = W_UMALLOC(2*DEG(f));
1453: pwrmodum(mod,p,e/2,f,wt);
1454: if ( !(e%2) ) {
1455: mulum(mod,wt,wt,pr); DEG(pr) = divum(mod,pr,f,q);
1456: } else {
1457: mulum(mod,wt,wt,ws); DEG(ws) = divum(mod,ws,f,q);
1458: mulum(mod,ws,p,pr); DEG(pr) = divum(mod,pr,f,q);
1459: }
1460: }
1461: }
1462:
1463: void spwrum(mod,m,base,f,e,r)
1464: int mod;
1465: UM f,m,r;
1466: UM *base;
1467: N e;
1468: {
1469: int a,n,i;
1470: N e1,an;
1471: UM t,s,u,q,r1,r2;
1472:
1473: if ( !e ) {
1474: DEG(r) = 0; COEF(r)[0] = 1;
1475: } else if ( UNIN(e) )
1476: cpyum(f,r);
1477: else if ( (PL(e) == 1) && (BD(e)[0] < (unsigned int)mod) )
1478: spwrum0(mod,m,f,e,r);
1479: else {
1480: a = divin(e,mod,&e1); STON(a,an);
1481: n = DEG(m); t = W_UMALLOC(n); s = W_UMALLOC(n);
1482: u = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
1483: for ( DEG(t) = -1, i = 0; i <= DEG(f); i++ )
1484: if ( COEF(f)[i] ) {
1485: Mulsum(mod,base[i],COEF(f)[i],s);
1486: addum(mod,s,t,u); cpyum(u,t);
1487: }
1488: r1 = W_UMALLOC(n); spwrum0(mod,m,f,an,r1);
1489: r2 = W_UMALLOC(n); spwrum(mod,m,base,t,e1,r2);
1490: Mulum(mod,r1,r2,u); DEG(u) = Divum(mod,u,m,q);
1491: cpyum(u,r);
1492: }
1493: }
1494:
1495: void spwrum0(mod,m,f,e,r)
1496: int mod;
1497: UM f,m,r;
1498: N e;
1499: {
1500: UM t,s,q;
1501: N e1;
1502: int a;
1503:
1504: if ( !e ) {
1505: DEG(r) = 0; COEF(r)[0] = 1;
1506: } else if ( UNIN(e) )
1507: cpyum(f,r);
1508: else {
1509: a = divin(e,2,&e1);
1510: t = W_UMALLOC(2*DEG(m)); spwrum0(mod,m,f,e1,t);
1511: s = W_UMALLOC(2*DEG(m)); q = W_UMALLOC(2*DEG(m));
1512: Mulum(mod,t,t,s); DEG(s) = Divum(mod,s,m,q);
1513: if ( a ) {
1514: Mulum(mod,s,f,t); DEG(t) = Divum(mod,t,m,q); cpyum(t,r);
1515: } else
1516: cpyum(s,r);
1517: }
1518: }
1519:
1520: #if 0
1521: void Mulum(mod,p1,p2,pr)
1522: register int mod;
1523: UM p1,p2,pr;
1524: {
1525: register int *pc1,*pcr;
1526: register int mul,i,j,d1,d2;
1527: int *c1,*c2,*cr;
1528:
1529: if ( ( (d1 = DEG(p1)) < 0) || ( (d2 = DEG(p2)) < 0 ) ) {
1530: DEG(pr) = -1;
1531: return;
1532: }
1533: c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
1534: bzero((char *)cr,(d1+d2+1)*sizeof(int));
1535: for ( i = 0; i <= d2; i++, cr++ )
1536: if ( mul = *c2++ )
1537: for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ )
1538: *pcr = (*pc1 * mul + *pcr) % mod;
1539: DEG(pr) = d1 + d2;
1540: }
1541:
1542: void Mulsum(mod,p,n,pr)
1543: register int mod,n;
1544: UM p,pr;
1545: {
1546: register int *sp,*dp;
1547: register int i;
1548:
1549: for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i;
1550: i >= 0; i--, dp--, sp-- )
1551: *dp = (*sp * n) % mod;
1552: }
1553:
1554: int Divum(mod,p1,p2,pq)
1555: register int mod;
1556: UM p1,p2,pq;
1557: {
1558: register int *pc1,*pct;
1559: register int tmp,i,j,inv;
1560: int *c1,*c2,*ct;
1561: int d1,d2,dd,hd;
1562:
1563: if ( (d1 = DEG(p1)) < (d2 = DEG(p2)) ) {
1564: DEG(pq) = -1;
1565: return( d1 );
1566: }
1567: c1 = COEF(p1); c2 = COEF(p2); dd = d1-d2;
1568: if ( ( hd = c2[d2] ) != 1 ) {
1569: inv = invm(hd,mod);
1570: for ( pc1 = c2 + d2; pc1 >= c2; pc1-- )
1571: *pc1 = (*pc1 * inv) % mod;
1572: } else
1573: inv = 1;
1574: for ( i = dd, ct = c1+d1; i >= 0; i-- )
1575: if ( tmp = *ct-- ) {
1576: tmp = mod - tmp;
1577: for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- )
1578: *pct = (*pc1 * tmp + *pct) % mod;
1579: }
1580: if ( inv != 1 ) {
1581: for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ )
1582: *pc1 = (*pc1 * inv) % mod;
1583: for ( pc1 = c2, pct = c2+d2, inv = hd; pc1 <= pct; pc1++ )
1584: *pc1 = (*pc1 * inv) % mod;
1585: }
1586: for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- );
1587: for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- )
1588: *pct-- = *pc1--;
1589: return( i );
1590: }
1591:
1592: void Gcdum(mod,p1,p2,pr)
1593: register int mod;
1594: UM p1,p2,pr;
1595: {
1596: register int *sp,*dp;
1597: register int i,inv;
1598: UM t1,t2,q,tum;
1599: int drem;
1600:
1601: if ( DEG(p1) < 0 )
1602: cpyum(p2,pr);
1603: else if ( DEG(p2) < 0 )
1604: cpyum(p1,pr);
1605: else {
1606: if ( DEG(p1) >= DEG(p2) ) {
1607: t1 = p1; t2 = p2;
1608: } else {
1609: t1 = p2; t2 = p1;
1610: }
1611: q = W_UMALLOC(DEG(t1));
1612: while ( ( drem = Divum(mod,t1,t2,q) ) >= 0 ) {
1613: tum = t1; t1 = t2; t2 = tum; DEG(t2) = drem;
1614: }
1615: inv = invm(COEF(t2)[DEG(t2)],mod);
1616: Mulsum(mod,t2,inv,pr);
1617: }
1618: }
1619: #endif
1620:
1621: void mult_mod_tab(p,mod,tab,r,d)
1622: UM p,r;
1623: UM *tab;
1624: int mod,d;
1625: {
1626: UM w,w1,c;
1627: int n,i;
1628: int *pc;
1629:
1630: w = W_UMALLOC(d); w1 = W_UMALLOC(d);
1631: c = W_UMALLOC(1); DEG(c) = 0;
1632: n = DEG(p); DEG(r) = -1;
1633: for ( i = 0, pc = COEF(p); i <= n; i++ )
1634: if ( pc[i] ) {
1635: COEF(c)[0] = pc[i];
1636: mulum(mod,tab[i],c,w);
1637: addum(mod,r,w,w1);
1638: cpyum(w1,r);
1639: }
1640: }
1641:
1642: void make_qmat(p,mod,tab,mp)
1643: UM p;
1644: int mod;
1645: UM *tab;
1646: int ***mp;
1647: {
1648: int n,i,j;
1649: int *c;
1650: UM q,r;
1651: int **mat;
1652:
1653: n = DEG(p);
1654: *mp = mat = almat(n,n);
1655: for ( j = 0; j < n; j++ ) {
1656: r = W_UMALLOC(DEG(tab[j])); q = W_UMALLOC(DEG(tab[j]));
1657: cpyum(tab[j],r); DEG(r) = divum(mod,r,p,q);
1658: for ( i = 0, c = COEF(r); i <= DEG(r); i++ )
1659: mat[i][j] = c[i];
1660: }
1661: for ( i = 0; i < n; i++ )
1662: mat[i][i] = (mat[i][i]+mod-1) % mod;
1663: }
1664:
1665: void null_mod(mat,mod,n,ind)
1666: int **mat;
1667: int *ind;
1668: int mod,n;
1669: {
1670: int i,j,l,s,h,inv;
1671: int *t,*u;
1672:
1673: bzero((char *)ind,n*sizeof(int));
1674: ind[0] = 0;
1675: for ( i = j = 0; j < n; i++, j++ ) {
1676: for ( ; j < n; j++ ) {
1677: for ( l = i; l < n; l++ )
1678: if ( mat[l][j] )
1679: break;
1680: if ( l < n ) {
1681: t = mat[i]; mat[i] = mat[l]; mat[l] = t; break;
1682: } else
1683: ind[j] = 1;
1684: }
1685: if ( j == n )
1686: break;
1687: inv = invm(mat[i][j],mod);
1688: for ( s = j, t = mat[i]; s < n; s++ )
1689: t[s] = dmar(t[s],inv,0,mod);
1690: for ( l = 0; l < n; l++ ) {
1691: if ( l == i )
1692: continue;
1693: for ( s = j, u = mat[l], h = (mod-u[j])%mod; s < n; s++ )
1694: u[s] = dmar(h,t[s],u[s],mod);
1695: }
1696: }
1697: }
1698:
1699: void null_to_sol(mat,ind,mod,n,r)
1700: int **mat;
1701: int *ind;
1702: int mod,n;
1703: UM *r;
1704: {
1705: int i,j,k,l;
1706: int *c;
1707: UM w;
1708:
1709: for ( i = 0, l = 0; i < n; i++ ) {
1710: if ( !ind[i] )
1711: continue;
1712: w = UMALLOC(n);
1713: for ( j = k = 0, c = COEF(w); j < n; j++ )
1714: if ( ind[j] )
1715: c[j] = 0;
1716: else
1717: c[j] = mat[k++][i];
1718: c[i] = mod-1;
1719: for ( j = n; j >= 0; j-- )
1720: if ( c[j] )
1721: break;
1722: DEG(w) = j;
1723: r[l++] = w;
1724: }
1725: }
1726: /*
1727: make_qmat(p,mod,tab,mp)
1728: null_mod(mat,mod,n,ind)
1729: null_to_sol(mat,ind,mod,n,r)
1730: */
1731:
1732: void newddd(mod,f,r)
1733: int mod;
1734: UM f,*r;
1735: {
1736: register int i,j;
1737: int d,n;
1738: UM q,s,t,u,v,w,g,x,m;
1739: UM *base;
1740:
1741: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1742: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1743: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1744: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1745: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1746: for ( i = 2; i < n; i++ ) {
1747: /* fprintf(stderr,"i=%d\n",i); */
1748: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1749: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1750: }
1751: v = W_UMALLOC(n); cpyum(f,v);
1752: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1753: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1754: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1755: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1756: /* fprintf(stderr,"d=%d\n",d); */
1757: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1758: if ( COEF(w)[i] ) {
1759: Mulsum(mod,base[i],COEF(w)[i],s);
1760: addum(mod,s,t,u); cpyum(u,t);
1761: }
1762: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1763: if ( DEG(g) >= 1 ) {
1764: berlekamp(g,mod,d,base,r+j); j += DEG(g)/d;
1765: Divum(mod,v,g,q); cpyum(q,v);
1766: DEG(w) = Divum(mod,w,v,q);
1767: for ( i = 0; i < DEG(v); i++ )
1768: DEG(base[i]) = Divum(mod,base[i],v,q);
1769: }
1770: }
1771: if ( DEG(v) ) {
1772: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
1773: }
1774: r[j] = 0;
1775: }
1776:
1777: int nfctr_mod(f,mod)
1778: int mod;
1779: UM f;
1780: {
1781: register int i,j;
1782: int d,n;
1783: UM q,s,t,u,v,w,g,x,m;
1784: UM *base;
1785:
1786: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1787: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1788: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1789: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1790: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1791: for ( i = 2; i < n; i++ ) {
1792: /* fprintf(stderr,"i=%d\n",i); */
1793: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1794: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1795: }
1796: v = W_UMALLOC(n); cpyum(f,v);
1797: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1798: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1799: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1800: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1801: /* fprintf(stderr,"d=%d\n",d); */
1802: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1803: if ( COEF(w)[i] ) {
1804: Mulsum(mod,base[i],COEF(w)[i],s);
1805: addum(mod,s,t,u); cpyum(u,t);
1806: }
1807: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1808: if ( DEG(g) >= 1 ) {
1809: j += DEG(g)/d;
1810: Divum(mod,v,g,q); cpyum(q,v);
1811: DEG(w) = Divum(mod,w,v,q);
1812: for ( i = 0; i < DEG(v); i++ )
1813: DEG(base[i]) = Divum(mod,base[i],v,q);
1814: }
1815: }
1816: if ( DEG(v) ) j++;
1817: return j;
1818: }
1819:
1820: int irred_check(f,mod)
1821: UM f;
1822: int mod;
1823: {
1824: register int i,j;
1825: int d,n;
1826: UM q,s,t,u,v,w,g,x,m,f1,b;
1827: UM *base;
1828:
1829: if ( (n = DEG(f)) == 1 )
1830: return 1;
1831: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
1832: f1 = W_UMALLOC(n); b = W_UMALLOC(n);
1833: diffum(mod,f,t); cpyum(f,s); Gcdum(mod,t,s,g);
1834: if ( DEG(g) )
1835: return 0;
1836:
1837: base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1838: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1839: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1840: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1841: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1842: for ( i = 2; i < n; i++ ) {
1843: /* fprintf(stderr,"i=%d\n",i); */
1844: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1845: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1846: }
1847: v = W_UMALLOC(n); cpyum(f,v);
1848: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1849: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1850: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1851: for ( j = 0, d = 1; 2*d <= n; d++ ) {
1852: /* fprintf(stderr,"d=%d\n",d); */
1853: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1854: if ( COEF(w)[i] ) {
1855: Mulsum(mod,base[i],COEF(w)[i],s);
1856: addum(mod,s,t,u); cpyum(u,t);
1857: }
1858: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1859: if ( DEG(g) >= 1 )
1860: return 0;
1861: }
1862: return 1;
1863: }
1864:
1865: int berlekamp(p,mod,df,tab,r)
1866: UM p;
1867: int mod,df;
1868: UM *tab,*r;
1869: {
1870: int n,i,j,k,nf,d,nr;
1871: int **mat;
1872: int *ind;
1873: UM mp,w,q,gcd,w1,w2;
1874: UM *u;
1875: int *root;
1876:
1877: n = DEG(p);
1878: ind = ALLOCA(n*sizeof(int));
1879: make_qmat(p,mod,tab,&mat);
1880: null_mod(mat,mod,n,ind);
1881: for ( i = 0, d = 0; i < n; i++ )
1882: if ( ind[i] )
1883: d++;
1884: if ( d == 1 ) {
1885: r[0] = UMALLOC(n); cpyum(p,r[0]); return 1;
1886: }
1887: u = ALLOCA(d*sizeof(UM *));
1888: r[0] = UMALLOC(n); cpyum(p,r[0]);
1889: null_to_sol(mat,ind,mod,n,u);
1890: root = ALLOCA(d*sizeof(int));
1891: w = W_UMALLOC(n); mp = W_UMALLOC(d);
1892: w1 = W_UMALLOC(n); w2 = W_UMALLOC(n);
1893: for ( i = 1, nf = 1; i < d; i++ ) {
1894: minipoly_mod(mod,u[i],p,mp);
1895: nr = find_root(mod,mp,root);
1896: for ( j = 0; j < nf; j++ ) {
1897: if ( DEG(r[j]) == df )
1898: continue;
1899: for ( k = 0; k < nr; k++ ) {
1900: cpyum(u[i],w1); cpyum(r[j],w2);
1901: COEF(w1)[0] = (mod-root[k]) % mod;
1902: gcdum(mod,w1,w2,w);
1903: if ( DEG(w) > 0 && DEG(w) < DEG(r[j]) ) {
1904: gcd = UMALLOC(DEG(w));
1905: q = UMALLOC(DEG(r[j])-DEG(w));
1906: cpyum(w,gcd); divum(mod,r[j],w,q);
1907: r[j] = q; r[nf++] = gcd;
1908: }
1909: if ( nf == d )
1910: return d;
1911: }
1912: }
1913: }
1914: }
1915:
1916: void minipoly_mod(mod,f,p,mp)
1917: int mod;
1918: UM f,p,mp;
1919: {
1920: struct p_pair *list,*l,*l1,*lprev;
1921: int n,d;
1922: UM u,p0,p1,np0,np1,q,w;
1923:
1924: list = (struct p_pair *)MALLOC(sizeof(struct p_pair));
1925: list->p0 = u = W_UMALLOC(0); DEG(u) = 0; COEF(u)[0] = 1;
1926: list->p1 = W_UMALLOC(0); cpyum(list->p0,list->p1);
1927: list->next = 0;
1928: n = DEG(p); w = UMALLOC(2*n);
1929: p0 = UMALLOC(2*n); cpyum(list->p0,p0);
1930: p1 = UMALLOC(2*n); cpyum(list->p1,p1);
1931: q = W_UMALLOC(2*n);
1932: while ( 1 ) {
1933: COEF(p0)[DEG(p0)] = 0; DEG(p0)++; COEF(p0)[DEG(p0)] = 1;
1934: mulum(mod,f,p1,w); DEG(w) = divum(mod,w,p,q); cpyum(w,p1);
1935: np0 = UMALLOC(n); np1 = UMALLOC(n);
1936: lnf_mod(mod,n,p0,p1,list,np0,np1);
1937: if ( DEG(np1) < 0 ) {
1938: cpyum(np0,mp); return;
1939: } else {
1940: l1 = (struct p_pair *)MALLOC(sizeof(struct p_pair));
1941: l1->p0 = np0; l1->p1 = np1;
1942: for ( l = list, lprev = 0, d = DEG(np1);
1943: l && (DEG(l->p1) > d); lprev = l, l = l->next );
1944: if ( lprev ) {
1945: lprev->next = l1; l1->next = l;
1946: } else {
1947: l1->next = list; list = l1;
1948: }
1949: }
1950: }
1951: }
1952:
1953: void lnf_mod(mod,n,p0,p1,list,np0,np1)
1954: int mod,n;
1955: UM p0,p1;
1956: struct p_pair *list;
1957: UM np0,np1;
1958: {
1959: int inv,h,d1;
1960: UM t0,t1,s0,s1;
1961: struct p_pair *l;
1962:
1963: cpyum(p0,np0); cpyum(p1,np1);
1964: t0 = W_UMALLOC(n); t1 = W_UMALLOC(n);
1965: s0 = W_UMALLOC(n); s1 = W_UMALLOC(n);
1966: for ( l = list; l; l = l->next ) {
1967: d1 = DEG(np1);
1968: if ( d1 == DEG(l->p1) ) {
1969: inv = invm((mod-COEF(l->p1)[d1])%mod,mod);
1970: h = dmar(COEF(np1)[d1],inv,0,mod);
1971: mulsum(mod,l->p0,h,t0); addum(mod,np0,t0,s0); cpyum(s0,np0);
1972: mulsum(mod,l->p1,h,t1); addum(mod,np1,t1,s1); cpyum(s1,np1);
1973: }
1974: }
1975: }
1976:
1977: int find_root(mod,p,root)
1978: int mod;
1979: UM p;
1980: int *root;
1981: {
1982: UM *r;
1983: int i,j;
1984:
1985: r = ALLOCA((DEG(p)+1)*sizeof(UM));
1986: ddd(mod,p,r);
1987: for ( i = 0, j = 0; r[i]; i++ )
1988: if ( DEG(r[i]) == 1 )
1989: root[j++] = (mod - COEF(r[i])[0]) % mod;
1990: return j;
1991: }
1992:
1993: void showum(p)
1994: UM p;
1995: {
1996: int i;
1997: int *c;
1998:
1999: for ( i = DEG(p), c = COEF(p); i >= 0; i-- )
2000: if ( c[i] )
2001: printf("+%dx^%d",c[i],i);
2002: printf("\n");
2003: }
2004:
2005: void showumat(mat,n)
2006: int **mat;
2007: int n;
2008: {
2009: int i,j;
2010:
2011: for ( i = 0; i < n; i++ ) {
2012: for ( j = 0; j < n; j++ )
2013: printf("%d ",mat[i][j]);
2014: printf("\n");
2015: }
2016: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>