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