Blame view

Giac_maj/giac-1.4.9/src/threaded.h 93.4 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
  /* -*- mode:C++ ; compile-command: "g++ -I.. -g -c threaded.cc" -*- */
  /*  Multivariate GCD for large data not covered by the heuristic GCD algo
   *  Copyright (C) 2000,2014 B. Parisse, Institut Fourier, 38402 St Martin d'Heres
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 3 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program. If not, see <http://www.gnu.org/licenses/>.
   */
  
  #ifndef _GIAC_THREADED_H_
  #define _GIAC_THREADED_H_
  #ifdef HAVE_CONFIG_H
  #include "config.h"
  #endif
  #include "first.h"
  #ifdef HAVE_UNISTD_H
  #include <unistd.h>
  #endif
  #if defined WIN32 && !defined CLOCK && !defined BESTA_OS
  #define CLOCK clock
  #endif
  
  /*
  #ifndef WIN32
  #if defined(__APPLE__) || defined(__FreeBSD__) || defined(VISUALC) | defined(__NetBSD__) 
  #else // was #ifndef __APPLE__
  #include <sys/sysinfo.h>
  #endif
  #endif // WIN32
  */
  #include <iostream>
  #include <algorithm>
  #include <stdexcept>
  #include "vector.h"
  #ifdef USTL
  #include <uheap.h>
  #include <umultimap.h>
  #endif
  #include <map>
  #include "monomial.h"
  #if defined HAVE_PTHREAD_H && defined HAVE_LIBPTHREAD
  #include <pthread.h>
  #endif
  
  #ifdef USE_GMP_REPLACEMENTS
  #undef HAVE_GMPXX_H
  #undef HAVE_LIBMPFR
  #endif
  
  #ifdef HAVE_GMPXX_H
  #include <gmpxx.h>
  #endif
  
  #if defined HAVE_SYS_TIME_H && !defined VISUALC13
  #include <time.h>
  //#else
  //#ifndef VISUALC13
  //#define clock_t int
  //#endif
  //#define CLOCK() 0
  #endif
  
  #ifndef NO_NAMESPACE_GIAC
  namespace giac {
  #endif // ndef NO_NAMESPACE_GIAC
    template<class U>
    inline unsigned sizeinbase2(U n){
      unsigned i=0;
      for (;n;++i){
        n >>= 1;
      }
      return i;
    }
    std::vector<int> operator % (const std::vector<int> & a,int modulo);
    std::vector<int> operator / (const std::vector<int> & v,const std::vector<int> & b);
    std::vector<int> operator % (const std::vector<int> & v,const std::vector<int> & b);
    bool operator > (const std::vector<int> & v,double q);
  
    class gen;
    class context;
    gen _heap_mult(const gen & g0,const context *);
    gen _modgcd_cachesize(const gen & g0,const context *);
    gen _debug_infolevel(const gen & g0,const context *);
    gen _background(const gen & g,const context *);
  
    // #define hashgcd_U unsigned int
    typedef unsigned int hashgcd_U; // replace with ulonglong for large index capacity on 32 bit CPU
  
  #ifdef HAVE_GMPXX_H
  
    inline bool is_zero(const mpz_class & a){
      return a==0;
    }
  
    mpz_class invmod(const mpz_class & a,int reduce);
  
    mpz_class smod(const mpz_class & a,int reduce);
  
  
    inline void type_operator_times(const mpz_class & a,const mpz_class & b,mpz_class & c){
      // cerr << gen(c) << " = " << gen(a) << "*" << gen(b) << std::endl;
      mpz_mul(c.get_mpz_t(),a.get_mpz_t(),b.get_mpz_t());
      // cerr << gen(c) << std::endl;
    }
  
    inline void type_operator_reduce(const mpz_class & a,const mpz_class & b,mpz_class & c,int reduce){
      mpz_mul(c.get_mpz_t(),a.get_mpz_t(),b.get_mpz_t());
      if (reduce){
        c %= reduce;
      }
    }
  
    inline void type_operator_plus_times(const mpz_class & a,const mpz_class & b,mpz_class & c){
      // cerr << gen(c) << " += " << gen(a) << "*" << gen(b) << std::endl;
      // c+=a*b
      mpz_addmul(c.get_mpz_t(),a.get_mpz_t(),b.get_mpz_t());
      // cerr << gen(c) << std::endl;
    }
  
    inline void type_operator_plus_times_reduce(const mpz_class & a,const mpz_class & b,mpz_class & c,int reduce){
      // c+=a*b % reduce;
      mpz_addmul(c.get_mpz_t(),a.get_mpz_t(),b.get_mpz_t());
      if (reduce){
        c %= reduce;
      }
    }
  
  #endif // HAVE__GMPXX_H
  
    int invmod(longlong a,int reduce);
  
  #ifdef INT128
    
    inline bool is_zero(const int128_t & a){
      return a==0;
    }
  
    int invmod(int128_t a,int reduce);
  
    int128_t smod(int128_t & a,int reduce);
  
    inline void type_operator_times(const int128_t & a,const int128_t & b,int128_t & c){
      c = a*b;
    }
  
    inline void type_operator_reduce(const int128_t & a,const int128_t & b,int128_t & c,int reduce){
      c =a*b;
      if (reduce)
        c %= reduce;
    }
  
    inline void type_operator_plus_times(const int128_t & a,const int128_t & b,int128_t & c){
      c+=a*b;
    }
  
    inline void type_operator_plus_times_reduce(const int128_t & a,const int128_t & b,int128_t & c,int reduce){
      c +=a*b;
      if (reduce)
        c %= reduce;
    }
  
  #endif
  
  
    bool is_zero(const std::vector<int> & v);
    void mulsmall(const std::vector<int>::const_iterator & ita0,const std::vector<int>::const_iterator & ita_end,const std::vector<int>::const_iterator & itb0,const std::vector<int>::const_iterator & itb_end,int modulo,std::vector<int> & new_coord);
    void mulext(const std::vector<int> & a,const std::vector<int> & b,const std::vector<int> & pmin,int modulo,std::vector<int> & res);
  
    // FIXME: put a #define in index.h if index_t=vector<int> and skip defs here
    // or make them as template
    std::vector<int> operator + (const std::vector<int> & a, const std::vector<int> & b);
    std::vector<int> operator - (const std::vector<int> & a, const std::vector<int> & b);
    std::vector<int> operator - (const std::vector<int> & a);
    inline void type_operator_times(const std::vector<int> & a,const std::vector<int> & b,std::vector<int> & c){
      c.clear();
  #ifndef NO_STDEXCEPT
      setsizeerr("type_operator_times ext");
  #endif
    }
  
    inline void type_operator_plus_times(const std::vector<int> & a,const std::vector<int> & b,std::vector<int> & c){
      c.clear();
  #ifndef NO_STDEXCEPT
      setsizeerr("type_operator_reduce ext");
  #endif
    }
  
  
    extern double heap_mult,modgcd_cachesize;
    // -1, -2, -3, force heap chains/multimap/map chains/heap multiplication
    // 0 always hashmap
    // >=1 use heap when product of number of monomials is >= value
  
    extern bool threads_allowed;
    extern int threads;
  
    extern int debug_infolevel;
    int invmod(int n,int modulo);
    int smod(int a,int b); // where b is assumed to be positive
    // v <- v*k % m
    void mulmod(std::vector<int> & v,int k,int m);
  
    inline void longlong2mpz(longlong i,mpz_t *mpzptr){
      int ii((long)i);
      if (i==ii)
        mpz_set_si(*mpzptr,ii);
      else {
        bool signe=(i<0);
        if (signe)
  	i=-i;
        unsigned int i1=i>>32;
        unsigned int i2=(unsigned int)i;
        mpz_set_ui(*mpzptr,i1); // was mpz_init_set_ui probably a BUG
        mpz_mul_2exp(*mpzptr,*mpzptr,32);
        mpz_add_ui(*mpzptr,*mpzptr,i2);
        if (signe)
  	mpz_neg(*mpzptr,*mpzptr);
      }
    }
  
    // tmp is an allocated mpz_t 
    inline void mpz2longlong(mpz_t * ptr,mpz_t * tmp,longlong & ans){
      int i=mpz_sgn(*ptr);
      if (i<0)
        mpz_neg(*ptr,*ptr);
      mpz_tdiv_q_2exp(*tmp,*ptr,31);
      ans=mpz_get_ui(*tmp);
      ans <<= 31;
      mpz_tdiv_r_2exp(*tmp,*ptr,31);
      ans += mpz_get_ui(*tmp);
      if (i<0){
        mpz_neg(*ptr,*ptr);
        ans = -ans;
      }
    }
  
  #ifdef INT128
    // tmp is an allocated mpz_t 
    inline void mpz2int128(mpz_t * ptr,mpz_t * tmp,int128_t & ans){
      int i=mpz_sgn(*ptr);
      if (i<0)
        mpz_neg(*ptr,*ptr);
      mpz_tdiv_q_2exp(*tmp,*ptr,93);
      ans=mpz_get_ui(*tmp);
      ans <<= 93;
      mpz_tdiv_q_2exp(*tmp,*ptr,62);
      mpz_tdiv_r_2exp(*tmp,*tmp,31);
      ans += (int128_t(mpz_get_ui(*tmp)) << 62);
      mpz_tdiv_q_2exp(*tmp,*ptr,31);
      mpz_tdiv_r_2exp(*tmp,*tmp,31);
      ans += (ulonglong(mpz_get_ui(*tmp))<<31);
      mpz_tdiv_r_2exp(*tmp,*ptr,31);
      ans += mpz_get_ui(*tmp);
      if (i<0){
        mpz_neg(*ptr,*ptr);
        ans = -ans;
      }
    }
  
  #endif
  
    class my_mpz {
    public:
      mpz_t ptr;
      my_mpz(){ mpz_init(ptr); }
      my_mpz(long l){ mpz_init_set_si(ptr,l); }
      my_mpz(const my_mpz & z){ mpz_init_set(ptr,z.ptr); }
      ~my_mpz() { mpz_clear(ptr); }
      my_mpz operator - () const { my_mpz tmp(*this); mpz_neg(tmp.ptr,tmp.ptr); return tmp;}
      my_mpz & operator = (const my_mpz & a) {mpz_set(ptr,a.ptr); return *this;}
    };
  
    my_mpz operator % (const my_mpz & a,const my_mpz & b);
    my_mpz operator + (const my_mpz & a,const my_mpz & b);
    my_mpz operator - (const my_mpz & a,const my_mpz & b);
    my_mpz operator * (const my_mpz & a,const my_mpz & b);
    my_mpz operator / (const my_mpz & a,const my_mpz & b);
    my_mpz invmod(const my_mpz & a,int reduce);
    my_mpz smod(const my_mpz & a,int reduce);
    my_mpz operator %= (my_mpz & a,const my_mpz & b);
    my_mpz operator += (my_mpz & a,const my_mpz & b);
    my_mpz operator -= (my_mpz & a,const my_mpz & b);
  
  
    inline bool is_zero(const my_mpz & a){
      return !mpz_sgn(a.ptr);
    }
  
    inline void type_operator_times(const my_mpz & a,const my_mpz & b,my_mpz & c){
      mpz_mul(c.ptr,a.ptr,b.ptr);
      // c=a*b;
    }
  
    inline void type_operator_reduce(const my_mpz & a,const my_mpz & b,my_mpz & c,int reduce){
      mpz_mul(c.ptr,a.ptr,b.ptr);
      if (reduce)
        mpz_fdiv_r_ui(c.ptr,c.ptr,reduce);
    }
  
    inline void type_operator_plus_times(const my_mpz & a,const my_mpz & b,my_mpz & c){
      // c+=a*b
      mpz_addmul(c.ptr,a.ptr,b.ptr);
    }
  
    inline void type_operator_plus_times_reduce(const my_mpz & a,const my_mpz & b,my_mpz & c,int reduce){
      // c+=a*b % reduce;
      mpz_addmul(c.ptr,a.ptr,b.ptr);
      if (reduce)
        mpz_fdiv_r_ui(c.ptr,c.ptr,reduce);
    }
  
    template<class T,class U>
    struct T_unsigned {
      T g;
      U u;
      T_unsigned(const T & myg,const U & myu): g(myg),u(myu) {};
      T_unsigned(): g(0),u(0) {};
      bool operator == (const T_unsigned<T,U> & tu) const { return g==tu.g && u==tu.u; }
      bool operator !=(const T_unsigned<T,U> & tu) const { return g==tu.g && u!=tu.u; }
    };
  
    // warning, < is > so that monomial ordering is ok after back-conversion
    template <class T,class U>
    inline bool operator < (const T_unsigned<T,U> & gu1,const T_unsigned<T,U> & gu2){
      return gu1.u > gu2.u;
    }
    typedef T_unsigned<int,unsigned> int_unsigned;
  
  #ifdef NSPIRE    
    template<class T,class U,class I>
    nio::ios_base<I> & operator << (nio::ios_base<I> & os, const std::vector< T_unsigned<T,U> > & v){  
      typename std::vector< T_unsigned<T,U> >::const_iterator it=v.begin(),itend=v.end();
      for (;it!=itend;++it){
        os << "(" << it->g << "," << it->u << "),";
      }
      return os << std::endl;
    }
  #else
    template<class T,class U>
    std::ostream & operator << (std::ostream & os, const std::vector< T_unsigned<T,U> > & v){  
      typename std::vector< T_unsigned<T,U> >::const_iterator it=v.begin(),itend=v.end();
      for (;it!=itend;++it){
        os << "(" << it->g << "," << it->u << "),";
      }
      return os << std::endl;
    }
  #endif
  
  #if defined VISUALC || defined __clang__
    template<class T>
    class vector_size64:public std::vector<T>{
    };
    template<class T>
    class vector_size32:public std::vector<T>{
    };
  #else
    // "smart" vector, T must be a type of size 64bits, sizeof(T)=8
    // and vector must be of size 192 bits, sizeof=24
    template<class T>
    class vector_size64 {
    public:
      longlong taille;
      T v0;
      T v1;
      vector_size64(){ taille=1;}
      vector_size64(size_t t){ 
        if (t>2){ 
  	taille=0; * (longlong *) &v0=0; * (longlong *) &v1=0; 
  	std::vector<T> tmp(t); 
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	std::swap(tmp,v); 
        }
        else { taille=2*t+1; * (longlong *) &v0=0; * (longlong *) &v1=0;}
      }
      vector_size64(size_t t,const T & elem){ 
        if (t>2){ 
  	taille=0; * (longlong *) &v0=0; * (longlong *) &v1=0; 
  	std::vector<T> tmp(t,elem); 
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	std::swap(tmp,v); 
        }
        else { taille=2*t+1; v0=elem; v1=elem;}
      }
      ~vector_size64(){
        if (taille%2==0){
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	std::vector<T> tmp;
  	std::swap(tmp,v);
        }
      }
      void push_back(T t){
        if (taille %2){
  	if (taille==5){
  	  std::vector<T> tmp(4);
  	  tmp.front()=v0;
  	  tmp[1]=v1;
  	  tmp[2]=t;
  	  tmp.pop_back();
  	  taille=0; * (longlong *) &v0=0; * (longlong *) &v1=0; 
  	  std::vector<T> & v = * (std::vector<T> *) &taille;
  	  std::swap(tmp,v);
  	  return;
  	}
  	taille += 2;
  	if (taille==5)
  	  v1=t;
  	else 
  	  v0=t;
        }
        else {
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	v.push_back(t);
        }
      }
      void pop_back(){
        if (taille%2)
  	taille -=2;
        else {
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	v.pop_back();
        }
      }
      void clear(){
        if (taille%2)
  	taille=1;
        else {
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	v.clear();
        }
      }
      T operator [] (size_t pos) const {
        if (taille%2)
  	return pos?v1:v0;
        else {
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	return v[pos];
        }
      }
      typename std::vector<T>::const_iterator begin() const {
        if (taille %2)
  	return typename std::vector<T>::const_iterator(&v0);
        else {
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	return v.begin();
        }
      }
      typename std::vector<T>::iterator begin() {
        if (taille %2)
  	return typename std::vector<T>::iterator(&v0);
        else {
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	return v.begin();
        }
      }
      typename std::vector<T>::const_iterator end() const {
        if (taille %2)
  	return typename std::vector<T>::const_iterator(&v0+taille/2);
        else {
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	return v.end();
        }
      }
      typename std::vector<T>::iterator end() {
        if (taille %2)
  	return typename std::vector<T>::iterator(&v0+taille/2);
        else {
  	std::vector<T> & v = * (std::vector<T> *) &taille;
  	return v.end();
        }
      }
    };
  
    // vector of type T of size 32 bits (sizeof==4), not too large
    template<class T>
    struct mytab{
      T * tab;
      unsigned int _size;
      unsigned int _capacity;
    };
  
    template<class T>
    class vector_size32 {
    public:
      struct {
        unsigned int taille;
        T v0;
        T v1;
        T v2;
      };
      vector_size32(){ taille=1;}
      vector_size32(size_t t){ 
        if (t>3){
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	mytabptr->tab = new T[t];
  	mytabptr->_size = mytabptr->_capacity = t;
  	int * ptr=(int *)mytabptr->tab;
  	for (size_t i=0;i<t;++ptr,++i)
  	  *ptr=0;
        }
        else { taille=2*t+1; * (int *) &v0=0; * (longlong *) &v1=0;}
      }
      vector_size32(size_t t,const T & elem){ 
        if (t>3){ 
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	mytabptr->tab = new T[t];
  	mytabptr->_size = mytabptr->_capacity = t;
  	T * ptr=mytabptr->tab;
  	for (size_t i=0;i<t;++ptr,++i)
  	  *ptr=elem;
        }
        else { taille=2*t+1; v2=v1=v0=elem;}
      }
      ~vector_size32(){
        if (taille%2==0){
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	delete [] mytabptr->tab;
        }
      }
      void push_back(T t){
        if (taille %2){
  	if (taille==7){
  	  T * ptr = new T[6];
  	  *ptr=v0; ++ptr;
  	  *ptr=v1; ++ptr;
  	  *ptr=v2; ++ptr;
  	  *ptr=t;
  	  mytab<T> * mytabptr = (mytab<T> *) this;
  	  mytabptr->tab = ptr-3;
  	  mytabptr->_size = 4;
  	  mytabptr->_capacity = 6;
  	  return;
  	}
  	taille += 2;
  	if (taille==7)
  	  v2=t;
  	else { 
  	  if (taille==5) v1=t; else v0=t; 
  	}
        }
        else {
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	if (mytabptr->_size>=mytabptr->_capacity){
  	  mytabptr->_capacity *=2;
  	  T * ptr = new T[mytabptr->_capacity];
  	  memcpy(ptr,mytabptr->tab,mytabptr->_size*sizeof(T));
  	  delete [] mytabptr->tab;
  	  mytabptr->tab=ptr;
  	}
  	mytabptr->tab[mytabptr->_size]=t;
  	++mytabptr->_size;
        }
      }
      void pop_back(){
        if (taille%2)
  	taille -=2;
        else {
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	--mytabptr->_size;
        }
      }
      void clear(){
        if (taille%2)
  	taille=1;
        else {
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	mytabptr->_size=0;
        }
      }
      T operator [] (size_t pos) const {
        if (taille%2)
  	return pos?((pos==2)?v2:v1):v0;
        else {
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	return mytabptr->tab[pos];
        }
      }
      typename std::vector<T>::const_iterator begin() const {
        if (taille %2)
  	return typename std::vector<T>::const_iterator(&v0);
        else {
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	return typename std::vector<T>::const_iterator(mytabptr->tab);
        }
      }
      typename std::vector<T>::iterator begin() {
        if (taille %2)
  	return typename std::vector<T>::iterator(&v0);
        else {
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	return typename std::vector<T>::iterator(mytabptr->tab);
        }
      }
      typename std::vector<T>::const_iterator end() const {
        if (taille %2)
  	return typename std::vector<T>::const_iterator(&v0+taille/2);
        else {
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	return typename std::vector<T>::const_iterator(mytabptr->_tab+mytabptr->_size);
        }
      }
      typename std::vector<T>::iterator end() {
        if (taille %2)
  	return typename std::vector<T>::iterator(&v0+taille/2);
        else {
  	mytab<T> * mytabptr = (mytab<T> *) this;
  	return typename std::vector<T>::iterator(mytabptr->tab+mytabptr->_size);
        }
      }
    };
  #endif
  
    class hash_function_unsigned_object {
    public:
      // inline size_t operator () (size_t x) const { return x; }
      inline size_t operator () (int x) const { return x; }
      inline size_t operator () (unsigned x) const { return x; }
      inline size_t operator () (long long unsigned x) const { return x%12345701; }
      inline size_t operator () (long long x) const { return x%12345701; }
      hash_function_unsigned_object() {};
    };
  
    bool mod_gcd(const std::vector< T_unsigned<int,hashgcd_U> > & p0,const std::vector< T_unsigned<int,hashgcd_U> > & q0,int modulo,std::vector< T_unsigned<int,hashgcd_U> > & pgcd, std::vector< T_unsigned<int,hashgcd_U> > & pcof, std::vector< T_unsigned<int,hashgcd_U> > & qcof,const std::vector<hashgcd_U> & vars, bool compute_cofactors,int nthreads);
  
    bool mod_gcd(const std::vector< T_unsigned<int,hashgcd_U> > & p_orig,const std::vector< T_unsigned<int,hashgcd_U> > & q_orig,int modulo,std::vector< T_unsigned<int,hashgcd_U> > & d, std::vector< T_unsigned<int,hashgcd_U> > & pcofactor, std::vector< T_unsigned<int,hashgcd_U> > & qcofactor,const std::vector<hashgcd_U> & vars, bool compute_pcofactor,bool compute_qcofactor,int nthreads);
  
    bool gcd(const std::vector< T_unsigned<gen,hashgcd_U> > & p_orig,const std::vector< T_unsigned<gen,hashgcd_U> > & q_orig,std::vector< T_unsigned<gen,hashgcd_U> > & d, std::vector< T_unsigned<gen,hashgcd_U> > & pcofactor, std::vector< T_unsigned<gen,hashgcd_U> > & qcofactor,const std::vector<hashgcd_U> & vars, bool compute_cofactors,int nthreads=1);
  
    bool gcd_ext(const std::vector< T_unsigned<gen,hashgcd_U> > & p_orig,const std::vector< T_unsigned<gen,hashgcd_U> > & q_orig,std::vector< T_unsigned<gen,hashgcd_U> > & d, std::vector< T_unsigned<gen,hashgcd_U> > & pcofactor, std::vector< T_unsigned<gen,hashgcd_U> > & qcofactor,const std::vector<hashgcd_U> & vars, bool compute_cofactors,int nthreads=1);
  
    template<class T,class U>
    void smalladd(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v){
      if (&v1==&v || &v2==&v){
        std::vector< T_unsigned<T,U> > tmp;
        smalladd(v1,v2,tmp);
        std::swap< std::vector< T_unsigned<T,U> > >(v,tmp);
        return;
      }
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1.begin(),it1end=v1.end(),it2=v2.begin(),it2end=v2.end();
      T g;
      v.clear();
      v.reserve((it1end-it1)+(it2end-it2)); // worst case
      for (;it1!=it1end && it2!=it2end;){
        if (it1->u==it2->u){
  	g=it1->g+it2->g;
  	if (!is_zero(g))
  	  v.push_back(T_unsigned<T,U>(g,it1->u));
  	++it1;
  	++it2;
        }
        else {
  	if (it1->u>it2->u){
  	  v.push_back(*it1);
  	  ++it1;
  	}
  	else {
  	  v.push_back(*it2);
  	  ++it2;
  	}
        }
      }
      for (;it1!=it1end;++it1)
        v.push_back(*it1);
      for (;it2!=it2end;++it2)
        v.push_back(*it2);
    }
  
    template<class T,class U,class R>
    void smalladd(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,const R & reduce){
      if (&v1==&v || &v2==&v){
        std::vector< T_unsigned<T,U> > tmp;
        smalladd(v1,v2,tmp,reduce);
        std::swap< std::vector< T_unsigned<T,U> > >(v,tmp);
        return;
      }
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1.begin(),it1end=v1.end(),it2=v2.begin(),it2end=v2.end();
      T g;
      v.clear();
      v.reserve((it1end-it1)+(it2end-it2)); // worst case
      for (;it1!=it1end && it2!=it2end;){
        if (it1->u==it2->u){
  	g=it1->g+it2->g;
  	g=g%reduce;
  	if (!is_zero(g))
  	  v.push_back(T_unsigned<T,U>(g,it1->u));
  	++it1;
  	++it2;
        }
        else {
  	if (it1->u>it2->u){
  	  v.push_back(*it1);
  	  ++it1;
  	}
  	else {
  	  v.push_back(*it2);
  	  ++it2;
  	}
        }
      }
      for (;it1!=it1end;++it1)
        v.push_back(*it1);
      for (;it2!=it2end;++it2)
        v.push_back(*it2);
    }
  
    template<class T,class U>
    void smallsub(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v){
      if (&v1==&v || &v2==&v){
        std::vector< T_unsigned<T,U> > tmp;
        smallsub(v1,v2,tmp);
        std::swap< std::vector< T_unsigned<T,U> > >(v,tmp);
        return;
      }
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1.begin(),it1end=v1.end(),it2=v2.begin(),it2end=v2.end();
      T g;
      v.clear();
      v.reserve((it1end-it1)+(it2end-it2)); // worst case
      for (;it1!=it1end && it2!=it2end;){
        if (it1->u==it2->u){
  	g=it1->g-it2->g;
  	if (!is_zero(g))
  	  v.push_back(T_unsigned<T,U>(g,it1->u));
  	++it1;
  	++it2;
        }
        else {
  	if (it1->u>it2->u){
  	  v.push_back(*it1);
  	  ++it1;
  	}
  	else {
  	  v.push_back(T_unsigned<T,U>(-it2->g,it2->u));
  	  ++it2;
  	}
        }
      }
      for (;it1!=it1end;++it1)
        v.push_back(*it1);
      for (;it2!=it2end;++it2)
        v.push_back(T_unsigned<T,U>(-it2->g,it2->u));
    }
  
    template<class T,class U,class R>
    void smallsub(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,const R & reduce){
      if (&v1==&v || &v2==&v){
        std::vector< T_unsigned<T,U> > tmp;
        smallsub(v1,v2,tmp,reduce);
        std::swap< std::vector< T_unsigned<T,U> > >(v,tmp);
        return;
      }
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1.begin(),it1end=v1.end(),it2=v2.begin(),it2end=v2.end();
      T g;
      v.clear();
      v.reserve((it1end-it1)+(it2end-it2)); // worst case
      for (;it1!=it1end && it2!=it2end;){
        if (it1->u==it2->u){
  	g=it1->g-it2->g;
  	g=g%reduce;
  	if (!is_zero(g))
  	  v.push_back(T_unsigned<T,U>(g,it1->u));
  	++it1;
  	++it2;
        }
        else {
  	if (it1->u>it2->u){
  	  v.push_back(*it1);
  	  ++it1;
  	}
  	else {
  	  v.push_back(T_unsigned<T,U>(-it2->g,it2->u));
  	  ++it2;
  	}
        }
      }
      for (;it1!=it1end;++it1)
        v.push_back(*it1);
      for (;it2!=it2end;++it2)
        v.push_back(T_unsigned<T,U>(-it2->g,it2->u));
    }
  
    template<class T,class U>
    void smallmult(const T & g,const std::vector< T_unsigned<T,U> > & v1,std::vector< T_unsigned<T,U> > & v){
      if (is_zero(g)){
        v.clear();
        return;
      }
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1.begin(),it1end=v1.end();
      if (&v1==&v){
        typename std::vector< T_unsigned<T,U> >::iterator it1=v.begin(),it1end=v.end();
        for (;it1!=it1end;++it1){
  	it1->g = g*it1->g;
        }
      }
      else {
        v.clear();
        v.reserve(it1end-it1); // worst case
        for (;it1!=it1end;++it1){
  	v.push_back(T_unsigned<T,U>(g*it1->g,it1->u));
        }
      }
    }
  
    template<class T,class U,class R>
    void smallmult(const T & g,const std::vector< T_unsigned<T,U> > & v1,std::vector< T_unsigned<T,U> > & v,const R & reduce){
      if (is_zero(g)){
        v.clear();
        return;
      }
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1.begin(),it1end=v1.end();
      if (&v1==&v){
        typename std::vector< T_unsigned<T,U> >::iterator it1=v.begin(),it1end=v.end();
        for (;it1!=it1end;++it1){
  	type_operator_reduce(g,it1->g,it1->g,reduce);
  	// it1->g = (g*it1->g) % reduce;
        }
      }
      else {
        v.clear();
        v.reserve(it1end-it1); // worst case
        T res;
        for (;it1!=it1end;++it1){
  	type_operator_reduce(g,it1->g,res,reduce);
  	v.push_back(T_unsigned<T,U>(res,it1->u));
  	// v.push_back(T_unsigned<T,U>((g*it1->g)%reduce,it1->u));
        }
      }
    }
  
    template<class T,class U>
    void smalldiv(const std::vector< T_unsigned<T,U> > & v1,const T & g,std::vector< T_unsigned<T,U> > & v){
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1.begin(),it1end=v1.end();
      if (&v1==&v){
        typename std::vector< T_unsigned<T,U> >::iterator it1=v.begin(),it1end=v.end();
        for (;it1!=it1end;++it1){
  	it1->g = it1->g/g;
        }
      }
      else {
        v.clear();
        v.reserve(it1end-it1); // worst case
        for (;it1!=it1end;++it1){
  	v.push_back(T_unsigned<T,U>(it1->g/g,it1->u));
        }
      }
    }
  
    template<class T,class U>
    void smallshift(const std::vector< T_unsigned<T,U> > & v1,U shift,std::vector< T_unsigned<T,U> > & v){
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1.begin(),it1end=v1.end();
      if (&v1==&v){
        typename std::vector< T_unsigned<T,U> >::iterator it1=v.begin(),it1end=v.end();
        for (;it1!=it1end;++it1){
  	it1->u += shift;
        }
      }
      else {
        v.clear();
        v.reserve(it1end-it1); // worst case
        for (;it1!=it1end;++it1){
  	v.push_back(T_unsigned<T,U>(it1->g,it1->u+shift));
        }
      }
    }
  
    // eval v1 at vars.back()=g
    // should be used with reduce!=0, T=int or longlong 
    // (powmod should be defined for T,U,T)
    // * should not overflow in T, if T=int, reduce must be < 46340
    // this could be fixed using type_operator_... instead of *
    template<class T,class U,class R>
    void smallhorner(const std::vector< T_unsigned<T,U> > & v1,const T & g,const std::vector<U> & vars,std::vector< T_unsigned<T,U> > & v,const R& reduce){
      typename std::vector< T_unsigned<T,U> >::const_iterator it=v1.begin(),itend=v1.end();
      U deg=vars.back();
      v.clear();
      v.reserve((itend-it)/deg);
      for (;it!=itend;){
        // find smallest possible monomial degree for next element of v
        U minu=(it->u/deg)*deg;
        U prevdiffu=it->u-minu,diffu;
        T tmp;
        for (;it!=itend;++it){
  	if (it->u<minu){
  	  if (prevdiffu)
  	    tmp = tmp*powmod(g,prevdiffu,reduce);
  	  prevdiffu=0;
  	  tmp = tmp%reduce;
  	  v.push_back(T_unsigned<T,U>(tmp,minu));
  	  break;
  	}
  	diffu=it->u-minu;
  	if (prevdiffu!=diffu){
  	  if (prevdiffu==diffu+1)
  	    tmp = tmp*g;
  	  else
  	    tmp = tmp*powmod(g,prevdiffu-diffu,reduce);
  	}
  	tmp += it->g;
  	tmp = tmp%reduce;
  	prevdiffu=diffu;
  	if (!diffu){
  	  v.push_back(T_unsigned<T,U>(tmp,minu));
  	  break;
  	}
        }
        if (prevdiffu){
  	tmp=tmp*powmod(g,prevdiffu,reduce)%reduce;
  	v.push_back(T_unsigned<T,U>(tmp,minu));
        }
      }
    }
  
    // eval v1 at vars.back()=g
    // should be used with T=gen 
    template<class T,class U>
    void smallhorner(const std::vector< T_unsigned<T,U> > & v1,const T & g,const std::vector<U> & vars,std::vector< T_unsigned<T,U> > & v){
      typename std::vector< T_unsigned<T,U> >::const_iterator it=v1.begin(),itend=v1.end();
      U deg=vars.back();
      v.clear();
      v.reserve((itend-it)/deg);
      for (;it!=itend;){
        // find smallest possible monomial degree for next element of v
        U minu=(it->u/deg)*deg;
        U prevdiffu=it->u-minu,diffu;
        T tmp(0);
        for (;it!=itend;){
  	if (it->u<minu){
  	  if (prevdiffu)
  	    tmp = tmp*pow(g,(unsigned long) prevdiffu);
  	  prevdiffu=0;
  	  v.push_back(T_unsigned<T,U>(tmp,minu));
  	  break;
  	}
  	diffu=it->u-minu;
  	if (prevdiffu!=diffu){
  	  if (prevdiffu==diffu+1)
  	    tmp = tmp*g;
  	  else
  	    tmp = tmp*pow(g,(unsigned long)prevdiffu-diffu);
  	}
  	tmp += it->g;
  	++it;
  	prevdiffu=diffu;
  	if (!diffu){
  	  v.push_back(T_unsigned<T,U>(tmp,minu));
  	  break;
  	}
        }
        if (prevdiffu){
  	tmp=tmp*pow(g,(unsigned long)prevdiffu);
  	v.push_back(T_unsigned<T,U>(tmp,minu));
        }
      }
    }
  
    template<class T,class U,class R>
    void smallmult(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,const R & reduce,size_t possible_size);
  
    template<class T,class U,class R>
    void smallmulpoly_interpolate(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,const std::vector<U> & vars,const index_t & vdeg,const R& reduce){
      int dim=int(vars.size());
      if (dim==1){
        smallmult(v1,v2,v,reduce,0);
        return;
      }
      std::vector<U> vars1=vars;
      vars1.pop_back();
      int s=vdeg[dim-1];
      v.clear();
      std::vector< T_unsigned<T,U> > tmp2,tmp3;
      std::vector< T_unsigned<T,U> > * tab = new std::vector< T_unsigned<T,U> >[s];
      for (int alpha=0;alpha<s;++alpha){
        smallhorner<T,U>(v1,alpha,vars,tmp2,reduce);
        smallhorner<T,U>(v2,alpha,vars,tmp3,reduce);
        smallmulpoly_interpolate<T,U>(tmp2,tmp3,tab[alpha],vars1,vdeg,reduce);
        CERR << alpha << ":" << tab[alpha] << std::endl;
      }
      // divided differences
      for (int k=1;k<s;++k){
        CERR << k << std::endl;
        for (int j=s-1;j>=k;--j){
  	smallsub(tab[j],tab[j-1],tmp2,reduce);
  	smallmult(invmod(k,reduce),tmp2,tab[j],reduce);
  	CERR << tab[j] ;
        }
      }
      // interpolation
      for (int alpha=s-1;alpha>=0;--alpha){
        smallmult<T,U,R>(-alpha,v,tmp2,reduce); 
        smallshift(v,U(1),v); // multiply v*(x-alpha)
        smalladd<T,U,R>(v,tmp2,tmp3,reduce);
        smalladd<T,U,R>(tmp3,tab[alpha],v,reduce);
      }
      delete [] tab;
    }
  
    template<class T,class U>
    void smallmulpoly_interpolate(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,const std::vector<U> & vars,const index_t & vdeg){
      int dim=int(vars.size());
      if (dim==1){
        smallmult(v1,v2,v,0,0);
        return;
      }
      std::vector<U> vars1=vars;
      vars1.pop_back();
      int s=vdeg[dim-1];
      v.clear();
      std::vector< T_unsigned<T,U> > tmp2,tmp3;
      std::vector< T_unsigned<T,U> > * tab = new std::vector< T_unsigned<T,U> >[s];
      for (int alpha=0;alpha<s;++alpha){
        smallhorner<T,U>(v1,alpha,vars,tmp2);
        smallhorner<T,U>(v2,alpha,vars,tmp3);
        smallmulpoly_interpolate<T,U>(tmp2,tmp3,tab[alpha],vars1,vdeg);
        // CERR << tab[alpha] << std::endl;
      }
      // divided differences
      for (int k=1;k<s;++k){
        for (int j=s-1;j>=k;--j){
  	smallsub(tab[j],tab[j-1],tmp2);
  	smalldiv(tmp2,T(k),tab[j]);
        }
      }
      // interpolation
      for (int alpha=s-1;alpha>=0;--alpha){
        smallmult<T,U>(-alpha,v,tmp2); 
        smallshift(v,U(1),v); // multiply v*(x-alpha)
        smalladd<T,U>(v,tmp2,tmp3);
        smalladd<T,U>(tmp3,tab[alpha],v);
      }
      delete [] tab;
    }
  
    template<class T,class U,class R>
    void smallmulpoly_interpolate(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,const index_t & vdeg,const R & reduce){
      int dim=int(vdeg.size());
      std::vector<U> vars(dim);
      vars.back()=vdeg.back();
      for (int i=dim-1;i>0;--i){
        vars[i-1]=vars[i]*vdeg[i-1];
      }
      smallmulpoly_interpolate(v1,v2,v,vars,vdeg,reduce);
    }
  
    template<class T,class U>
    void smallmulpoly_interpolate(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,const index_t & vdeg){
      int dim=int(vdeg.size());
      std::vector<U> vars(dim);
      vars.back()=vdeg.back();
      for (int i=dim-1;i>0;--i){
        vars[i-1]=vars[i]*vdeg[i-1];
      }
      smallmulpoly_interpolate(v1,v2,v,vars,vdeg);
    }
  
    template<class U>
    struct u_pair_index {
      U u;
      unsigned i1,i2;
      u_pair_index(unsigned _i1,unsigned _i2,U _u):u(_u),i1(_i1),i2(_i2) {}
      u_pair_index():u(0),i1(0),i2(0) {}
    };
    template<class U>
    inline bool operator < (const u_pair_index<U> & p1,const u_pair_index<U> & p2){
      return p1.u<p2.u;
    }
  
    template<class U>
    inline bool operator <= (const u_pair_index<U> & p1,const u_pair_index<U> & p2){
      return p1.u<=p2.u;
    }
  
    template<class T>
    void in_out_heap(T * tab,size_t size,T value){
      unsigned childindex=2,holeindex=0;
      while (childindex<size){
        // find largest child until end of tab
        register T * ptr=tab+childindex;
        if (*ptr<*(ptr-1)){
  	--childindex;
  	--ptr;
        }
        *(tab+holeindex)=*ptr;
        holeindex=childindex;
        childindex = (holeindex+1) << 1;
      }
      if (childindex==size){
        --childindex;
        *(tab+holeindex)=*(tab+childindex);
        holeindex=childindex;
      }
      // now the hole is at the bottom, replace it with value and promote value
      *(tab+holeindex)=value;
      // childindex is now the parent
      while (holeindex){
        childindex=(holeindex-1) >> 1;
        if (*(tab+holeindex)<=*(tab+childindex))
  	break;
        std::swap<T>(*(tab+childindex),*(tab+holeindex));
        holeindex=childindex;
      }
    }
  
    template<class U>
    inline bool inverse_order(const U & u1,const U & u2){
      return u1<u2;
    }
  
    template<class U>
    struct U_unsigned {
      U u;
      unsigned v;
      U_unsigned(U _u,unsigned _v):u(_u),v(_v) {}
      U_unsigned():u(0) {}
    };
    template<class U>
    inline bool operator < (const U_unsigned<U> & p1,const U_unsigned<U> & p2){
      return p1.u<p2.u;
    }
  
    // Possible improvement for threaded execution:
    // make each j of the k threads compute terms of the product with
    // degree wrt 1st var = j % k
    // at the end merge the results
    // For this, we might mark positions in v1 and v2 where the degree
    // wrt to the 1st var changes
    template<class T,class U,class R>
    void smallmult(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,const R & reduce,size_t possible_size){
      if (v1.empty()){ 
        v.clear(); return; 
      }
      if (v2.empty()){ 
        v.clear(); return; 
      }
      if (&v1==&v || &v2==&v){
        std::vector< T_unsigned<T,U> > tmp;
        smallmult(v1,v2,tmp,reduce,possible_size);
        std::swap< std::vector< T_unsigned<T,U> > >(v,tmp);
        return;
      }
      typename std::vector< T_unsigned<T,U> >::const_iterator it1beg=v1.begin(),it1=v1.begin(),it1end=v1.end(),it2beg=v2.begin(),it2,it2end=v2.end();
      T g1,g;
      U u1=it1beg->u,u2=it2beg->u,u;
      v.clear();
      unsigned v1s=unsigned(it1end-it1beg),v2s=unsigned(it2end-it2beg);
      double v1v2=double(v1s)*v2s;
      U u12=u1+u2; // size of the array for array multiplication
      // compare u12 and v1v2*ln(v1v2)
      if ( heap_mult>=0 && possible_size && u12<512e6/sizeof(T) && std::log(double(std::min(v1s,v2s)))/std::log(2.0)>1+2*u12/v1v2){
        if (debug_infolevel>20)
  	CERR << "array multiplication, v1 size " << v1s << " v2 size " << v2s << " u1+u2 " << u12 << std::endl;
        // array multiplication
        T * prod = new T[unsigned(u12+1)];
        for (u=0;u<=u12;++u)
  	prod[u]=T(0);
        typename std::vector< T_unsigned<T,U> >::const_iterator slice_it2beg=it2beg,slice_it2end=it2end;
        const int slice_size=60;
        for (;slice_it2beg<slice_it2end;slice_it2beg=it2end){
  	it2beg=slice_it2beg;
  	it2end=it2beg+slice_size;
  	if (it2end>slice_it2end) 
  	  it2end=slice_it2end;
  	for (it1=it1beg;it1!=it1end;++it1){
  	  g1=it1->g;
  	  u1=it1->u;
  	  if (is_zero(reduce)){
  	    for (it2=it2beg;it2!=it2end;++it2){
  	      type_operator_plus_times(g1,it2->g,prod[u1+it2->u]);
  	    }
  	  }
  	  else {
  	    for (it2=it2beg;it2!=it2end;++it2){
  	      type_operator_plus_times_reduce(g1,it2->g,prod[u1+it2->u],reduce);
  	    }
  	  }
  	}
        }
        int n=0;
        for (u=u12;;--u){
  	if (!is_zero(prod[u]))
  	  ++n;
  	if (!u)
  	  break;
        }
        v.reserve(n);
        for (u=u12;;--u){
  	if (!is_zero(prod[u]))
  	  v.push_back(T_unsigned<T,U>(prod[u],u));
  	if (!u)
  	  break;
        }
        delete [] prod ;
        return;
      }
      bool use_heap=(heap_mult>0 && v1v2>=heap_mult);
      if (debug_infolevel>20){
        CERR << "// " << CLOCK() << "using ";
        if (use_heap)
  	CERR << "heap";
        else
  	CERR << heap_mult;
        CERR<< " multiplication" << std::endl;
      }
      if (heap_mult<0 || use_heap){
        if (v1s>v2s){
  	smallmult(v2,v1,v,reduce,possible_size);
  	return;
        }
        if (v1s<128)
  	use_heap=false; // use heap instead of heap of chains
        if (heap_mult==-1 || use_heap ){
  	// using heap of chains
  	// std::vector< vector_size64< std::pair<unsigned,unsigned> > > vindex(v1s);
  	std::vector< std::vector< std::pair<unsigned,unsigned> > > vindex(v1s);
  	double count1=0,count2=0,total=double(v1s)*v2s;
  	U_unsigned<U> * heap = new U_unsigned<U>[v1s] ; // pointers to v2 monomials
  	U_unsigned<U> * heap0, *heapbeg=heap,* heapend=heap+v1s;
  	for (it1=it1beg,heap0=heap;heap0!=heapend;++heap0,++it1){
  	  // vindex[it1-it1beg]=vector_size64< std::pair<unsigned,unsigned> >(1,std::pair<unsigned,unsigned>(it1-it1beg,0));
  	  vindex[it1-it1beg]=std::vector< std::pair<unsigned,unsigned> >(1,std::pair<unsigned,unsigned>(unsigned(it1-it1beg),0));
  	  *heap0=U_unsigned<U>(it1->u+u2,unsigned(it1-it1beg));
  	}
  	// vector_size64< std::pair<unsigned,unsigned> > nouveau;
  	std::vector< std::pair<unsigned,unsigned> > nouveau;
  	for (;heapbeg!=heapend;){
  	  U topu=heapbeg->u;
  	  if (!v.empty() && v.back().u==heapbeg->u){
  	    g=v.back().g;
  	    v.pop_back();
  	  }
  	  else
  	    g=T(0);
  	  std::vector< std::pair<unsigned,unsigned> >::iterator it,itend;
  	  nouveau.clear();
  	  while (heapend!=heapbeg && topu==heapbeg->u){
  	    // add all elements of the top chain	
  	    it=vindex[heapbeg->v].begin();
  	    itend=vindex[heapbeg->v].end();
  	    for (;it!=itend;++it){
  	      unsigned & its=it->second;
  	      type_operator_plus_times_reduce((it1beg+it->first)->g,(it2beg+its)->g,g,reduce);
  	      // increment 2nd poly index of the elements of the top chain
  	      ++its;
  	      if (its!=v2s)
  		nouveau.push_back(*it);
  	    }
  #ifdef USTL
  	    ustl::pop_heap(heapbeg,heapend);
  #else
  	    std::pop_heap(heapbeg,heapend);
  #endif
  	    --heapend;
  	  }
  	  if (!is_zero(g))
  	    v.push_back(T_unsigned<T,U>(g,topu));
  	  // erase top node, then push each element of the incremented top chain 
  	  it=nouveau.begin();
  	  itend=nouveau.end();
  	  U prevu=0; int previndex=-1;
  	  for (;it!=itend;++it){
  	    u=(it1beg+it->first)->u+(it2beg+it->second)->u;
  	    if (u==prevu && previndex>=0){
  	      vindex[previndex].push_back(*it);
  #ifdef HEAP_STATS
  	      ++count1;
  #endif
  	      continue;
  	    }
  	    prevu=u;
  	    // check if u is in the path to the root of the heap
  	    unsigned holeindex=unsigned(heapend-heapbeg),parentindex;
  	    if (holeindex && u==heapbeg->u){
  	      vindex[previndex=heapbeg->v].push_back(*it);
  #ifdef HEAP_STATS
  	      ++count1;
  #endif
  	      continue;
  	    }
  	    bool done=false;
  	    while (holeindex){
  	      parentindex=(holeindex-1) >> 1;
  	      U pu=(heapbeg+parentindex)->u;
  	      if (u<pu)
  		break;
  	      if (u==pu){
  		done=true;
  #ifdef HEAP_STATS
  		++count2;
  #endif
  		vindex[previndex=(heapbeg+parentindex)->v].push_back(*it);
  		break;
  	      }
  	      holeindex=parentindex;
  	    }
  	    if (!done){
  	      // not found, add a new node to the heap
  	      vindex[previndex=heapend->v].clear();
  	      vindex[previndex].push_back(*it);
  	      heapend->u=u;
  	      ++heapend;
  #ifdef USTL
  	      ustl::push_heap(heapbeg,heapend);
  #else
  	      std::push_heap(heapbeg,heapend);
  #endif
  	    }
  	  }
  	} // end for heapbeg!=heapend
  #ifdef HEAP_STATS
  	if (debug_infolevel>20)
  	  CERR << CLOCK() << " heap_mult, %age of chains" << count1/total << " " << count2/total << " " << std::endl;
  #endif
  	delete [] heap;
  	return;
        }
  #ifndef USTL
        if (heap_mult==-2){
  	// using multimap for f*g, seems slower than heap
  	typedef std::multimap< U,std::pair<unsigned,unsigned> > mmap;
  	mmap M;
  	typename mmap::iterator Mbeg,Mit=M.begin(),Mitbeg,Mend;
  	// fill M with (f_i,g_1)
  	for (it1=it1end-1;;){
  	  Mit=M.insert(Mit,std::pair<U,std::pair<unsigned,unsigned> >(it1->u+u2,std::pair<unsigned,unsigned>(unsigned(it1-it1beg),0)));
  	  if (it1==it1beg)
  	    break;
  	  --it1;
  	}
  	// find top degree coefficient of the product then
  	// replace top range degree elements or erase them
  	for (;!M.empty();){
  	  Mend=M.end();
  	  Mbeg=M.begin();
  	  Mitbeg=Mend;
  	  --Mitbeg;
  	  U deg=Mitbeg->first;
  	  T coeff;
  	  for (;Mitbeg!=Mbeg;--Mitbeg){
  	    if (Mitbeg->first!=deg)
  	      break;
  	  }
  	  if (Mitbeg->first!=deg)
  	    ++Mitbeg;
  	  Mit=Mitbeg;
  	  type_operator_reduce((it1beg+Mit->second.first)->g,(it2beg+Mit->second.second)->g,coeff,reduce); 
  	  for (++Mit;Mit!=Mend;++Mit){
  	    type_operator_plus_times_reduce((it1beg+Mit->second.first)->g,(it2beg+Mit->second.second)->g,coeff,reduce); 
  	  }
  	  if (!is_zero(coeff))
  	    v.push_back(T_unsigned<T,U>(coeff,deg));
  	  Mit=Mitbeg;
  	  std::vector< std::pair<unsigned,unsigned> > vp;
  	  for (;Mit!=Mend;++Mit){
  	    std::pair<unsigned,unsigned> p=Mit->second;
  	    ++p.second;
  	    if (p.second<v2s)
  	      vp.push_back(p);
  	  }
  	  M.erase(Mitbeg,Mend);
  	  std::vector< std::pair<unsigned,unsigned> > ::iterator vit=vp.begin(),vitend=vp.end();
  	  for (;vit!=vitend;++vit)
  	    M.insert(std::pair<U,std::pair<unsigned,unsigned> >((it1beg+vit->first)->u+(it2beg+vit->second)->u,*vit));
  	}
  	return;
        }
        if (heap_mult==-3){ // using map of U -> chains
  	typedef std::map< U,std::vector<std::pair<unsigned,unsigned> > > mmap;
  	std::vector< std::pair<unsigned,unsigned> >::iterator it,itend;
  	mmap M;
  	typename mmap::iterator Mit=M.begin();
  	// fill M with (f_i,g_1)
  	for (it1=it1end-1;;){
  	  Mit=M.insert(Mit,std::pair<U,std::vector< std::pair<unsigned,unsigned> > >(it1->u+u2,std::vector< std::pair<unsigned,unsigned> > (1, std::pair<unsigned,unsigned>(unsigned(it1-it1beg),0) )));
  	  if (it1==it1beg)
  	    break;
  	  --it1;
  	}
  	while (!M.empty()){
  	  Mit=M.end();
  	  --Mit;
  	  u=Mit->first;
  	  it=Mit->second.begin();
  	  itend=Mit->second.end();
  	  g=T(0);
  	  std::vector< std::pair<unsigned,unsigned> > nouveau;
  	  for (;it!=itend;++it){
  	    type_operator_plus_times_reduce((it1beg+it->first)->g,(it2beg+it->second)->g,g,reduce);
  	    ++it->second;
  	    if (it->second!=v2s)
  	      nouveau.push_back(*it);
  	  }
  	  if (!is_zero(g))
  	    v.push_back(T_unsigned<T,U>(g,u));
  	  M.erase(Mit);
  	  it=nouveau.begin();
  	  itend=nouveau.end();
  	  for (;it!=itend;++it){
  	    u=(it1beg+it->first)->u+(it2beg+it->second)->u;
  	    Mit=M.find(u);
  	    if (Mit!=M.end())
  	      Mit->second.push_back(std::pair<unsigned,unsigned>(it->first,it->second));
  	    else
  	      M[u]=std::vector< std::pair<unsigned,unsigned> >(1,std::pair<unsigned,unsigned>(it->first,it->second));
  	  }
  	}
  	return;
        }
  #endif
        // using heap
        u_pair_index<U> newelem, * heap = new u_pair_index<U>[v1s] ; // pointers to v2 monomials
        u_pair_index<U> * heap0, *heapbeg=heap,* heapend=heap+v1s, * heaplast=heap+v1s-1;
        for (it1=it1beg,heap0=heap;heap0!=heapend;++heap0,++it1){
  	*heap0=u_pair_index<U>(unsigned(it1-it1beg),0,it1->u+u2);
        }
        for (;heapbeg!=heapend;){
  	if (!v.empty() && v.back().u==heapbeg->u){
  	  type_operator_plus_times_reduce((it1beg+heapbeg->i1)->g,(it2beg+heapbeg->i2)->g,v.back().g,reduce); 
  	  if ( is_zero(v.back().g) )
  	    v.pop_back();
  	}
  	else {
  	  type_operator_reduce((it1beg+heapbeg->i1)->g,(it2beg+heapbeg->i2)->g,g,reduce); 
  	  v.push_back(T_unsigned<T,U>(g,heapbeg->u));
  	}
  	++heapbeg->i2;
  	if (heapbeg->i2==v2s){
  #ifdef USTL
  	  ustl::pop_heap(heapbeg,heapend);
  #else
  	  std::pop_heap(heapbeg,heapend);
  #endif
  	  // std::pop_heap(heapbeg,heapend);
  	  --heaplast;
  	  --heapend;
  	}
  	else {
  #ifdef USTL
  	  ustl::pop_heap(heapbeg,heapend);
  #else
  	  std::pop_heap(heapbeg,heapend);
  #endif
  	  // std::pop_heap(heapbeg,heapend);
  	  heaplast->u=(it1beg+heaplast->i1)->u+(it2beg+heaplast->i2)->u;
  #ifdef USTL
  	  ustl::push_heap(heapbeg,heapend);
  #else
  	  std::push_heap(heapbeg,heapend);
  #endif
  	  // in_out_heap(heapbeg,heapend-heapbeg,newelem);
  	}
        }
        delete [] heap;
      } // end if (heapmult)
      else {
  #ifdef HASH_MAP_NAMESPACE
        typedef HASH_MAP_NAMESPACE::hash_map< U,T,hash_function_unsigned_object > hash_prod ;
        hash_prod produit(possible_size); // try to avoid reallocation
        // cout << "hash " << CLOCK() << std::endl;
  #else
  #ifdef USTL
        typedef ustl::map<U,T> hash_prod;
  #else
        typedef std::map<U,T> hash_prod;
  #endif
        // cout << "small map" << std::endl;
        hash_prod produit; 
  #endif    
        typename hash_prod::iterator prod_it,prod_itend;
        for (;it1!=it1end;++it1){
  	g1=it1->g;
  	u1=it1->u;
  	if (!is_zero(reduce)){
  	  for (it2=it2beg;it2!=it2end;++it2){
  	    u=u1+it2->u;
  	    prod_it=produit.find(u);
  	    if (prod_it==produit.end())
  	      type_operator_reduce(g1,it2->g,produit[u],reduce); // g=g1*it2->g; 
  	    else 
  	      type_operator_plus_times_reduce(g1,it2->g,prod_it->second,reduce); 
  	  }
  	}
  	else {
  	  for (it2=it2beg;it2!=it2end;++it2){
  	    u=u1+it2->u;
  	    prod_it=produit.find(u);
  	    if (prod_it==produit.end()){
  	      type_operator_times(g1,it2->g,produit[u]); // g=g1*it2->g; 
  	    }
  	    else {
  	      type_operator_plus_times(g1,it2->g,prod_it->second); 
  	      // g=g1*it2->g; 
  	      // prod_it->second+=g;
  	    }
  	  }
  	}
        }
        T_unsigned<T,U> gu;
        prod_it=produit.begin(),prod_itend=produit.end();
        v.reserve(produit.size());
        for (;prod_it!=prod_itend;++prod_it){
  	if (!is_zero(gu.g=prod_it->second)){
  	  gu.u=prod_it->first;
  	  v.push_back(gu);
  	}
        }    
        // CERR << "smallmult sort " << CLOCK() << std::endl;
        sort(v.begin(),v.end());
        // CERR << "smallmult sort end " << CLOCK() << std::endl;
      } // endif // HEAP_MULT
    }
  
  
    template<class T,class U,class R>
    struct threadmult_t {
      const std::vector< T_unsigned<T,U> > * v1ptr ;
      std::vector< typename std::vector< T_unsigned<T,U> >::const_iterator > * v1ptrs, * v2ptrs;
      std::vector< T_unsigned<T,U> > * vptr;
      U degdiv;
      unsigned current_deg;
      unsigned clock;
      R reduce;
      int status;
      bool use_heap;
      T * prod;
      std::vector< vector_size64< std::pair<unsigned,unsigned> > > * vindexptr;
      std::vector< vector_size32< std::pair<unsigned short,unsigned short> > > * vsmallindexptr;
      U_unsigned<U> * heapptr;
    };
  
  #if defined HAVE_PTHREAD_H && defined HAVE_LIBPTHREAD
  
    template<class T,class U,class R> void * do_threadmult(void * ptr){
      threadmult_t<T,U,R> * argptr = (threadmult_t<T,U,R> *) ptr;
      argptr->status=1;
      argptr->clock=CLOCK();
      R reduce=argptr->reduce;
      const std::vector< T_unsigned<T,U> > * v1 = argptr->v1ptr;
      std::vector< typename std::vector< T_unsigned<T,U> >::const_iterator >  * v2ptrs = argptr->v2ptrs;
      std::vector< T_unsigned<T,U> > & v = *argptr->vptr;
      typename std::vector< T_unsigned<T,U> >::const_iterator it1=v1->begin(),it1beg=v1->begin(),it1end=v1->end(),it2beg,it2,it2end;
      T g1,g;
      U u1,u2,u,degdiv=argptr->degdiv;
      int d1=0,d2,v2deg=v2ptrs->size()-2,v1deg=argptr->v1ptrs->size()-2,d=argptr->current_deg;
      T * prod=argptr->prod;
      if (prod){
        for (int slice_d2=0;slice_d2<=v2deg;++slice_d2){
  	if (slice_d2>d) break;
  	d1=d-slice_d2;
  	if (d1>v1deg) continue;
  	it1beg=(*argptr->v1ptrs)[d1];
  	it1end=(*argptr->v1ptrs)[d1+1];
  	typename std::vector< T_unsigned<T,U> >::const_iterator slice_it2beg=(*v2ptrs)[slice_d2],slice_it2end=(*v2ptrs)[slice_d2+1];
  	const int slice_size=56;
  	for (; slice_it2beg<slice_it2end;slice_it2beg=it2end){
  	  it2beg=slice_it2beg;
  	  it2end=it2beg+slice_size;
  	  if (it2end>slice_it2end)
  	    it2end=slice_it2end;
  	  for (it1=it1beg;it1!=it1end;++it1){
  	    u1=it1->u;
  	    g1=it1->g;
  #if 1
  	    if ((it1+3)<it1end){
  	      // 4 monomials have same main degree: make the product simult
  	      T * prod0=prod+u1, * prod1=prod+(it1+1)->u,*prod2=prod+(it1+2)->u,*prod3=prod+(it1+3)->u;
  	      T g1_1=(it1+1)->g,g1_2=(it1+2)->g,g1_3=(it1+3)->g,g2;
  	      if (is_zero(reduce)){
  		it2end -= 4;
  		for (it2=it2beg;it2<=it2end;it2+=4){
  		  u2=it2->u;
  		  g2=it2->g;
  		  type_operator_plus_times(g1,g2,prod0[u2]); 
  		  type_operator_plus_times(g1_1,g2,prod1[u2]); 
  		  type_operator_plus_times(g1_2,g2,prod2[u2]); 
  		  type_operator_plus_times(g1_3,g2,prod3[u2]); 
  		  u2=(it2+1)->u;
  		  g2=(it2+1)->g;
  		  type_operator_plus_times(g1,g2,prod0[u2]); 
  		  type_operator_plus_times(g1_1,g2,prod1[u2]); 
  		  type_operator_plus_times(g1_2,g2,prod2[u2]); 
  		  type_operator_plus_times(g1_3,g2,prod3[u2]); 
  		  u2=(it2+2)->u;
  		  g2=(it2+2)->g;
  		  type_operator_plus_times(g1,g2,prod0[u2]); 
  		  type_operator_plus_times(g1_1,g2,prod1[u2]); 
  		  type_operator_plus_times(g1_2,g2,prod2[u2]); 
  		  type_operator_plus_times(g1_3,g2,prod3[u2]); 
  		  u2=(it2+3)->u;
  		  g2=(it2+3)->g;
  		  type_operator_plus_times(g1,g2,prod0[u2]); 
  		  type_operator_plus_times(g1_1,g2,prod1[u2]); 
  		  type_operator_plus_times(g1_2,g2,prod2[u2]); 
  		  type_operator_plus_times(g1_3,g2,prod3[u2]); 
  		}
  		it2end += 4;
  		for (;it2!=it2end;++it2){
  		  u2=it2->u;
  		  g2=it2->g;
  		  type_operator_plus_times(g1,g2,prod0[u2]); 
  		  type_operator_plus_times(g1_1,g2,prod1[u2]); 
  		  type_operator_plus_times(g1_2,g2,prod2[u2]); 
  		  type_operator_plus_times(g1_3,g2,prod3[u2]); 
  		}
  	      }
  	      else {
  		for (it2=it2beg;it2!=it2end;++it2){
  		  u2=it2->u;
  		  g2=it2->g;
  		  type_operator_plus_times_reduce(g1,g2,prod0[u2],reduce); 
  		  type_operator_plus_times_reduce(g1_1,g2,prod1[u2],reduce); 
  		  type_operator_plus_times_reduce(g1_2,g2,prod2[u2],reduce); 
  		  type_operator_plus_times_reduce(g1_3,g2,prod3[u2],reduce); 
  		}
  	      }
  	      it1 += 3;
  	      continue;
  	    }
  #endif
  	    T * prod0 = prod+u1;
  	    if (is_zero(reduce)){
  	      for (it2=it2beg;it2!=it2end;++it2){
  		type_operator_plus_times(g1,it2->g,prod0[it2->u]); 
  		// prod_it->second += g; 
  	      }
  	    }
  	    else {
  	      for (it2=it2beg;it2!=it2end;++it2){
  		type_operator_plus_times_reduce(g1,it2->g,prod0[it2->u],reduce); 
  	      }
  	    }
  	  } // end it1 loop
  	} // end slice_it2 loop
        } // end slice_degree2 loop
        argptr->clock = CLOCK() - argptr->clock;
        argptr->status=2;
        return &v; // not used, all is stored in prod
      }
      // thread-heap multiplication
      if (//false
  	argptr->use_heap
  	){
        // using heap of chains
        // int v1s=it1end-it1,v2s=it2end-it2;
        std::vector< vector_size64< std::pair<unsigned,unsigned> > > * vindexptr=argptr->vindexptr;
        std::vector< vector_size32< std::pair<unsigned short,unsigned short> > > * vsmallindexptr=argptr->vsmallindexptr;
        bool smallindex=vsmallindexptr;
        U_unsigned<U> * heap = argptr->heapptr ; // pointers to v2 monomials
        U_unsigned<U> * heap0, *heapbeg=heap,* heapend;
        // initial fill of the heap
        int count=0;
        for (it1=it1beg,heap0=heap;it1!=it1end;++it1){
  	u1=it1->u;
  	d1=u1/degdiv;
  	if (d1>d) // first partial degree too large
  	  continue;
  	d2=v2deg-(d-d1);
  	if (d2<0) // first partial degree too small
  	  continue;
  	it2=(*v2ptrs)[d2]; // first monomial of second poly having a compatible partial degree
  	it2end=(*v2ptrs)[d2+1];
  	if (it2==it2end)
  	  continue;
  	u2=it2->u;
  	if (int(u2/degdiv+d1)!=d)
  	  continue;
  	if (smallindex){
  	  (*vsmallindexptr)[count].clear();
  	  (*vsmallindexptr)[count].push_back(std::pair<unsigned short,unsigned short>(it1-it1beg,0));
  	}
  	else {
  	  (*vindexptr)[count].clear();
  	  (*vindexptr)[count].push_back(std::pair<unsigned,unsigned>(it1-it1beg,0));
  	}
  	*heap0=U_unsigned<U>(u1+u2,count);
  	++heap0;
  	++count;
        }
        heapend=heap0;
        std::make_heap(heapbeg,heapend);
        if (smallindex){
  	vector_size32< std::pair<unsigned short,unsigned short> > nouveau;
  	for (;heapbeg!=heapend;){
  	  U topu=heapbeg->u;
  	  if (!v.empty() && v.back().u==heapbeg->u){
  	    g=v.back().g;
  	    v.pop_back();
  	  }
  	  else
  	    g=T(0);
  	  std::vector< std::pair<unsigned short,unsigned short> >::iterator it,itend;
  	  vector_size32< std::pair<unsigned short,unsigned short> > * vptr;
  	  nouveau.clear();
  	  while (heapend!=heapbeg && topu==heapbeg->u){
  	    // add all elements of the top chain	
  	    vptr = &(*vsmallindexptr)[heapbeg->v];
  	    it=vptr->begin();
  	    itend=vptr->end();
  	    for (;it!=itend;++it){
  	      it1=it1beg+it->first;
  	      u1=it1->u;
  	      d1=u1/degdiv;
  	      d2=v2deg-(d-d1);
  	      it2beg=(*v2ptrs)[d2];
  	      it2=it2beg+it->second;
  	      type_operator_plus_times_reduce(it1->g,it2->g,g,reduce);
  	      // increment 2nd poly index of the elements of the top chain
  	      ++it->second;
  	      // check if it is still with a compatible partial degree
  	      if (it->second+it2beg-(*v2ptrs)[d2+1]<0)
  		nouveau.push_back(*it);
  	    }
  #ifdef USTL
  	    ustl::pop_heap(heapbeg,heapend);
  #else
  	    std::pop_heap(heapbeg,heapend);
  #endif
  	    // std::pop_heap(heapbeg,heapend);
  	    --heapend;
  	  }
  	  if (!is_zero(g))
  	    v.push_back(T_unsigned<T,U>(g,topu));
  	  // erase top node, then push each element of the incremented top chain 
  	  it=nouveau.begin();
  	  itend=nouveau.end();
  	  U prevu=0; int previndex=-1;
  	  for (;it!=itend;++it){
  	    it1=it1beg+it->first;
  	    u1=it1->u;
  	    d1=u1/degdiv;
  	    d2=v2deg-(d-d1);
  	    it2beg=(*v2ptrs)[d2];
  	    u=u1+(it2beg+it->second)->u;
  	    if (u==prevu && previndex>=0){
  	      vptr=&(*vsmallindexptr)[previndex];
  	      vptr->push_back(*it);
  	      continue;
  	    }
  	    prevu=u;
  	    // check if u is in the path to the root of the heap
  	    unsigned holeindex=heapend-heapbeg,parentindex;
  	    if (holeindex && u==heapbeg->u){
  	      vptr=&(*vsmallindexptr)[previndex=heapbeg->v];
  	      vptr->push_back(*it);
  	      continue;
  	    }
  	    bool done=false;
  	    while (holeindex){
  	      parentindex=(holeindex-1) >> 1;
  	      if (u<(heapbeg+parentindex)->u)
  		break;
  	      if (u==(heapbeg+parentindex)->u){
  		done=true;
  		vptr=&(*vsmallindexptr)[previndex=(heapbeg+parentindex)->v];
  		vptr->push_back(*it);
  		break;
  	      }
  	      holeindex=parentindex;
  	    }
  	    if (!done){
  	      // not found, add a new node to the heap
  	      vptr=&(*vsmallindexptr)[previndex=heapend->v];
  	      vptr->clear();
  	      vptr->push_back(*it);
  	      heapend->u=u;
  	      ++heapend;
  #ifdef USTL
  	      ustl::push_heap(heapbeg,heapend);
  #else
  	      std::push_heap(heapbeg,heapend);
  #endif
  	      // std::push_heap(heapbeg,heapend);
  	    }
  	  }
  	} // end for heapbeg!=heapend
        } // end smallindex
        else {
  	vector_size64< std::pair<unsigned,unsigned> > nouveau;
  	for (;heapbeg!=heapend;){
  	  U topu=heapbeg->u;
  	  if (!v.empty() && v.back().u==heapbeg->u){
  	    g=v.back().g;
  	    v.pop_back();
  	  }
  	  else
  	    g=T(0);
  	  std::vector< std::pair<unsigned,unsigned> >::iterator it,itend;
  	  vector_size64< std::pair<unsigned,unsigned> > * vptr;
  	  nouveau.clear();
  	  while (heapend!=heapbeg && topu==heapbeg->u){
  	    // add all elements of the top chain	
  	    vptr = &(*vindexptr)[heapbeg->v];
  	    it=vptr->begin();
  	    itend=vptr->end();
  	    for (;it!=itend;++it){
  	      it1=it1beg+it->first;
  	      u1=it1->u;
  	      d1=u1/degdiv;
  	      d2=v2deg-(d-d1);
  	      it2beg=(*v2ptrs)[d2];
  	      it2=it2beg+it->second;
  	      type_operator_plus_times_reduce(it1->g,it2->g,g,reduce);
  	      // increment 2nd poly index of the elements of the top chain
  	      ++it->second;
  	      // check if it is still with a compatible partial degree
  	      if (it->second+it2beg-(*v2ptrs)[d2+1]<0)
  		nouveau.push_back(*it);
  	    }
  #ifdef USTL
  	    ustl::pop_heap(heapbeg,heapend);
  #else
  	    std::pop_heap(heapbeg,heapend);
  #endif
  	    // std::pop_heap(heapbeg,heapend);
  	    --heapend;
  	  }
  	  if (!is_zero(g))
  	    v.push_back(T_unsigned<T,U>(g,topu));
  	  // erase top node, then push each element of the incremented top chain 
  	  it=nouveau.begin();
  	  itend=nouveau.end();
  	  U prevu=0; int previndex=-1;
  	  for (;it!=itend;++it){
  	    it1=it1beg+it->first;
  	    u1=it1->u;
  	    d1=u1/degdiv;
  	    d2=v2deg-(d-d1);
  	    it2beg=(*v2ptrs)[d2];
  	    u=u1+(it2beg+it->second)->u;
  	    if (u==prevu && previndex>=0){
  	      vptr=&(*vindexptr)[previndex];
  	      vptr->push_back(*it);
  	      continue;
  	    }
  	    prevu=u;
  	    // check if u is in the path to the root of the heap
  	    unsigned holeindex=heapend-heapbeg,parentindex;
  	    if (holeindex && u==heapbeg->u){
  	      vptr=&(*vindexptr)[previndex=heapbeg->v];
  	      vptr->push_back(*it);
  	      continue;
  	    }
  	    bool done=false;
  	    while (holeindex){
  	      parentindex=(holeindex-1) >> 1;
  	      if (u<(heapbeg+parentindex)->u)
  		break;
  	      if (u==(heapbeg+parentindex)->u){
  		done=true;
  		vptr=&(*vindexptr)[previndex=(heapbeg+parentindex)->v];
  		vptr->push_back(*it);
  		break;
  	      }
  	      holeindex=parentindex;
  	    }
  	    if (!done){
  	      // not found, add a new node to the heap
  	      vptr=&(*vindexptr)[previndex=heapend->v];
  	      vptr->clear();
  	      vptr->push_back(*it);
  	      heapend->u=u;
  	      ++heapend;
  #ifdef USTL
  	      ustl::push_heap(heapbeg,heapend);
  #else
  	      std::push_heap(heapbeg,heapend);
  #endif
  	      // std::push_heap(heapbeg,heapend);
  	    }
  	  }
  	} // end for heapbeg!=heapend
        } // end els (smallindex)
        argptr->clock = CLOCK() - argptr->clock;
        argptr->status=2;
        return &v;
      }
  #ifdef HASH_MAP_NAMESPACE
      typedef HASH_MAP_NAMESPACE::hash_map< U,T,hash_function_unsigned_object > hash_prod ;
      hash_prod produit; // try to avoid reallocation
  #else
      typedef std::map<U,T> hash_prod;
      // cout << "small map" << std::endl;
      hash_prod produit; 
  #endif    
      typename hash_prod::iterator prod_it,prod_itend;
      for (;it1!=it1end;++it1){
        u1=it1->u;
        d1=u1/degdiv;
        if (d1>d)
  	continue;
        d2=v2deg-(d-d1);
        if (d2<0) // degree of d1 incompatible
  	break;
        g1=it1->g;
        it2beg=(*v2ptrs)[d2];
        it2end=(*v2ptrs)[d2+1];
        if (!is_zero(reduce)){
  	for (it2=it2beg;it2!=it2end;++it2){
  	  u2=it2->u;
  	  u=u1+u2;
  	  prod_it=produit.find(u);
  	  if (prod_it==produit.end())
  	    type_operator_reduce(g1,it2->g,produit[u],reduce); // g=g1*it2->g; 
  	  else 
  	    type_operator_plus_times_reduce(g1,it2->g,prod_it->second,reduce); 
  	}
        }
        else {
  	for (it2=it2beg;it2!=it2end;++it2){
  	  u2=it2->u;
  	  u=u1+u2;
  	  prod_it=produit.find(u);
  	  if (prod_it==produit.end()){
  	    type_operator_times(g1,it2->g,g); // g=g1*it2->g; 
  	    produit[u]=g;
  	  }
  	  else 
  	    type_operator_plus_times(g1,it2->g,prod_it->second); 
  	  // prod_it->second += g; 
  	}
        }
      }
      T_unsigned<T,U> gu;
      prod_it=produit.begin(),prod_itend=produit.end();
      v.clear();
      v.reserve(produit.size());
      for (;prod_it!=prod_itend;++prod_it){
        if (!is_zero(gu.g=prod_it->second)){
  	gu.u=prod_it->first;
  	v.push_back(gu);
        }
      }    
      // CERR << "do_threadmult end " << CLOCK() << std::endl;
      sort(v.begin(),v.end());
      // CERR << "do_threadmult sort end " << CLOCK() << std::endl;
      argptr->clock = CLOCK() - argptr->clock;
      argptr->status = 2;
      return &v;
    }
  
    template<class T,class U,class R>
    bool threadmult(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,U degdiv,const R & reduce,size_t possible_size=100){
      if (!threads_allowed)
        return false;
      v.clear();
      if (v1.empty() || v2.empty())
        return true;
      size_t v1si=v1.size(),v2si=v2.size();
      double v1s=double(v1si),v2s=double(v2si);
      if (v2si<v1si)
        return threadmult<T,U>(v2,v1,v,degdiv,reduce,possible_size);
      unsigned threads_time=0;
  
      int nthreads=threads;
  
      if (nthreads<2) 
        ; // return false;
      double v1v2=v1s*v2s;
      T * prod =0;
      U u1=v1.front().u, u2=v2.front().u;
      U u12=u1+u2; // size of the array for array multiplication
      // compare u12 and v1v2*ln(v1v2)
      if ( heap_mult>=0 && possible_size>100 && u12<512e6/sizeof(T) && u12<v1v2*std::log(double(possible_size))*2){
        if (debug_infolevel>20)
  	CERR << "array multiplication, v1 size " << v1s << " v2 size " << v2s << " u1+u2 " << u12 << std::endl;
        // array multiplication
        prod = new T[u12+1];
        for (U u=0;u<=u12;++u)
  	prod[u]=T(0);
      }
      // if array multiplication is faster, set prod
      bool use_heap = (heap_mult<0) || (heap_mult>0 && v1v2>heap_mult);
      if (!prod && use_heap 
  	//&& nthreads<2
  	) // multi-thread heap disabled because of locks by inserting in chains 
        return false;  
      if (debug_infolevel>20){
        CERR << "// " << CLOCK() << "using threaded " ;
        if (use_heap)
  	CERR << "heap";
        else 
  	CERR << "hash";
        CERR << " multiplication" << std::endl;
      }
      unsigned d2=v2.front().u/degdiv,deg1v=v1.front().u/degdiv+d2;
      int cur_deg=-1,prev_deg=d2;
      // initialize iterators to the degree beginning
      std::vector< typename std::vector< T_unsigned<T,U> >::const_iterator > v1it,v2it;
      typename std::vector< T_unsigned<T,U> >::const_iterator it=v2.begin(),itend=v2.end();
      for (v2it.push_back(it);it!=itend;++it){
        cur_deg=it->u/degdiv;
        if (cur_deg==prev_deg)
  	continue;
        for (int i=prev_deg-1;i>=cur_deg;--i){
  	v2it.push_back(it);
  	if (!i)
  	  break;
        }
        prev_deg=cur_deg;
      }
      for (int i=prev_deg-1;i>=0;--i)
        v2it.push_back(it);
      v2it.push_back(it);
      it=v1.begin();itend=v1.end(); prev_deg=v1.front().u/degdiv;
      for (v1it.push_back(it);it!=itend;++it){
        cur_deg=it->u/degdiv;
        if (cur_deg==prev_deg)
  	continue;
        for (int i=prev_deg-1;i>=cur_deg;--i){
  	v1it.push_back(it);
  	if (!i)
  	  break;
        }
        prev_deg=cur_deg;
      }
      for (int i=prev_deg-1;i>=0;--i)
        v1it.push_back(it);
      v1it.push_back(it);
      // degree of product wrt to the main variable
      // will launch deg1v+1 threads to compute each degree
      pthread_t tab[deg1v+1];
      // threadmult_t<T,U> arg[deg1v+1];
      threadmult_t<T,U,R> * arg=new threadmult_t<T,U,R>[deg1v+1];
      possible_size=0;
      int res=0;
      int i=deg1v;
      bool smallindex=(v1s<65535 && v2s<65535);
      if (
  	// true || 
  	nthreads==1){
        U_unsigned<U> *heapptr=new U_unsigned<U>[v1si];
        std::vector< vector_size64< std::pair<unsigned,unsigned> > >* vindexptr=smallindex?0:(new std::vector< vector_size64< std::pair<unsigned,unsigned> > >(v1si));
        std::vector< vector_size32< std::pair<unsigned short,unsigned short> > >* vsmallindexptr=smallindex?(new std::vector< vector_size32< std::pair<unsigned short,unsigned short> > >(v1si)):0;
        for (;i>=0;--i){
  	arg[i].v1ptr=&v1;
  	arg[i].v1ptrs=&v1it;
  	arg[i].v2ptrs=&v2it;
  	arg[i].vptr = new std::vector< T_unsigned<T,U> >;
  	if (i!=int(deg1v))
  	  arg[i].vptr->reserve(arg[i+1].vptr->size());
  	arg[i].degdiv=degdiv;
  	arg[i].current_deg=i;
  	arg[i].reduce=reduce;
  	arg[i].status=0;
  	arg[i].prod=prod;
  	if (use_heap){
  	  arg[i].use_heap=use_heap;
  	  arg[i].heapptr=heapptr;
  	  arg[i].vindexptr=vindexptr;
  	  arg[i].vsmallindexptr=vsmallindexptr;
  	}
  	else {
  	  arg[i].use_heap=false;
  	  arg[i].heapptr=0;
  	  arg[i].vindexptr=0;
  	  arg[i].vsmallindexptr=0;
  	}
  	if (debug_infolevel>30)
  	  CERR << "Computing degree " << i << " " << CLOCK() << std::endl;
  	do_threadmult<T,U,R>(&arg[i]);
  	threads_time += arg[i].clock;
  	possible_size += arg[i].vptr->size();	
        }
        delete [] heapptr;
        delete vindexptr;
      }
      else {
        std::vector<int> in_progress;
        // create initials threads
        for (int j=0;i>=0 && j<nthreads;--i,++j){
  	in_progress.push_back(i);
  	arg[i].v1ptr=&v1;
  	arg[i].v1ptrs=&v1it;
  	arg[i].v2ptrs=&v2it;
  	arg[i].vptr = new std::vector< T_unsigned<T,U> >;
  	arg[i].degdiv=degdiv;
  	arg[i].current_deg=i;
  	arg[i].reduce=reduce;
  	arg[i].status=0;
  	arg[i].prod=prod;
  	if (use_heap){
  	  arg[i].use_heap=use_heap;
  	  arg[i].heapptr=new U_unsigned<U>[v1si];
  	  arg[i].vindexptr=smallindex?0:(new std::vector< vector_size64< std::pair<unsigned,unsigned> > >(v1si));
  	  arg[i].vsmallindexptr=smallindex?(new std::vector< vector_size32< std::pair<unsigned short,unsigned short> > >(v1si)):0;
  	}
  	else {
  	  arg[i].use_heap=false;
  	  arg[i].heapptr=0;
  	  arg[i].vindexptr=0;
  	  arg[i].vsmallindexptr=0;
  	}
  	res=pthread_create(&tab[i],(pthread_attr_t *) NULL,do_threadmult<T,U,R>,(void *) &arg[i]);
  	if (res){
  	  // should cancel previous threads and delete created arg[i].vptr
  	  delete [] arg;
  	  return false;
  	}
        } // end initial thread creation
        // now wait for each thread and create replacement threads
        while (1){
  	int nb=in_progress.size();
  	int todo=0,towait=0;
  	for (int j=0;j<nb;++j){
  	  int k=in_progress[j];
  	  if (k>=0){
  	    int concurrent=arg[k].status;
  	    if (concurrent==2){
  	      void * ptr;
  	      pthread_join(tab[k],&ptr);
  	      threads_time += arg[k].clock;
  	      possible_size += arg[k].vptr->size();
  	      if (i>=0){
  		arg[i].v1ptr=&v1;
  		arg[i].v1ptrs=&v1it;
  		arg[i].v2ptrs=&v2it;
  		arg[i].vptr = new std::vector< T_unsigned<T,U> >;
  		arg[i].vptr->reserve(arg[k].vptr->size());
  		arg[i].degdiv=degdiv;
  		arg[i].current_deg=i;
  		arg[i].reduce=reduce;
  		arg[i].status=0;
  		arg[i].use_heap=use_heap;
  		arg[i].prod=prod;
  		arg[i].heapptr=arg[k].heapptr;
  		arg[i].vindexptr=arg[k].vindexptr;
  		arg[i].vsmallindexptr=arg[k].vsmallindexptr;
  		res=pthread_create(&tab[i],(pthread_attr_t *) NULL,do_threadmult<T,U,R>,(void *) &arg[i]);
  		if (res){
  		  // should cancel previous threads and delete created arg[i].vptr
  		  delete [] arg;
  		  return false;
  		}
  		in_progress[j]=i;
  		--i;
  		++todo;
  	      } // end if (i>=0)
  	      else 
  		in_progress[j]=-1;
  	    } // if (concurrent!=2)
  	    else
  	      ++towait;
  	  } // end if (k>=0)
  	} // end for (j=0;j<=nb;++j)
  	if (!todo && !towait)
  	  break;
  	if (towait)
  	  usleep(1);
        } // end while(1)
        if (use_heap){
  	i=deg1v;
  	for (int j=0;i>=0 && j<nthreads;--i,++j){
  	  delete [] arg[i].heapptr;
  	  if (arg[i].vindexptr) delete arg[i].vindexptr;
  	  if (arg[i].vsmallindexptr) delete arg[i].vsmallindexptr;
  	}
        }
      } // end else of if (nthreads==1)
      if (debug_infolevel>30)
        CERR << "Begin copy " << CLOCK() << std::endl;
      // store to v
      if (prod){
        int n=0;
        for (U u=u12;;--u){
  	if (!is_zero(prod[u]))
  	  ++n;
  	if (!u)
  	  break;
        }
        v.reserve(n);
        for (U u=u12;;--u){
  	if (!is_zero(prod[u]))
  	  v.push_back(T_unsigned<T,U>(prod[u],u));
  	if (!u)
  	  break;
        }
        delete [] prod ;      
        for (int i=deg1v;i>=0;--i){
  	delete arg[i].vptr;
        }
      }
      else {
        v.reserve(possible_size);
        for (int i=deg1v;i>=0;--i){
  	typename std::vector< T_unsigned<T,U> >::const_iterator it=arg[i].vptr->begin(),itend=arg[i].vptr->end();
  	for (;it!=itend;++it){
  	  v.push_back(*it);
  	}
  	delete arg[i].vptr;
        }
        /* 
        v=std::vector< T_unsigned<T,U> >(possible_size);
        typename std::vector< T_unsigned<T,U> >::const_iterator jt=v.begin();
        for (int i=deg1v;i>=0;--i){
  	typename std::vector< T_unsigned<T,U> >::const_iterator it=arg[i].vptr->begin(),itend=arg[i].vptr->end();
  #ifdef VISUALC
  	for (;it!=itend;++jt,++it){
  	  *jt=*it;
  	}
  #else
  	memcpy(*(void **) &jt,*(void **)&it,(itend-it)*sizeof(T_unsigned<T,U>));
  	jt += (itend-it);
  #endif
  	delete arg[i].vptr;
        }
        */
      }
      if (debug_infolevel>30)
        CERR << "End copy " << CLOCK() << std::endl;
      delete [] arg;
      return true;
    }
  
  #else // PTHREAD
    template<class T,class U,class R>
    bool threadmult(const std::vector< T_unsigned<T,U> > & v1,const std::vector< T_unsigned<T,U> > & v2,std::vector< T_unsigned<T,U> > & v,U degdiv,const R& reduce,size_t possible_size=100){
      return false;
    }
  
  #endif // PTHREAD
  
    template<class U>
    void partial_degrees2vars(const index_t & d,std::vector<U> & vars){
      int dim=int(d.size());
      vars[dim-1]=1;
      for (int i=dim-2;i>=0;--i){
        vars[i]=(1+d[i+1])*vars[i+1];
      }
    }
  
    template<class U>
    void partial_degrees(U u,const std::vector<U> & vars,index_t & res){
      for (int k=int(vars.size())-1;k>0;--k){
        U v=u%vars[k-1];
        res[k]=v/vars[k];
      }
      res[0]=u/vars[0];
    }
  
    template<class T,class U>
    void partial_degrees(const std::vector< T_unsigned<T,U> > & a,const std::vector<U> & vars,index_t & res){
      typename std::vector< T_unsigned<T,U> >::const_iterator it=a.begin(),itend=a.end();
      int dim=int(vars.size());
      res.clear(); res.resize(dim);
      index_t cur(dim);
      for (;it!=itend;++it){
        partial_degrees(it->u,vars,cur);
        index_lcm(res,cur,res);
      }
    }
  
    // convert u monomial from vars source to vars target, tmp is a temp index_t
    template<class U> 
    void convert(U & u,const std::vector<U> & source,const std::vector<U> & target,index_t & tmp){
      partial_degrees(u,source,tmp);
      u=0;
      for (int i=int(source.size())-1;i>=0;--i){
        u += target[i]*tmp[i];
      }
    }
  
    template<class T,class U>
    void convert(std::vector< T_unsigned<T,U> > & a,const std::vector<U> & source,const std::vector<U> & target){
      index_t tmp(source.size());
      typename std::vector< T_unsigned<T,U> >::iterator it=a.begin(),itend=a.end();
      for (;it!=itend;++it){
        convert(it->u,source,target,tmp);
      }
    }
  
    inline bool hashdivrem_finish_later(longlong a){ return false;}
    inline bool hashdivrem_finish_later(double a){return false;}
    inline bool hashdivrem_finish_later(int a){ return false; }
    inline bool hashdivrem_finish_later(const vecteur & v){ return false; }
    inline bool hashdivrem_finish_later(const std::vector<int> & v){ return false; }
  #ifdef INT128
    inline bool hashdivrem_finish_later(int128_t a){return false;}
  #endif
    
    inline bool hashdivrem_finish_later(const gen & a){return true;}
    inline bool hashdivrem_finish_later(const my_mpz & a){return true;}
  #ifdef HAVE_GMPXX_H
    inline bool hashdivrem_finish_later(const mpz_class & a){return true;}
  #endif
  
    template<class U>
    inline bool one_index_smaller(U u,U v,const std::vector<int> & varsshift){
      if (u<v) 
        return true;
      return false;
      // the code below is too slow
      std::vector<int>::const_iterator it=varsshift.begin(),itend=varsshift.end();
      for (;it!=itend;++it){
        int shift=*it;
        U u1=(u>>shift);
        U v1=(v>>shift);
        if (u1<v1) 
  	return true;
        u -= (u1 << shift);
        v -= (v1 << shift);
      }
      return false;
    }
  
    // #define HEAP_STATS
    // note that U may be of type vector of int or an int
    // + is used to multiply monomials and - to divide
    // / should return the quotient of the main variable exponent
    // > should return true if a monomial has main degree >
    // vars is the list of monomials x,y,z,etc. as translated in U type
    // quo_only==-3 means heap div (compute quo and rem)
    // quo_only==-2 means compute quotient only using heap div
    // quo_only==-1 heap quotient then guess between heap remainder 
    //              or r=a-b*q must be done by caller (if returns 2)
    // quo_only==0 array division or univariate with hashmap
    // quo_only==1 means we want to check that b divides a
    // quo_only==2 means we know that b divides a and we search the cofactor
    // quo_only==3 array division if enough memory
    // quo_only>3 univariate division with hashmap or map
    // for coefficients monomial storage
    // returns 1 if ok, 2 if ok but remainder not computed, 0 or -1 otherwise
    template<class T,class U,class R>
    int hashdivrem(const std::vector< T_unsigned<T,U> > & a,const std::vector< T_unsigned<T,U> > & b,std::vector< T_unsigned<T,U> > & q,std::vector< T_unsigned<T,U> > & r,const std::vector<U> & vars,const R & reduce,double qmax,bool allowrational,int quo_only=0){
      // CERR << "hashdivrem dim " << vars.size() << " clock " << CLOCK() << std::endl;
      q.clear();
      r.clear();
      if (a.empty()){
        return 1;
      }
      if (b.empty()){
        r=a;
        return 1;
      }
      U mainv=vars.front();
      unsigned mainvar=0;
      for (; (mainv >>= 1) ;++mainvar)
        ;
      U bu=b.front().u;
      int bdeg=int(bu >> mainvar);
      int adeg=int(a.front().u >> mainvar),rdeg;
      if (adeg<bdeg){
        r=a;
        return 1;
      }
      U rstop=U(bdeg) << mainvar;
      typename std::vector< T_unsigned<T,U> >::iterator it1,it1end;
      typename std::vector< T_unsigned<T,U> >::const_iterator it2,it2end,itbbeg=b.begin(),itbend=b.end(),ita=a.begin(),itaend=a.end(),cit,citend,qbeg;
      T binv=b.front().g;
      if (!is_zero(reduce))
        binv=invmod(binv,reduce);
      if (b.size()==1){
        for (cit=a.begin(),citend=a.end();cit!=citend;++cit){
  	if (rstop>cit->u)
  	  break;
  	if (cit->u<b.front().u)
  	  return 0;
  	T qn;
  	// qn=reduce?smod(cit->g*binv,reduce):cit->g/binv;
  	if (!is_zero(reduce))
  	  type_operator_reduce(cit->g,binv,qn,reduce);
  	else
  	  qn=cit->g/binv;
  	if (qmax!=0 && qn>qmax)
  	  return -1;
  	if (is_zero(reduce) && !allowrational && !is_zero(cit->g % binv))
  	  return 0;
  	q.push_back(T_unsigned<T,U>(qn,cit->u-b.front().u));
        }
        for (;cit!=citend;++cit)
  	r.push_back(*cit);
        // CERR << "hashdivrem end dim " << vars.size() << " clock " << CLOCK() << std::endl;
        return 1;
      }
      unsigned as=unsigned(a.size()),bs=unsigned(b.size());
      double v1v2=double(as)*bs;
      // FIXME, if bdeg==0
      if (
  	(!quo_only || quo_only==3) &&
  	//quo_only==3 && //is_zero(reduce) &&
  	bdeg && as>=a.front().u/25. // array div disabled, probably too much memory used
  	&& heap_mult>=0 && a.front().u < 512e6/sizeof(T)){
        U umax=a.front().u,u;
        if (debug_infolevel>1)
  	CERR << CLOCK()*1e-6 << " array division, a size " << a.size() << " b size " << b.size() << " u " << umax << std::endl;
        // array division
        T * rem = new T[unsigned(umax+1)];
        for (u=0;u<=umax;++u)
  	rem[u]=T(0);
        // find maincoeff of b
        std::vector< T_unsigned<T,U> > lcoeffb;
        for (cit=b.begin(),citend=b.end();cit!=citend;++cit){
  	register U u=cit->u;
  	if (rstop>u)
  	  break;
  	lcoeffb.push_back(T_unsigned<T,U>(cit->g,u-rstop));
        }
        // fill rem with a
        for (cit=a.begin(),citend=a.end();cit!=citend;++cit){
  	rem[cit->u]=cit->g;
        }
        std::vector< T_unsigned<T,U> > maincoeff,quo,tmp;
        for (rdeg=adeg;rdeg>=bdeg;--rdeg){
  	U ushift=U(rdeg) << mainvar;
  	maincoeff.clear();
  	quo.clear();
  	tmp.clear();
  	// find leading coeff of rem
  	for (;umax>=ushift;--umax){
  	  T & g=rem[umax];
  	  if (!is_zero(g))
  	    maincoeff.push_back(T_unsigned<T,U>(g,umax-ushift));
  	}
  	if (maincoeff.empty()) 
  	  continue;
  	ushift=U(rdeg-bdeg) << mainvar;
  	// divide maincoeff by lcoeff(b)
  	// this is done by recursion except when univariate
  	if (vars.size()==1){
  	  if (lcoeffb.size()!=1 || maincoeff.size()!=1)
  	    return 0;
  	  T res;
  	  if (!is_zero(reduce))
  	    type_operator_reduce(maincoeff.front().g,binv,res,reduce);// res=smod(maincoeff.front().g*binv,reduce);
  	  else {
  	    res=maincoeff.front().g/binv;
  	    if (qmax && res>qmax){
  	      delete [] rem;
  	      return -1;
  	    }
  	    if (!allowrational && !is_zero(maincoeff.front().g%binv) ){
  	      delete [] rem;
  	      return 0;
  	    }
  	  }
  	  quo.push_back(T_unsigned<T,U>(res,maincoeff.front().u+ushift));
  	  q.push_back(quo.back());
  	}
  	else {
  	  int recdivres=hashdivrem(maincoeff,lcoeffb,quo,tmp,std::vector<U>(vars.begin()+1,vars.end()),reduce,qmax,allowrational);
  	  if (recdivres<1){
  	    delete [] rem;
  	    return recdivres;
  	  }
  	  if (!tmp.empty()){
  	    delete [] rem;
  	    return 0;
  	  }
  	  for (it1=quo.begin(),it1end=quo.end();it1!=it1end;++it1){
  	    it1->u += ushift;
  	    q.push_back(*it1);
  	  }
  	}
  	// rem -= quo*b
  	for (cit=itbbeg;cit!=itbend;++cit){
  	  T g1=-cit->g;
  	  U u,u1=cit->u;
  	  // int deg1=u1/mainvar;
  	  if (!is_zero(reduce)){
  	    for (it2=quo.begin(),it2end=quo.end();it2!=it2end;++it2){
  	      u=u1+it2->u;
  	      register int deg = int(u >> mainvar); // deg=deg1+it2->u/mainvar;
  	      if (deg<rdeg){
  		type_operator_plus_times_reduce(g1,it2->g,rem[u],reduce); 
  	      }
  	    }
  	  }
  	  else {
  	    for (it2=quo.begin(),it2end=quo.end();it2!=it2end;++it2){
  	      u=u1+it2->u;
  	      register int deg=int(u >> mainvar);
  	      if (deg<rdeg){
  		type_operator_plus_times(g1,it2->g,rem[u]);	      
  	      }
  	    }
  	  }
  	}
  	// end rem -= quo*b
        }
        // move rem to r
        for (;;--umax){
  	T & g=rem[umax];
  	if (!is_zero(g))
  	  r.push_back(T_unsigned<T,U>(g,umax));
  	if (umax==0)
  	  break;
        }
        delete [] rem;
        return 1;
      } // end array division
      bool use_heap=false && 
        (heap_mult>0 
         && v1v2>=heap_mult
         );
  #if 1 // heap division
      if (heap_mult<0 || use_heap ||
  	(quo_only && quo_only<3)){
  	  // quo_only<3){
        bool norem=quo_only==2 || quo_only==-2;
        norem=false; // currently disabled, it's not clear it wins something
        std::vector<int> varsshift; // vars=2^varsshift
        if (norem){
  	for (size_t i=0;i<vars.size();++i){
  	  int j=sizeinbase2(vars[i])-1;
  	  if (vars[i]!=(U(1)<<j))
  	    norem=false;
  	  varsshift.push_back(j);
  	}
        }
  #ifdef HEAP_STATS
        unsigned chain=0,nochain=0,typeopreduce=0,nullq=0;
  #endif
        if (debug_infolevel>1)
  	CERR << CLOCK()*1e-6 << " heap division, a size " << a.size() << " b size " << b.size() << " vars " << vars << std::endl;
        // heap division:
        // ita an iterator on a, initial value a.begin()
        // a heap with the current state of q*b, initialized to empty heap
        // loop:
        // compare degree of the iterator in a and heap top
        // if both are < rstop (degree of b) break
        // if equal: add monomial of a to heap top coeff, move a iterator
        // if a>: add term to q and to the heap by mult/dividing by binv
        // if heap >: ++ heaptop b iterator, add term to q and to the heap
        // after division loop:
        // finish the heap multiplication into r
        //
        // vindex[i] is the heap chain corresponding to index i
        // it contains pairs of index in g and q
        std::vector< std::vector< std::pair<unsigned,unsigned> > > vindex(bs) ; // ,std::vector< std::pair<unsigned,unsigned> >(4));
        U_unsigned<U> * heap = new U_unsigned<U>[bs], * heapbeg =heap, * heapend=heap ;
        T g;
        U heapu,u;
        // qnouveau is used to store new pairs of products g*q when the monomial in q is not yet known
        std::vector< std::pair<unsigned,unsigned> > qnouveau(bs);
        std::vector< std::pair<unsigned,unsigned> > nouveau;
        // initialize qnouveau to fill the heap when first quotient term computed
        for (unsigned int i=0;i<bs;++i){
  	qnouveau[i]=std::pair<unsigned,unsigned>(i,0);
  	*(heap+i)=U_unsigned<U>(0,i);
        }
        for (;;){
  	g=T(0);
  	// compare current position in a with heap top
  	if (heapbeg!=heapend){
  	  heapu=heapbeg->u;
  	  if (ita!=itaend && ita->u>heapu){
  	    if (ita->u>=bu){
  	      heapu=ita->u;
  	      g=ita->g;
  	      ++ita;
  	    }
  	  }
  	  else {
  	    if (heapu>=bu){
  	      // find all pairs having heapu as monomial
  	      std::vector< std::pair<unsigned,unsigned> >::iterator it,itend;
  	      nouveau.clear();
  	      while (heapend!=heapbeg && heapu==heapbeg->u){
  		//nouveau.clear();
  		// add all elements of the top chain	
  		std::vector< std::pair<unsigned,unsigned> > & V=vindex[heapbeg->v];
  		it=V.begin();
  		itend=V.end();
  		qbeg=q.begin();
  		size_t qsize=q.size();
  		for (;it!=itend;++it){
  #ifdef HEAP_STATS
  		  ++typeopreduce;
  #endif
  		  unsigned & its=it->second;
  		  type_operator_plus_times_reduce((itbbeg+it->first)->g,(qbeg+its)->g,g,reduce);
  		  // increment 2nd poly index of the elements of the top chain
  		  ++its;
  		  if (its<qsize){
  		    nouveau.push_back(*it);
  		  }
  		  else // wait for computation of a new term of a before adding to the heap 
  		    qnouveau.push_back(*it);
  		}
  		// erase top node, 
  #ifdef USTL
  		ustl::pop_heap(heapbeg,heapend);
  #else
  		std::pop_heap(heapbeg,heapend);
  #endif
  		// std::pop_heap(heapbeg,heapend);
  		--heapend;
  	      } // while heapend!=heapbeg && heapu==
  	      {
  		// push each element of the incremented top chain 
  		it=nouveau.begin();
  		itend=nouveau.end();
  		for (;it!=itend;++it) {
  		  u=(itbbeg+it->first)->u+(qbeg+it->second)->u;
  		  if (norem && one_index_smaller(u,bu,varsshift)) continue;
  		  // check if u is in the path to the root of the heap
  		  unsigned holeindex=unsigned(heapend-heapbeg),parentindex;
  		  if (holeindex && u==heapbeg->u){
  #ifdef HEAP_STATS
  		    ++chain;
  #endif
  		    vindex[heapbeg->v].push_back(*it);
  		    continue;
  		  }
  		  bool done=false;
  		  while (holeindex){
  		    parentindex=(holeindex-1) >> 1;
  		    U pu=(heapbeg+parentindex)->u;
  		    if (u<pu)
  		      break;
  		    if (u==pu){
  		      done=true;
  		      vindex[(heapbeg+parentindex)->v].push_back(*it);
  #ifdef HEAP_STATS
  		      ++chain;
  #endif
  		      break;
  		    }
  		    holeindex=parentindex;
  		  }
  		  if (!done){
  #ifdef HEAP_STATS
  		    ++nochain;
  #endif
  		    // not found, add a new node to the heap
  		    std::vector< std::pair<unsigned,unsigned> > & V=vindex[heapend->v];
  		    V.clear();
  		    V.push_back(*it);
  		    heapend->u=u;
  		    ++heapend;
  #ifdef USTL
  		    ustl::push_heap(heapbeg,heapend);
  #else
  		    std::push_heap(heapbeg,heapend);
  #endif
  		    // std::push_heap(heapbeg,heapend);
  		  }
  		} // end adding incremented pairs from nouveau
  	      } // end loop on monomial of the heap having the same u
  	      if (heapu==ita->u){
  		g = ita->g -g ; // FIXME must be reduced!
  		if (!is_zero(reduce))
  		  g = g % reduce;
  		++ita;
  	      }
  	      else
  		g=-g;
  	      if (is_zero(g)){
  #ifdef HEAP_STATS
  		++nullq;
  #endif
  		continue;
  	      }
  	    } // end if (heapu>=bu)
  	  } // end else ita->u>heapu
  	} // if heap non empty
  	else {
  	  if (ita!=itaend && (heapu=ita->u)>=bu){
  	    g=ita->g;
  	    ++ita;
  	  } 
  	} // end if (!heap.empty())
  	if (is_zero(g))
  	  break;
  	if (is_zero(reduce) && !allowrational && !is_zero(g % binv)){
  	  delete [] heap;
  	  return 0;
  	}
  	// g=reduce?smod(g*binv,reduce):g/binv;
  	if (!is_zero(reduce))
  	  type_operator_reduce(g,binv,g,reduce);
  	else
  	  g=g/binv;
  	if (qmax && (g>qmax || -g>qmax)){
  	  delete [] heap;
  	  return -1;
  	}
  	// FIXME check that heapu has all components>=bu, otherwise should be in remainder
  	// new quotient term
  	q.push_back(T_unsigned<T,U>(g,heapu-bu));
  	// explore qnouveau and add terms to the heap
  	std::vector< std::pair<unsigned,unsigned> >::iterator it,itend;
  	it=qnouveau.begin();
  	itend=qnouveau.end();
  	U prevu=0; int previndex=-1;
  	for (;it!=itend;++it){
  	  if (!it->first) // leading term of b already taken in account
  	    continue;
  	  u=(itbbeg+it->first)->u+(q.begin()+it->second)->u;
  	  if (norem && one_index_smaller(u,bu,varsshift)) continue;
  	  if (u==prevu && previndex>=0){
  #ifdef HEAP_STATS
  	    ++chain;
  #endif
  	    vindex[previndex].push_back(*it);
  	    continue;
  	  }
  	  prevu=u;
  	  // check if u is in the path to the root of the heap
  	  unsigned holeindex=unsigned(heapend-heapbeg),parentindex;
  	  if (holeindex && u==heapbeg->u){
  #ifdef HEAP_STATS
  	    ++chain;
  #endif
  	    vindex[previndex=heapbeg->v].push_back(*it);
  	    continue;
  	  }
  	  bool done=false;
  	  while (holeindex){
  	    parentindex=(holeindex-1) >> 1;
  	    U pu=(heapbeg+parentindex)->u;
  	    if (u<pu)
  	      break;
  	    if (u==pu){
  #ifdef HEAP_STATS
  	      ++chain;
  #endif
  	      done=true;
  	      vindex[previndex=(heapbeg+parentindex)->v].push_back(*it);
  	      break;
  	    }
  	    holeindex=parentindex;
  	  }
  	  if (!done){
  #ifdef HEAP_STATS
  	    ++nochain;
  #endif
  	    // not found, add a new node to the heap
  	    std::vector< std::pair<unsigned,unsigned> > & V=vindex[(previndex=heapend->v)];
  	    V.clear();
  	    V.push_back(*it);
  	    heapend->u=u;
  	    ++heapend;
  #ifdef USTL
  	    ustl::push_heap(heapbeg,heapend);
  #else
  	    std::push_heap(heapbeg,heapend);
  #endif
  	    // std::push_heap(heapbeg,heapend);
  	  }
  	} // end adding incremented pairs from qnouveau
  	qnouveau.clear();
        } // for (;;)
  #ifdef HEAP_STATS
        if (debug_infolevel)
  	CERR << "chain " << chain << ", nochain " << nochain << ", type_op_reduce " << typeopreduce << " null quotients" << nullq << std::endl;
  #endif
        // r still empty
        if (debug_infolevel>2)
  	CERR << CLOCK()*1e-6 << " Finished computing quotient, size " << q.size() << std::endl ;
        if (quo_only==2 || quo_only==-2){
  	delete [] heap;
  	return 1;
        }
        if (quo_only==-1 ){
  	// try to compare heap mult and array mult, return for array mult only
  	double qb=double(q.size())*b.size();
  	qb /= a.size();
  	if (debug_infolevel>1)
  	  CERR << CLOCK()*1e-6 << " qb=" << qb << std::endl;
  	if (qb>100){
  	  // the coefficients might be not optimal (mpz_class instead of int)
  	  if (hashdivrem_finish_later(a.front().g)){
  	    delete [] heap;
  	    return 2;
  	  }
  	  // the monomials are not stored efficiently for array *, compress
  	  index_t adeg,bdeg,qdeg,bqdeg;
  	  partial_degrees(a,vars,adeg);
  	  partial_degrees(b,vars,bdeg);
  	  partial_degrees(q,vars,qdeg);
  	  bqdeg=bdeg+qdeg;
  	  bqdeg=index_lcm(bqdeg,adeg);
  	  std::vector<U> newvars(vars.size());
  	  partial_degrees2vars(bqdeg,newvars);
  	  std::vector< T_unsigned<T,U> > acopy(a),bcopy(b),qcopy(q),bq;
  	  convert(acopy,vars,newvars);
  	  convert(bcopy,vars,newvars);
  	  convert(qcopy,vars,newvars);
  	  if (debug_infolevel>1)
  	    CERR << CLOCK()*1e-6 << " compress monomials done" <<std::endl;
  	  if (!threadmult(bcopy,qcopy,bq,newvars.front(),reduce,a.size()))
  	    smallmult(bcopy,qcopy,bq,reduce,as);
  	  smallsub(acopy,bq,r);
  	  if (debug_infolevel>1)
  	    CERR << CLOCK()*1e-6 << " uncompress monomials" <<std::endl;
  	  convert(r,newvars,vars);
  	  if (debug_infolevel>1)
  	    CERR << CLOCK()*1e-6 << " uncompress monomials end"<< std::endl;
  	  delete [] heap;
  	  return 1;
  	}
        }
        // now q is computed, combine a and remaining product to r
        for (;heapbeg!=heapend;){
  	heapu=heapbeg->u;
  	if (ita!=itaend){
  	  if (ita->u>heapu){
  	    r.push_back(*ita);
  	    ++ita;
  	    continue;
  	  }
  	  if (ita->u<heapu)
  	    g=T(0);
  	  else {
  	    g=-ita->g; // opposite sign since we neg at the end
  	    ++ita;
  	  }
  	} // ita!=itaend
  	// add all terms from the heap with same monomial
  	while (heapbeg!=heapend && heapbeg->u==heapu){
  	  qnouveau.clear();
  	  std::vector< std::pair<unsigned,unsigned> >::iterator it,itend;
  	  it=vindex[heapbeg->v].begin();
  	  itend=vindex[heapbeg->v].end();
  	  for (;it!=itend;++it){
  	    unsigned & its=it->second;
  	    type_operator_plus_times_reduce((itbbeg+it->first)->g,(q.begin()+its)->g,g,reduce);
  	    // increment 2nd poly index of the elements of the top chain
  	    ++its;
  	    if (its<q.size())
  	      qnouveau.push_back(*it);
  	  }
  	  // erase top node, 
  #ifdef USTL
  	  ustl::pop_heap(heapbeg,heapend);
  #else
  	  std::pop_heap(heapbeg,heapend);
  #endif
  	  // std::pop_heap(heapbeg,heapend);
  	  --heapend;
  	  // push each element of the incremented top chain 
  	  it=qnouveau.begin();
  	  itend=qnouveau.end();
  	  U prevu=0; int previndex=-1;
  	  for (;it!=itend;++it){
  	    u=(itbbeg+it->first)->u+(q.begin()+it->second)->u;
  	    if (u==prevu && previndex>=0){
  	      vindex[previndex].push_back(*it);
  	      continue;
  	    }
  	    prevu=u;
  	    // check if u is in the path to the root of the heap
  	    unsigned holeindex=unsigned(heapend-heapbeg),parentindex;
  	    if (holeindex && u==heapbeg->u){
  	      vindex[(previndex=heapbeg->v)].push_back(*it);
  	      continue;
  	    }
  	    bool done=false;
  	    while (holeindex){
  	      parentindex=(holeindex-1) >> 1;
  	      U pu=(heapbeg+parentindex)->u;
  	      if (u<pu)
  		break;
  	      if (u==pu){
  		done=true;
  		vindex[(previndex=(heapbeg+parentindex)->v)].push_back(*it);
  		break;
  	      }
  	      holeindex=parentindex;
  	    }
  	    if (!done){
  	      // not found, add a new node to the heap
  	      std::vector< std::pair<unsigned,unsigned> > & V=vindex[(previndex=heapend->v)];
  	      V.clear();
  	      V.push_back(*it);
  	      heapend->u=u;
  	      ++heapend;
  #ifdef USTL
  	      ustl::push_heap(heapbeg,heapend);
  #else
  	      std::push_heap(heapbeg,heapend);
  #endif
  	      // std::push_heap(heapbeg,heapend);
  	    }
  	  } // end adding incremented pairs from nouveau
  	} // end while heapbeg!=heapend && heapbeg->u==heapu
  	// add -g to r
  	if (!is_zero(g))
  	  r.push_back(T_unsigned<T,U>(-g,heapu));
        } // end for (heapbeg!=heapend)
        for (;ita!=itaend;++ita)
  	r.push_back(*ita);
        delete [] heap;
        return 1;
      }    
  #endif // heap division
  #ifdef HASH_MAP_NAMESPACE
      typedef HASH_MAP_NAMESPACE::hash_map< U,T,hash_function_unsigned_object> hash_prod ;
      std::vector< hash_prod > produit(adeg+1);
  #else
  #ifdef USTL
      typedef ustl::map<U,T> hash_prod;
  #else
      typedef std::map<U,T> hash_prod;
  #endif
      std::vector< hash_prod > produit(adeg+1); 
  #endif    
      typename hash_prod::iterator prod_it,prod_itend;
      hash_prod * hashptr;
      // find maincoeff of b
      std::vector< T_unsigned<T,U> > lcoeffb;
      for (cit=b.begin(),citend=b.end();cit!=citend;++cit){
        register U u=cit->u;
        if (rstop>u)
  	break;
        lcoeffb.push_back(T_unsigned<T,U>(cit->g,u-rstop));
      }
      // copy a to remainder
      for (cit=a.begin(),citend=a.end();cit!=citend;++cit){ 
        U u=cit->u; 
        produit[unsigned(u >> mainvar)][u]=cit->g; 
      }
      for (rdeg=adeg;rdeg>=bdeg;--rdeg){
        if (debug_infolevel>20)
  	CERR << "hashdivrem degree " << rdeg << " " << CLOCK() << std::endl;
        if (produit[rdeg].empty())
  	continue;
        // find degree of remainder and main coeff
        std::vector< T_unsigned<T,U> > maincoeff,quo,tmp;
        U ushift=U(rdeg) << mainvar;
        for (prod_it=produit[rdeg].begin(),prod_itend=produit[rdeg].end();prod_it!=prod_itend;++prod_it){
  	if (!is_zero(prod_it->second))
  	  maincoeff.push_back(T_unsigned<T,U>(prod_it->second,prod_it->first-ushift));
        }
        if (maincoeff.empty()) 
  	continue;
        sort(maincoeff.begin(),maincoeff.end());
        ushift=U(rdeg-bdeg) << mainvar;
        // divide maincoeff by lcoeff(b)
        // this is done by recursion except when univariate
        if (vars.size()==1){
  	if (lcoeffb.size()!=1 || maincoeff.size()!=1)
  	  return 0;
  	T res;
  	if (!is_zero(reduce))
  	  type_operator_reduce(maincoeff.front().g,binv,res,reduce);// res=smod(maincoeff.front().g*binv,reduce);
  	else {
  	  res=maincoeff.front().g/binv;
  	  if (qmax && res>qmax)
  	    return -1;
  	  if (!allowrational && !is_zero(maincoeff.front().g%binv) )
  	    return 0;
  	}
  	quo.push_back(T_unsigned<T,U>(res,maincoeff.front().u+ushift));
  	q.push_back(quo.back());
        }
        else {
  	int recdivres=hashdivrem(maincoeff,lcoeffb,quo,tmp,std::vector<U>(vars.begin()+1,vars.end()),reduce,qmax,allowrational);
  	if (recdivres<1)
  	  return recdivres;
  	if (!tmp.empty())
  	  return 0;
  	for (it1=quo.begin(),it1end=quo.end();it1!=it1end;++it1){
  	  it1->u += ushift;
  	  q.push_back(*it1);
  	}
        }
        // remainder -= quo*b
        for (cit=itbbeg;cit!=itbend;++cit){
  	T g1=-cit->g;
  	U u,u1=cit->u;
  	// int deg1=u1/mainvar;
  	if (!is_zero(reduce)){
  	  for (it2=quo.begin(),it2end=quo.end();it2!=it2end;++it2){
  	    u=u1+it2->u;
  	    register int deg = int(u >> mainvar); // deg=deg1+it2->u/mainvar;
  	    if (deg<rdeg){
  	      hashptr = &produit[deg];
  	      prod_it=hashptr->find(u);
  	      if (prod_it==hashptr->end())
  		//(*hashptr)[u]=(g1*it2->g)%reduce; 
  		type_operator_reduce(g1,it2->g,(*hashptr)[u],reduce); 
  	      else {
  		// prod_it->second += g1*it2->g;
  		// prod_it->second %= reduce;
  		type_operator_plus_times_reduce(g1,it2->g,prod_it->second,reduce); 
  		if (is_zero(prod_it->second)) hashptr->erase(prod_it);
  	      }
  	    }
  	  }
  	}
  	else {
  	  for (it2=quo.begin(),it2end=quo.end();it2!=it2end;++it2){
  	    u=u1+it2->u;
  	    register int deg=int(u >> mainvar);
  	    if (deg<rdeg){
  	      hashptr = &produit[deg];
  	      prod_it=hashptr->find(u);
  	      if (prod_it==hashptr->end()){
  		type_operator_times(g1,it2->g,(*hashptr)[u]); 
  	      }
  	      else {
  		type_operator_plus_times(g1,it2->g,prod_it->second);	      
  		if (is_zero(prod_it->second)) hashptr->erase(prod_it);
  	      }
  	    }
  	  }
  	}
        }
        // end rem -= quo*b
      }
      // copy remainder to r and sort
      unsigned rsize=0;
      for (int i=0;i<bdeg;++i)
        rsize += unsigned(produit[i].size());
      r.reserve(rsize);
      T_unsigned<T,U> gu;
      for (int i=bdeg-1;i>=0;--i){
        for (prod_it=produit[i].begin(),prod_itend=produit[i].end();prod_it!=prod_itend;++prod_it){
  	if (!is_zero(gu.g=prod_it->second)){
  	  gu.u=prod_it->first;
  	  r.push_back(gu);
  	}
        }    
      }
      // IMPROVE: might do partial sort 
      sort(r.begin(),r.end());
      return 1;
    }
  
  
  
    template<class T,class U>
    bool is_content_trivially_1(const typename std::vector< T_unsigned<T,U> > & v,U mainvar){
  #ifdef HASH_MAP_NAMESPACE
      typedef HASH_MAP_NAMESPACE::hash_map< U,T,hash_function_unsigned_object > hash_prod ;
      hash_prod produit; // try to avoid reallocation
      // cout << "hash " << CLOCK() << std::endl;
  #else
  #ifdef USTL
      typedef ustl::map<U,T> hash_prod;
  #else
      typedef std::map<U,T> hash_prod;
  #endif
      // cout << "small map" << std::endl;
      hash_prod produit; 
  #endif
      U outer_index,inner_index;
      typename std::vector< T_unsigned<T,U> >::const_iterator it=v.begin(),itend=v.end();
      for (;it!=itend;++it){
        outer_index=it->u % mainvar;
        inner_index=it->u/mainvar;
        typename hash_prod::iterator jt=produit.find(outer_index),jtend=produit.end();
        if (jt==jtend){
  	if (inner_index==0)
  	  return true;
  	produit[outer_index]=it->g;
        }
        else
  	jt->second += it->g;
      }
      return false;
    }
  
    template<class T,class U>
    T peval_x1_xn(
  		typename std::vector< T_unsigned<T,U> >::const_iterator it,
  		typename std::vector< T_unsigned<T,U> >::const_iterator itend,
  		const typename std::vector<T> & v,
  		const typename std::vector<U> & vars,
  		const T & reduce){
      if (vars.empty())
        return it->g;
      int dim=int(vars.size())-1,nterms;
      if (dim!=int(v.size())){
  #ifndef NO_STDEXCEPT
        throw(std::runtime_error("Invalid dimension"));
  #endif
        return T(0);
      }
      U mainvar=vars.front(),var2=vars.back(),uend,u,prevu;
      T x=v.back();
      typename std::vector< T_unsigned<T,U> >::const_iterator itstop;
      typename std::vector<U>::const_iterator jtbeg=vars.begin(),jtend=vars.end(),jt;
      ++jtbeg;
      --jtend;
      typename std::vector<T>::const_iterator ktbeg=v.begin(),kt;
      T ans=0,total=0;
      for (;it!=itend;){
        // group monomials with the same x1..xn-1
        prevu = it->u % mainvar;
        if (dim==1)
  	uend=0;
        else {
  	const U & varxn=vars[dim-1];
  	uend =(prevu/varxn)*varxn;
        }
        nterms = (prevu-uend)/var2;
        ans = it->g;
        if (nterms>2 && itend-it>nterms && (itstop=it+nterms)->u % mainvar==uend){
  	// dense case
  	for (;it!=itstop;){
  	  ++it;
  	  ans = (ans*x+it->g)%reduce;	  
  	}
  	++it;
        }
        else {
  	for (++it;it!=itend;++it){
  	  u = it->u %mainvar;
  	  if (u<uend)
  	    break;
  	  if (prevu-u==var2)
  	    ans = (ans*x+it->g)%reduce;
  	  else
  	    ans = (ans*powmod(x,(prevu-u)/var2,reduce)+it->g)%reduce;
  	  prevu=u;
  	}
  	ans = (ans*powmod(x,(prevu-uend)/var2,reduce))%reduce;
        }
        for (jt=jtbeg,kt=ktbeg;jt!=jtend;++jt,++kt){
  	// px=px*powmod(*kt,u / *jt,modulo);
  	ans = (ans*powmod(*kt,uend / *jt,reduce))%reduce;
  	uend = uend % *jt;
        }
        total = (total+ans) % reduce;
      }
      return total;
    }
  
    // eval p at x2...xn
    template<class T,class U>
    void peval_x2_xn(const typename std::vector< T_unsigned<T,U> > & p,
  		   const typename std::vector<T> & v,
  		   const std::vector<U> & vars,
  		   std::vector< T_unsigned<T,U> > & res,
  		   const T & reduce){
      U mainvar=vars.front(),deg1;
      res.clear();
      typename std::vector< T_unsigned<T,U> >::const_iterator it=p.begin(),itend=p.end(),it2;
      for (;it!=itend;){
        deg1=(it->u/mainvar)*mainvar;
        for (it2=it;it2!=itend;++it2){
  	if (it2->u<deg1)
  	  break;
        }
        T tmp=peval_x1_xn<T,U>(it,it2,v,vars,reduce);
        it=it2;
        if (!is_zero(tmp))
  	res.push_back(T_unsigned<T,U>(tmp,deg1));
      }
    }
  
  #ifndef NO_NAMESPACE_GIAC
  } // namespace giac
  #endif // NO_NAMESPACE_GIAC
  
  #endif // _GIAC_THREADED_H