![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
/*
3 *
4 *本程序采用链表存储多项式,实现了EUCLID GF(2)内多项式求逆算法
5 *并测试了一个例子:
6 * mx=1x^8+1x^4+1x^3+1x+1
7 * bx=1x^7+1x+1
8 * 输出结果:have inverse element! 1x^7
9 */
10 #include < stdio.h >
11 #include < malloc.h >
12 #define MOD 2
13 struct node
14 {
15 int data; // 系数
16 int power; // 指数
17 node * next;
18 };
19 // 全局变量
20 struct node * A1x, * A2x, * A3x, * B1x, * B2x, * B3x;
21
22 // 函数声明
23 void creatList( struct node ** head); // 创建链表
24 void destoryList( struct node * head); // 销毁链表
25 void PrintList( struct node * head); // 打印链表多项式
26 void ListCopy( struct node * headA, struct node * headB); // 复制链表
27 void checkListEmpty( struct node * head); // 检查链表多项式,若存在0项(系数为零),则清除之;若多项式为空,则添加一0结点
28 struct node * polynomeAdd( struct node * polynomeA, struct node * polynomeB); // 普通多项式加法,返回结果链表
29 struct node * polynomeSub( struct node * polynomeA, struct node * polynomeB); // 普通多项式减法,返回结果链表
30 struct node * polynomeMultiply( struct node * polynomeA, struct node * polynomeB); // 普通多项式乘法,返回结果链表
31 struct node * polynomeDivide( struct node * polynomeA, struct node * polynomeB); // GF(2)内的多项式除法,返回结果链表
32 struct node * ModPolynomeAdd( int mod, struct node * polynomeA, struct node * polynomeB); // 模数为mod的多项式加法,返回结果链表
33 struct node * ModPolynomeSub( int mod, struct node * polynomeA, struct node * polynomeB); // 模数为mod的多项式减法,返回结果链表
34 struct node * ModPolynomeMultiply( int mod, struct node * polynomeA, struct node * polynomeB); // 模数为mod的多项式乘法,返回结果链表
35 void EUCLIDBody( struct node * inverseElement, struct node * mx, struct node * bx); // EUCLID算法迭代函数体
36 void EUCLID( struct node * mx, struct node * bx); // EUCLID算法
37
38
39
40 void creatList( struct node ** head)
41 {
42 * head = ( struct node * )malloc( sizeof ( struct node));
43 ( * head) -> next = NULL;
44 }
45 void destoryList( struct node * head) // 释放链表所占空间,但保留头结点
46 { // 释放链表所占空间
47 struct node * p, * q;
48 if (head == NULL)
49 {
50 return ;
51 }
52 p = head -> next;
53 while (p != NULL)
54 {
55 q = p -> next;
56 free(p);
57 p = q;
58 }
59 }
60
61 void PrintList( struct node * head)
62 {
63 struct node * p;
64 p = head -> next;
65 while (p != NULL)
66 {
67 if ( 0 == p -> power)
68 {
69 if (p -> next == NULL)
70 {
71 printf( " %d " ,p -> data);
72 }
73 else
74 {
75 printf( " %d+ " ,p -> data);
76 }
77 }
78 else if ( 1 == p -> power)
79 {
80 if (p -> next == NULL)
81 {
82 printf( " %dx " ,p -> data);
83 }
84 else
85 {
86 printf( " %dx+ " ,p -> data);
87 }
88 }
89 else
90 {
91 if (p -> next == NULL)
92 {
93 printf( " %dx^%d " ,p -> data,p -> power);
94 }
95 else
96 {
97 printf( " %dx^%d+ " ,p -> data,p -> power);
98 }
99 }
100 p = p -> next;
101 }
102 printf( " \n " );
103 }
104 void ListCopy( struct node * headA, struct node * headB)
105 {
106
107 if (headA -> next != NULL) // 若headA为非空链表,则先释放掉其空间
108 {
109 destoryList(headA);
110 }
111
112 struct node * p, * q, * temp;
113 p = headA;
114 q = headB;
115 p -> data = q -> data;
116
117 q = headB -> next;
118 while (q != NULL)
119 {
120 temp = ( struct node * )malloc( sizeof ( struct node));
121 temp -> data = q -> data;
122 temp -> power = q -> power;
123 temp -> next = NULL;
124 p -> next = temp;
125
126 p = p -> next;
127 q = q -> next;
128 }
129 }
130 void checkListEmpty( struct node * head)
131 {
132 struct node * p, * pre;
133 pre = head;
134 p = head -> next;
135 while (p != NULL)
136 {
137 if (p -> data == 0 )
138 {
139 pre -> next = p -> next;
140 free(p);
141 p = pre -> next;
142 }
143 else
144 {
145 pre = p;
146 p = p -> next;
147 }
148 }
149
150 struct node * temp;
151 temp = ( struct node * )malloc( sizeof ( struct node));
152 temp -> data = 0 ;
153 temp -> power = 0 ;
154 temp -> next = NULL;
155
156 if (head -> next == NULL)
157 {
158 head -> next = temp;
159 return ;
160 }
161 }
162
163 struct node * polynomeAdd( struct node * polynomeA, struct node * polynomeB)
164 {
165 struct node * p, * q;
166
167 struct node * temp;
168
169 struct node * r;
170 struct node * result;
171 creatList( & result);
172
173 p = polynomeA -> next;
174 q = polynomeB -> next;
175 r = result;
176 while (p != NULL && q != NULL)
177 {
178 if (p -> power > q -> power)
179 {
180 temp = ( struct node * )malloc( sizeof ( struct node));
181 temp -> data = p -> data;
182 temp -> power = p -> power;
183 temp -> next = NULL;
184 r -> next = temp;
185 r = r -> next;
186
187 p = p -> next;
188 }
189 else if (p -> power < q -> power)
190 {
191 temp = ( struct node * )malloc( sizeof ( struct node));
192 temp -> data = q -> data;
193 temp -> power = q -> power;
194 temp -> next = NULL;
195 r -> next = temp;
196 r = r -> next;
197
198 q = q -> next;
199 }
200 else // p->power==q->power
201 {
202 temp = ( struct node * )malloc( sizeof ( struct node));
203 temp -> data = p -> data + q -> data;
204 temp -> power = p -> power;
205 temp -> next = NULL;
206 r -> next = temp;
207 r = r -> next;
208
209 p = p -> next;
210 q = q -> next;
211 }
212 }
213 if (p == NULL)
214 {
215 while (q != NULL)
216 {
217 temp = ( struct node * )malloc( sizeof ( struct node));
218 temp -> data = q -> data;
219 temp -> power = q -> power;
220 temp -> next = NULL;
221 r -> next = temp;
222 r = r -> next;
223
224 q = q -> next;
225 }
226 }
227 if (q == NULL)
228 {
229 while (p != NULL)
230 {
231 temp = ( struct node * )malloc( sizeof ( struct node));
232 temp -> data = p -> data;
233 temp -> power = p -> power;
234 temp -> next = NULL;
235 r -> next = temp;
236 r = r -> next;
237
238 p = p -> next;
239 }
240 }
241 checkListEmpty(result); // 判断链表是不是为空的处理函数
242 return result;
243 }
244 struct node * polynomeSub( struct node * polynomeA, struct node * polynomeB)
245 {
246 struct node * p, * q;
247
248 struct node * temp;
249
250 struct node * r;
251 struct node * result;
252 creatList( & result);
253
254 p = polynomeA -> next;
255 q = polynomeB -> next;
256 r = result;
257 while (p != NULL && q != NULL)
258 {
259 if (p -> power > q -> power)
260 {
261 temp = ( struct node * )malloc( sizeof ( struct node));
262 temp -> data = p -> data;
263 temp -> power = p -> power;
264 temp -> next = NULL;
265 r -> next = temp;
266 r = r -> next;
267
268 p = p -> next;
269 }
270 else if (p -> power < q -> power)
271 {
272 temp = ( struct node * )malloc( sizeof ( struct node));
273 temp -> data = 0 - q -> data;
274 temp -> power = q -> power;
275 temp -> next = NULL;
276 r -> next = temp;
277 r = r -> next;
278
279 q = q -> next;
280 }
281 else // p->power==q->power
282 {
283 if ( 0 != p -> data - q -> data)
284 {
285 temp = ( struct node * )malloc( sizeof ( struct node));
286 temp -> data = p -> data - q -> data;
287 temp -> power = p -> power;
288 temp -> next = NULL;
289 r -> next = temp;
290 r = r -> next;
291 }
292 p = p -> next;
293 q = q -> next;
294 }
295 }
296 if (p == NULL)
297 {
298 while (q != NULL)
299 {
300 temp = ( struct node * )malloc( sizeof ( struct node));
301 temp -> data = 0 - q -> data;
302 temp -> power = q -> power;
303 temp -> next = NULL;
304 r -> next = temp;
305 r = r -> next;
306
307 q = q -> next;
308 }
309 }
310 if (q == NULL)
311 {
312 while (p != NULL)
313 {
314 temp = ( struct node * )malloc( sizeof ( struct node));
315 temp -> data = p -> data;
316 temp -> power = p -> power;
317 temp -> next = NULL;
318 r -> next = temp;
319 r = r -> next;
320
321 p = p -> next;
322 }
323 }
324
325 checkListEmpty(result); // 判断链表是不是为空的处理函数
326 return result;
327 }
328 struct node * polynomeMultiply( struct node * polynomeA, struct node * polynomeB)
329 {
330 struct node * result;
331 creatList( & result);
332
333 struct node * temp;
334 creatList( & temp);
335 ListCopy(temp,polynomeA);
336
337 struct node * q;
338 struct node * p;
339
340 p = temp -> next;
341 q = polynomeB -> next;
342
343 while (q != NULL)
344 {
345 while (p != NULL)
346 {
347 p -> data *= q -> data;
348 p -> power += q -> power;
349 p = p -> next;
350 }
351 ListCopy(result,polynomeAdd(result,temp));
352 ListCopy(temp,polynomeA);
353
354 p = temp -> next;
355 q = q -> next;
356 }
357 checkListEmpty(result); // 判断链表是不是为空的处理函数
358 return result;
359 }
360
361 struct node * ModPolynomeAdd( int mod, struct node * polynomeA, struct node * polynomeB)
362 {
363 struct node * result;
364 creatList( & result);
365 ListCopy(result,polynomeAdd(polynomeA,polynomeB));
366
367 struct node * r;
368 r = result -> next;
369
370 while (r != NULL)
371 {
372 r -> data = r -> data % mod;
373 if (r -> data < 0 )
374 {
375 r -> data += mod;
376 }
377
378 r = r -> next;
379 }
380 checkListEmpty(result); // 判断链表是不是为空的处理函数
381 return result;
382 }
383
384 struct node * ModPolynomeSub( int mod, struct node * polynomeA, struct node * polynomeB)
385 {
386 struct node * result;
387 creatList( & result);
388 ListCopy(result,polynomeSub(polynomeA,polynomeB));
389
390 struct node * r;
391 r = result -> next;
392
393 while (r != NULL)
394 {
395 r -> data = r -> data % mod;
396 if (r -> data < 0 )
397 {
398 r -> data += mod;
399 }
400
401 r = r -> next;
402 }
403 checkListEmpty(result); // 判断链表是不是为空的处理函数
404 return result;
405 }
406 struct node * ModPolynomeMultiply( int mod, struct node * polynomeA, struct node * polynomeB)
407 {
408 struct node * result;
409 creatList( & result);
410 ListCopy(result,polynomeMultiply(polynomeA,polynomeB));
411
412 struct node * r;
413 r = result -> next;
414
415 while (r != NULL)
416 {
417 r -> data = r -> data % mod;
418 if (r -> data < 0 )
419 {
420 r -> data += mod;
421 }
422
423 r = r -> next;
424 }
425 checkListEmpty(result); // 判断链表是不是为空的处理函数
426 return result;
427 }
428 struct node * polynomeDivide( struct node * polynomeA, struct node * polynomeB)
429 {
430 // 本函数只考虑模数是2的情况,即多项式系数不大于1
431
432 struct node * result;
433 creatList( & result);
434 struct node * rtemp;
435 struct node * r;
436 r = result;
437
438 if (polynomeA -> next -> power < polynomeB -> next -> power)
439 {
440 rtemp = ( struct node * )malloc( sizeof ( struct node));
441 rtemp -> data = 0 ;
442 rtemp -> power = 0 ;
443 rtemp -> next = NULL;
444 r -> next = rtemp;
445
446 return result;
447 }
448
449 struct node * A, * B;
450 creatList( & A);
451 creatList( & B);
452 ListCopy(A,polynomeA);
453 ListCopy(B,polynomeB);
454
455 int power = 0 ;
456
457 while (A -> next -> power >= B -> next -> power)
458 {
459 power = A -> next -> power - B -> next -> power;
460
461 struct node * temp;
462 creatList( & temp);
463 ListCopy(temp,polynomeB);
464
465 struct node * t;
466 t = temp -> next;
467 while (t != NULL)
468 {
469 t -> power += power;
470 t = t -> next;
471 }
472 ListCopy(A,ModPolynomeSub( 2 ,A,temp));
473
474 rtemp = ( struct node * )malloc( sizeof ( struct node));
475 rtemp -> data = 1 ;
476 rtemp -> power = power;
477 rtemp -> next = NULL;
478 r -> next = rtemp;
479 r = r -> next;
480 }
481 checkListEmpty(result); // 判断链表是不是为空的处理函数
482 return result;
483 }
484
485 void EUCLIDBody( struct node * inverseElement, struct node * mx, struct node * bx)
486 {
487 struct node * T1x, * T2x, * T3x, * Qx;
488 creatList( & T1x);
489 creatList( & T2x);
490 creatList( & T3x);
491 creatList( & Qx);
492
493
494 if (B3x -> next -> next == NULL && 0 == B3x -> next -> data && 0 == B3x -> next -> power)
495 {
496 printf( " no inverse element!\n " );
497 return ;
498 }
499 if (B3x -> next -> next == NULL && 1 == B3x -> next -> data && 0 == B3x -> next -> power)
500 {
501 printf( " have inverse element!\n " );
502 ListCopy(inverseElement,B2x);
503 return ;
504 }
505
506 // Q(x)=quotient of A3(x)/B3(x)
507 ListCopy(Qx,polynomeDivide(A3x,B3x));
508
509 // [T1(x),T2(x),T3(x)]<-[A1(x)-Q(x)B1(x),A2(x)-Q(x)B2(x),A3(x)-Q(x)B3(x)]
510 ListCopy(T1x,ModPolynomeSub(MOD,A1x,ModPolynomeMultiply( 2 ,Qx,B1x)));
511 ListCopy(T2x,ModPolynomeSub(MOD,A2x,ModPolynomeMultiply( 2 ,Qx,B2x)));
512 ListCopy(T3x,ModPolynomeSub(MOD,A3x,ModPolynomeMultiply( 2 ,Qx,B3x)));
513
514 // [A1(x),A2(x),A3(x)]<-[B1(x),B2(x),B3(x)]
515 ListCopy(A1x,B1x);
516 ListCopy(A2x,B2x);
517 ListCopy(A3x,B3x);
518
519 // [B1(x),B2(x),B3(x)]<-[T1(x),T2(x),T3(x)]
520 ListCopy(B1x,T1x);
521 ListCopy(B2x,T2x);
522 ListCopy(B3x,T3x);
523
524 destoryList(T1x);
525 destoryList(T2x);
526 destoryList(T3x);
527 EUCLIDBody(inverseElement,A3x,B3x);
528 }
529 void EUCLID( struct node * mx, struct node * bx)
530 {
531 creatList( & A1x);
532 creatList( & A2x);
533 creatList( & A3x);
534 creatList( & B1x);
535 creatList( & B2x);
536 creatList( & B3x);
537
538 struct node * temp = ( struct node * )malloc( sizeof ( struct node));
539 // [A1(x),A2(x),A3(x)]<-[1,0,m(x)]
540 temp -> data = 1 ;
541 temp -> power = 0 ;
542 temp -> next = NULL;
543 A1x -> next = temp;
544 A1x -> data ++ ;
545
546 temp = ( struct node * )malloc( sizeof ( struct node));
547 temp -> data = 0 ;
548 temp -> power = 0 ;
549 temp -> next = NULL;
550 A2x -> next = temp;
551 A2x -> data ++ ;
552
553 ListCopy(A3x,mx);
554
555 // [B1(x),B2(x),B3(x)]<-[0,1,b(x)]
556 temp = ( struct node * )malloc( sizeof ( struct node));
557 temp -> data = 0 ;
558 temp -> power = 0 ;
559 temp -> next = NULL;
560 B1x -> next = temp;
561 B1x -> data ++ ;
562
563 temp = ( struct node * )malloc( sizeof ( struct node));
564 temp -> data = 1 ;
565 temp -> power = 0 ;
566 temp -> next = NULL;
567 B2x -> next = temp;
568 B2x -> data ++ ;
569
570 ListCopy(B3x,bx);
571
572 struct node * inverseElement;
573 creatList( & inverseElement);
574 EUCLIDBody(inverseElement,mx,bx);
575 PrintList(inverseElement);
576 }
577 void main()
578 {
579 struct node * mx, * bx, * temp, * p, * q;
580 creatList( & mx);
581 creatList( & bx);
582
583 // 初始化mx=x^8+x^4+x^3+x+1
584 p = mx;
585
586 temp = ( struct node * )malloc( sizeof ( struct node));
587 temp -> data = 1 ;
588 temp -> power = 8 ;
589 temp -> next = NULL;
590 p -> next = temp;
591 p = p -> next;
592
593 temp = ( struct node * )malloc( sizeof ( struct node));
594 temp -> data = 1 ;
595 temp -> power = 4 ;
596 temp -> next = NULL;
597 p -> next = temp;
598 p = p -> next;
599
600 temp = ( struct node * )malloc( sizeof ( struct node));
601 temp -> data = 1 ;
602 temp -> power = 3 ;
603 temp -> next = NULL;
604 p -> next = temp;
605 p = p -> next;
606
607 temp = ( struct node * )malloc( sizeof ( struct node));
608 temp -> data = 1 ;
609 temp -> power = 1 ;
610 temp -> next = NULL;
611 p -> next = temp;
612 p = p -> next;
613
614 temp = ( struct node * )malloc( sizeof ( struct node));
615 temp -> data = 1 ;
616 temp -> power = 0 ;
617 temp -> next = NULL;
618 p -> next = temp;
619
620
621 // 初始化bx=x^7+x+1
622 q = bx;
623
624 temp = ( struct node * )malloc( sizeof ( struct node));
625 temp -> data = 1 ;
626 temp -> power = 7 ;
627 temp -> next = NULL;
628 q -> next = temp;
629 q = q -> next;
630
631 temp = ( struct node * )malloc( sizeof ( struct node));
632 temp -> data = 1 ;
633 temp -> power = 1 ;
634 temp -> next = NULL;
635 q -> next = temp;
636 q = q -> next;
637
638 temp = ( struct node * )malloc( sizeof ( struct node));
639 temp -> data = 1 ;
640 temp -> power = 0 ;
641 temp -> next = NULL;
642 q -> next = temp;
643
644 printf( " mx: " );
645 PrintList(mx);
646 printf( " bx: " );
647 PrintList(bx);
648
649 // EUCLID算法求逆元
650 EUCLID(mx,bx);
651
652 }
3 *
4 *本程序采用链表存储多项式,实现了EUCLID GF(2)内多项式求逆算法
5 *并测试了一个例子:
6 * mx=1x^8+1x^4+1x^3+1x+1
7 * bx=1x^7+1x+1
8 * 输出结果:have inverse element! 1x^7
9 */
10 #include < stdio.h >
11 #include < malloc.h >
12 #define MOD 2
13 struct node
14 {
15 int data; // 系数
16 int power; // 指数
17 node * next;
18 };
19 // 全局变量
20 struct node * A1x, * A2x, * A3x, * B1x, * B2x, * B3x;
21
22 // 函数声明
23 void creatList( struct node ** head); // 创建链表
24 void destoryList( struct node * head); // 销毁链表
25 void PrintList( struct node * head); // 打印链表多项式
26 void ListCopy( struct node * headA, struct node * headB); // 复制链表
27 void checkListEmpty( struct node * head); // 检查链表多项式,若存在0项(系数为零),则清除之;若多项式为空,则添加一0结点
28 struct node * polynomeAdd( struct node * polynomeA, struct node * polynomeB); // 普通多项式加法,返回结果链表
29 struct node * polynomeSub( struct node * polynomeA, struct node * polynomeB); // 普通多项式减法,返回结果链表
30 struct node * polynomeMultiply( struct node * polynomeA, struct node * polynomeB); // 普通多项式乘法,返回结果链表
31 struct node * polynomeDivide( struct node * polynomeA, struct node * polynomeB); // GF(2)内的多项式除法,返回结果链表
32 struct node * ModPolynomeAdd( int mod, struct node * polynomeA, struct node * polynomeB); // 模数为mod的多项式加法,返回结果链表
33 struct node * ModPolynomeSub( int mod, struct node * polynomeA, struct node * polynomeB); // 模数为mod的多项式减法,返回结果链表
34 struct node * ModPolynomeMultiply( int mod, struct node * polynomeA, struct node * polynomeB); // 模数为mod的多项式乘法,返回结果链表
35 void EUCLIDBody( struct node * inverseElement, struct node * mx, struct node * bx); // EUCLID算法迭代函数体
36 void EUCLID( struct node * mx, struct node * bx); // EUCLID算法
37
38
39
40 void creatList( struct node ** head)
41 {
42 * head = ( struct node * )malloc( sizeof ( struct node));
43 ( * head) -> next = NULL;
44 }
45 void destoryList( struct node * head) // 释放链表所占空间,但保留头结点
46 { // 释放链表所占空间
47 struct node * p, * q;
48 if (head == NULL)
49 {
50 return ;
51 }
52 p = head -> next;
53 while (p != NULL)
54 {
55 q = p -> next;
56 free(p);
57 p = q;
58 }
59 }
60
61 void PrintList( struct node * head)
62 {
63 struct node * p;
64 p = head -> next;
65 while (p != NULL)
66 {
67 if ( 0 == p -> power)
68 {
69 if (p -> next == NULL)
70 {
71 printf( " %d " ,p -> data);
72 }
73 else
74 {
75 printf( " %d+ " ,p -> data);
76 }
77 }
78 else if ( 1 == p -> power)
79 {
80 if (p -> next == NULL)
81 {
82 printf( " %dx " ,p -> data);
83 }
84 else
85 {
86 printf( " %dx+ " ,p -> data);
87 }
88 }
89 else
90 {
91 if (p -> next == NULL)
92 {
93 printf( " %dx^%d " ,p -> data,p -> power);
94 }
95 else
96 {
97 printf( " %dx^%d+ " ,p -> data,p -> power);
98 }
99 }
100 p = p -> next;
101 }
102 printf( " \n " );
103 }
104 void ListCopy( struct node * headA, struct node * headB)
105 {
106
107 if (headA -> next != NULL) // 若headA为非空链表,则先释放掉其空间
108 {
109 destoryList(headA);
110 }
111
112 struct node * p, * q, * temp;
113 p = headA;
114 q = headB;
115 p -> data = q -> data;
116
117 q = headB -> next;
118 while (q != NULL)
119 {
120 temp = ( struct node * )malloc( sizeof ( struct node));
121 temp -> data = q -> data;
122 temp -> power = q -> power;
123 temp -> next = NULL;
124 p -> next = temp;
125
126 p = p -> next;
127 q = q -> next;
128 }
129 }
130 void checkListEmpty( struct node * head)
131 {
132 struct node * p, * pre;
133 pre = head;
134 p = head -> next;
135 while (p != NULL)
136 {
137 if (p -> data == 0 )
138 {
139 pre -> next = p -> next;
140 free(p);
141 p = pre -> next;
142 }
143 else
144 {
145 pre = p;
146 p = p -> next;
147 }
148 }
149
150 struct node * temp;
151 temp = ( struct node * )malloc( sizeof ( struct node));
152 temp -> data = 0 ;
153 temp -> power = 0 ;
154 temp -> next = NULL;
155
156 if (head -> next == NULL)
157 {
158 head -> next = temp;
159 return ;
160 }
161 }
162
163 struct node * polynomeAdd( struct node * polynomeA, struct node * polynomeB)
164 {
165 struct node * p, * q;
166
167 struct node * temp;
168
169 struct node * r;
170 struct node * result;
171 creatList( & result);
172
173 p = polynomeA -> next;
174 q = polynomeB -> next;
175 r = result;
176 while (p != NULL && q != NULL)
177 {
178 if (p -> power > q -> power)
179 {
180 temp = ( struct node * )malloc( sizeof ( struct node));
181 temp -> data = p -> data;
182 temp -> power = p -> power;
183 temp -> next = NULL;
184 r -> next = temp;
185 r = r -> next;
186
187 p = p -> next;
188 }
189 else if (p -> power < q -> power)
190 {
191 temp = ( struct node * )malloc( sizeof ( struct node));
192 temp -> data = q -> data;
193 temp -> power = q -> power;
194 temp -> next = NULL;
195 r -> next = temp;
196 r = r -> next;
197
198 q = q -> next;
199 }
200 else // p->power==q->power
201 {
202 temp = ( struct node * )malloc( sizeof ( struct node));
203 temp -> data = p -> data + q -> data;
204 temp -> power = p -> power;
205 temp -> next = NULL;
206 r -> next = temp;
207 r = r -> next;
208
209 p = p -> next;
210 q = q -> next;
211 }
212 }
213 if (p == NULL)
214 {
215 while (q != NULL)
216 {
217 temp = ( struct node * )malloc( sizeof ( struct node));
218 temp -> data = q -> data;
219 temp -> power = q -> power;
220 temp -> next = NULL;
221 r -> next = temp;
222 r = r -> next;
223
224 q = q -> next;
225 }
226 }
227 if (q == NULL)
228 {
229 while (p != NULL)
230 {
231 temp = ( struct node * )malloc( sizeof ( struct node));
232 temp -> data = p -> data;
233 temp -> power = p -> power;
234 temp -> next = NULL;
235 r -> next = temp;
236 r = r -> next;
237
238 p = p -> next;
239 }
240 }
241 checkListEmpty(result); // 判断链表是不是为空的处理函数
242 return result;
243 }
244 struct node * polynomeSub( struct node * polynomeA, struct node * polynomeB)
245 {
246 struct node * p, * q;
247
248 struct node * temp;
249
250 struct node * r;
251 struct node * result;
252 creatList( & result);
253
254 p = polynomeA -> next;
255 q = polynomeB -> next;
256 r = result;
257 while (p != NULL && q != NULL)
258 {
259 if (p -> power > q -> power)
260 {
261 temp = ( struct node * )malloc( sizeof ( struct node));
262 temp -> data = p -> data;
263 temp -> power = p -> power;
264 temp -> next = NULL;
265 r -> next = temp;
266 r = r -> next;
267
268 p = p -> next;
269 }
270 else if (p -> power < q -> power)
271 {
272 temp = ( struct node * )malloc( sizeof ( struct node));
273 temp -> data = 0 - q -> data;
274 temp -> power = q -> power;
275 temp -> next = NULL;
276 r -> next = temp;
277 r = r -> next;
278
279 q = q -> next;
280 }
281 else // p->power==q->power
282 {
283 if ( 0 != p -> data - q -> data)
284 {
285 temp = ( struct node * )malloc( sizeof ( struct node));
286 temp -> data = p -> data - q -> data;
287 temp -> power = p -> power;
288 temp -> next = NULL;
289 r -> next = temp;
290 r = r -> next;
291 }
292 p = p -> next;
293 q = q -> next;
294 }
295 }
296 if (p == NULL)
297 {
298 while (q != NULL)
299 {
300 temp = ( struct node * )malloc( sizeof ( struct node));
301 temp -> data = 0 - q -> data;
302 temp -> power = q -> power;
303 temp -> next = NULL;
304 r -> next = temp;
305 r = r -> next;
306
307 q = q -> next;
308 }
309 }
310 if (q == NULL)
311 {
312 while (p != NULL)
313 {
314 temp = ( struct node * )malloc( sizeof ( struct node));
315 temp -> data = p -> data;
316 temp -> power = p -> power;
317 temp -> next = NULL;
318 r -> next = temp;
319 r = r -> next;
320
321 p = p -> next;
322 }
323 }
324
325 checkListEmpty(result); // 判断链表是不是为空的处理函数
326 return result;
327 }
328 struct node * polynomeMultiply( struct node * polynomeA, struct node * polynomeB)
329 {
330 struct node * result;
331 creatList( & result);
332
333 struct node * temp;
334 creatList( & temp);
335 ListCopy(temp,polynomeA);
336
337 struct node * q;
338 struct node * p;
339
340 p = temp -> next;
341 q = polynomeB -> next;
342
343 while (q != NULL)
344 {
345 while (p != NULL)
346 {
347 p -> data *= q -> data;
348 p -> power += q -> power;
349 p = p -> next;
350 }
351 ListCopy(result,polynomeAdd(result,temp));
352 ListCopy(temp,polynomeA);
353
354 p = temp -> next;
355 q = q -> next;
356 }
357 checkListEmpty(result); // 判断链表是不是为空的处理函数
358 return result;
359 }
360
361 struct node * ModPolynomeAdd( int mod, struct node * polynomeA, struct node * polynomeB)
362 {
363 struct node * result;
364 creatList( & result);
365 ListCopy(result,polynomeAdd(polynomeA,polynomeB));
366
367 struct node * r;
368 r = result -> next;
369
370 while (r != NULL)
371 {
372 r -> data = r -> data % mod;
373 if (r -> data < 0 )
374 {
375 r -> data += mod;
376 }
377
378 r = r -> next;
379 }
380 checkListEmpty(result); // 判断链表是不是为空的处理函数
381 return result;
382 }
383
384 struct node * ModPolynomeSub( int mod, struct node * polynomeA, struct node * polynomeB)
385 {
386 struct node * result;
387 creatList( & result);
388 ListCopy(result,polynomeSub(polynomeA,polynomeB));
389
390 struct node * r;
391 r = result -> next;
392
393 while (r != NULL)
394 {
395 r -> data = r -> data % mod;
396 if (r -> data < 0 )
397 {
398 r -> data += mod;
399 }
400
401 r = r -> next;
402 }
403 checkListEmpty(result); // 判断链表是不是为空的处理函数
404 return result;
405 }
406 struct node * ModPolynomeMultiply( int mod, struct node * polynomeA, struct node * polynomeB)
407 {
408 struct node * result;
409 creatList( & result);
410 ListCopy(result,polynomeMultiply(polynomeA,polynomeB));
411
412 struct node * r;
413 r = result -> next;
414
415 while (r != NULL)
416 {
417 r -> data = r -> data % mod;
418 if (r -> data < 0 )
419 {
420 r -> data += mod;
421 }
422
423 r = r -> next;
424 }
425 checkListEmpty(result); // 判断链表是不是为空的处理函数
426 return result;
427 }
428 struct node * polynomeDivide( struct node * polynomeA, struct node * polynomeB)
429 {
430 // 本函数只考虑模数是2的情况,即多项式系数不大于1
431
432 struct node * result;
433 creatList( & result);
434 struct node * rtemp;
435 struct node * r;
436 r = result;
437
438 if (polynomeA -> next -> power < polynomeB -> next -> power)
439 {
440 rtemp = ( struct node * )malloc( sizeof ( struct node));
441 rtemp -> data = 0 ;
442 rtemp -> power = 0 ;
443 rtemp -> next = NULL;
444 r -> next = rtemp;
445
446 return result;
447 }
448
449 struct node * A, * B;
450 creatList( & A);
451 creatList( & B);
452 ListCopy(A,polynomeA);
453 ListCopy(B,polynomeB);
454
455 int power = 0 ;
456
457 while (A -> next -> power >= B -> next -> power)
458 {
459 power = A -> next -> power - B -> next -> power;
460
461 struct node * temp;
462 creatList( & temp);
463 ListCopy(temp,polynomeB);
464
465 struct node * t;
466 t = temp -> next;
467 while (t != NULL)
468 {
469 t -> power += power;
470 t = t -> next;
471 }
472 ListCopy(A,ModPolynomeSub( 2 ,A,temp));
473
474 rtemp = ( struct node * )malloc( sizeof ( struct node));
475 rtemp -> data = 1 ;
476 rtemp -> power = power;
477 rtemp -> next = NULL;
478 r -> next = rtemp;
479 r = r -> next;
480 }
481 checkListEmpty(result); // 判断链表是不是为空的处理函数
482 return result;
483 }
484
485 void EUCLIDBody( struct node * inverseElement, struct node * mx, struct node * bx)
486 {
487 struct node * T1x, * T2x, * T3x, * Qx;
488 creatList( & T1x);
489 creatList( & T2x);
490 creatList( & T3x);
491 creatList( & Qx);
492
493
494 if (B3x -> next -> next == NULL && 0 == B3x -> next -> data && 0 == B3x -> next -> power)
495 {
496 printf( " no inverse element!\n " );
497 return ;
498 }
499 if (B3x -> next -> next == NULL && 1 == B3x -> next -> data && 0 == B3x -> next -> power)
500 {
501 printf( " have inverse element!\n " );
502 ListCopy(inverseElement,B2x);
503 return ;
504 }
505
506 // Q(x)=quotient of A3(x)/B3(x)
507 ListCopy(Qx,polynomeDivide(A3x,B3x));
508
509 // [T1(x),T2(x),T3(x)]<-[A1(x)-Q(x)B1(x),A2(x)-Q(x)B2(x),A3(x)-Q(x)B3(x)]
510 ListCopy(T1x,ModPolynomeSub(MOD,A1x,ModPolynomeMultiply( 2 ,Qx,B1x)));
511 ListCopy(T2x,ModPolynomeSub(MOD,A2x,ModPolynomeMultiply( 2 ,Qx,B2x)));
512 ListCopy(T3x,ModPolynomeSub(MOD,A3x,ModPolynomeMultiply( 2 ,Qx,B3x)));
513
514 // [A1(x),A2(x),A3(x)]<-[B1(x),B2(x),B3(x)]
515 ListCopy(A1x,B1x);
516 ListCopy(A2x,B2x);
517 ListCopy(A3x,B3x);
518
519 // [B1(x),B2(x),B3(x)]<-[T1(x),T2(x),T3(x)]
520 ListCopy(B1x,T1x);
521 ListCopy(B2x,T2x);
522 ListCopy(B3x,T3x);
523
524 destoryList(T1x);
525 destoryList(T2x);
526 destoryList(T3x);
527 EUCLIDBody(inverseElement,A3x,B3x);
528 }
529 void EUCLID( struct node * mx, struct node * bx)
530 {
531 creatList( & A1x);
532 creatList( & A2x);
533 creatList( & A3x);
534 creatList( & B1x);
535 creatList( & B2x);
536 creatList( & B3x);
537
538 struct node * temp = ( struct node * )malloc( sizeof ( struct node));
539 // [A1(x),A2(x),A3(x)]<-[1,0,m(x)]
540 temp -> data = 1 ;
541 temp -> power = 0 ;
542 temp -> next = NULL;
543 A1x -> next = temp;
544 A1x -> data ++ ;
545
546 temp = ( struct node * )malloc( sizeof ( struct node));
547 temp -> data = 0 ;
548 temp -> power = 0 ;
549 temp -> next = NULL;
550 A2x -> next = temp;
551 A2x -> data ++ ;
552
553 ListCopy(A3x,mx);
554
555 // [B1(x),B2(x),B3(x)]<-[0,1,b(x)]
556 temp = ( struct node * )malloc( sizeof ( struct node));
557 temp -> data = 0 ;
558 temp -> power = 0 ;
559 temp -> next = NULL;
560 B1x -> next = temp;
561 B1x -> data ++ ;
562
563 temp = ( struct node * )malloc( sizeof ( struct node));
564 temp -> data = 1 ;
565 temp -> power = 0 ;
566 temp -> next = NULL;
567 B2x -> next = temp;
568 B2x -> data ++ ;
569
570 ListCopy(B3x,bx);
571
572 struct node * inverseElement;
573 creatList( & inverseElement);
574 EUCLIDBody(inverseElement,mx,bx);
575 PrintList(inverseElement);
576 }
577 void main()
578 {
579 struct node * mx, * bx, * temp, * p, * q;
580 creatList( & mx);
581 creatList( & bx);
582
583 // 初始化mx=x^8+x^4+x^3+x+1
584 p = mx;
585
586 temp = ( struct node * )malloc( sizeof ( struct node));
587 temp -> data = 1 ;
588 temp -> power = 8 ;
589 temp -> next = NULL;
590 p -> next = temp;
591 p = p -> next;
592
593 temp = ( struct node * )malloc( sizeof ( struct node));
594 temp -> data = 1 ;
595 temp -> power = 4 ;
596 temp -> next = NULL;
597 p -> next = temp;
598 p = p -> next;
599
600 temp = ( struct node * )malloc( sizeof ( struct node));
601 temp -> data = 1 ;
602 temp -> power = 3 ;
603 temp -> next = NULL;
604 p -> next = temp;
605 p = p -> next;
606
607 temp = ( struct node * )malloc( sizeof ( struct node));
608 temp -> data = 1 ;
609 temp -> power = 1 ;
610 temp -> next = NULL;
611 p -> next = temp;
612 p = p -> next;
613
614 temp = ( struct node * )malloc( sizeof ( struct node));
615 temp -> data = 1 ;
616 temp -> power = 0 ;
617 temp -> next = NULL;
618 p -> next = temp;
619
620
621 // 初始化bx=x^7+x+1
622 q = bx;
623
624 temp = ( struct node * )malloc( sizeof ( struct node));
625 temp -> data = 1 ;
626 temp -> power = 7 ;
627 temp -> next = NULL;
628 q -> next = temp;
629 q = q -> next;
630
631 temp = ( struct node * )malloc( sizeof ( struct node));
632 temp -> data = 1 ;
633 temp -> power = 1 ;
634 temp -> next = NULL;
635 q -> next = temp;
636 q = q -> next;
637
638 temp = ( struct node * )malloc( sizeof ( struct node));
639 temp -> data = 1 ;
640 temp -> power = 0 ;
641 temp -> next = NULL;
642 q -> next = temp;
643
644 printf( " mx: " );
645 PrintList(mx);
646 printf( " bx: " );
647 PrintList(bx);
648
649 // EUCLID算法求逆元
650 EUCLID(mx,bx);
651
652 }