summaryrefslogtreecommitdiffstats
path: root/libcxx/include/random
blob: 811bc26e179420b5ee8f4e4ebe71df439afca11d (plain)
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
// -*- C++ -*-
//===--------------------------- random -----------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_RANDOM
#define _LIBCPP_RANDOM

/*
    random synopsis

#include <initializer_list>

namespace std
{

// Engines

template <class UIntType, UIntType a, UIntType c, UIntType m>
class linear_congruential_engine
{
public:
    // types
    typedef UIntType result_type;

    // engine characteristics
    static constexpr result_type multiplier = a;
    static constexpr result_type increment = c;
    static constexpr result_type modulus = m;
    static constexpr result_type min() { return c == 0u ? 1u: 0u;}
    static constexpr result_type max() { return m - 1u;}
    static constexpr result_type default_seed = 1u;

    // constructors and seeding functions
    explicit linear_congruential_engine(result_type s = default_seed);
    template<class Sseq> explicit linear_congruential_engine(Sseq& q);
    void seed(result_type s = default_seed);
    template<class Sseq> void seed(Sseq& q);

    // generating functions
    result_type operator()();
    void discard(unsigned long long z);
};

template <class UIntType, UIntType a, UIntType c, UIntType m>
bool
operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
           const linear_congruential_engine<UIntType, a, c, m>& y);

template <class UIntType, UIntType a, UIntType c, UIntType m>
bool
operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
           const linear_congruential_engine<UIntType, a, c, m>& y);

template <class charT, class traits,
          class UIntType, UIntType a, UIntType c, UIntType m>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
           const linear_congruential_engine<UIntType, a, c, m>& x);

template <class charT, class traits,
          class UIntType, UIntType a, UIntType c, UIntType m>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           linear_congruential_engine<UIntType, a, c, m>& x);

template <class UIntType, size_t w, size_t n, size_t m, size_t r,
          UIntType a, size_t u, UIntType d, size_t s,
          UIntType b, size_t t, UIntType c, size_t l, UIntType f>
class mersenne_twister_engine
{
public:
    // types
    typedef UIntType result_type;

    // engine characteristics
    static constexpr size_t word_size = w;
    static constexpr size_t state_size = n;
    static constexpr size_t shift_size = m;
    static constexpr size_t mask_bits = r;
    static constexpr result_type xor_mask = a;
    static constexpr size_t tempering_u = u;
    static constexpr result_type tempering_d = d;
    static constexpr size_t tempering_s = s;
    static constexpr result_type tempering_b = b;
    static constexpr size_t tempering_t = t;
    static constexpr result_type tempering_c = c;
    static constexpr size_t tempering_l = l;
    static constexpr result_type initialization_multiplier = f;
    static constexpr result_type min () { return 0; }
    static constexpr result_type max() { return 2^w - 1; }
    static constexpr result_type default_seed = 5489u;

    // constructors and seeding functions
    explicit mersenne_twister_engine(result_type value = default_seed);
    template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
    void seed(result_type value = default_seed);
    template<class Sseq> void seed(Sseq& q);

    // generating functions
    result_type operator()();
    void discard(unsigned long long z);
};

template <class UIntType, size_t w, size_t n, size_t m, size_t r,
          UIntType a, size_t u, UIntType d, size_t s,
          UIntType b, size_t t, UIntType c, size_t l, UIntType f>
bool
operator==(
    const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
    const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);

template <class UIntType, size_t w, size_t n, size_t m, size_t r,
          UIntType a, size_t u, UIntType d, size_t s,
          UIntType b, size_t t, UIntType c, size_t l, UIntType f>
bool
operator!=(
    const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
    const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);

template <class charT, class traits,
          class UIntType, size_t w, size_t n, size_t m, size_t r,
          UIntType a, size_t u, UIntType d, size_t s,
          UIntType b, size_t t, UIntType c, size_t l, UIntType f>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
           const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);

template <class charT, class traits,
          class UIntType, size_t w, size_t n, size_t m, size_t r,
          UIntType a, size_t u, UIntType d, size_t s,
          UIntType b, size_t t, UIntType c, size_t l, UIntType f>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);

template<class UIntType, size_t w, size_t s, size_t r>
class subtract_with_carry_engine
{
public:
    // types
    typedef UIntType result_type;

    // engine characteristics
    static constexpr size_t word_size = w;
    static constexpr size_t short_lag = s;
    static constexpr size_t long_lag = r;
    static constexpr result_type min() { return 0; }
    static constexpr result_type max() { return m-1; }
    static constexpr result_type default_seed = 19780503u;

    // constructors and seeding functions
    explicit subtract_with_carry_engine(result_type value = default_seed);
    template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
    void seed(result_type value = default_seed);
    template<class Sseq> void seed(Sseq& q);

    // generating functions
    result_type operator()();
    void discard(unsigned long long z);
};

template<class UIntType, size_t w, size_t s, size_t r>
bool
operator==(
    const subtract_with_carry_engine<UIntType, w, s, r>& x,
    const subtract_with_carry_engine<UIntType, w, s, r>& y);

template<class UIntType, size_t w, size_t s, size_t r>
bool
operator!=(
    const subtract_with_carry_engine<UIntType, w, s, r>& x,
    const subtract_with_carry_engine<UIntType, w, s, r>& y);

template <class charT, class traits,
          class UIntType, size_t w, size_t s, size_t r>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
           const subtract_with_carry_engine<UIntType, w, s, r>& x);

template <class charT, class traits,
          class UIntType, size_t w, size_t s, size_t r>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           subtract_with_carry_engine<UIntType, w, s, r>& x);

template<class Engine, size_t p, size_t r>
class discard_block_engine
{
public:
    // types
    typedef typename Engine::result_type result_type;

    // engine characteristics
    static constexpr size_t block_size = p;
    static constexpr size_t used_block = r;
    static constexpr result_type min() { return Engine::min(); }
    static constexpr result_type max() { return Engine::max(); }

    // constructors and seeding functions
    discard_block_engine();
    explicit discard_block_engine(const Engine& e);
    explicit discard_block_engine(Engine&& e);
    explicit discard_block_engine(result_type s);
    template<class Sseq> explicit discard_block_engine(Sseq& q);
    void seed();
    void seed(result_type s);
    template<class Sseq> void seed(Sseq& q);

    // generating functions
    result_type operator()();
    void discard(unsigned long long z);

    // property functions
    const Engine& base() const;
};

template<class Engine, size_t p, size_t r>
bool
operator==(
    const discard_block_engine<Engine, p, r>& x,
    const discard_block_engine<Engine, p, r>& y);

template<class Engine, size_t p, size_t r>
bool
operator!=(
    const discard_block_engine<Engine, p, r>& x,
    const discard_block_engine<Engine, p, r>& y);

template <class charT, class traits,
          class Engine, size_t p, size_t r>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
           const discard_block_engine<Engine, p, r>& x);

template <class charT, class traits,
          class Engine, size_t p, size_t r>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           discard_block_engine<Engine, p, r>& x);

template<class Engine, size_t w, class UIntType>
class independent_bits_engine
{
public:
    // types
    typedef UIntType result_type;

    // engine characteristics
    static constexpr result_type min() { return 0; }
    static constexpr result_type max() { return 2^w - 1; }

    // constructors and seeding functions
    independent_bits_engine();
    explicit independent_bits_engine(const Engine& e);
    explicit independent_bits_engine(Engine&& e);
    explicit independent_bits_engine(result_type s);
    template<class Sseq> explicit independent_bits_engine(Sseq& q);
    void seed();
    void seed(result_type s);
    template<class Sseq> void seed(Sseq& q);

    // generating functions
    result_type operator()(); void discard(unsigned long long z);

    // property functions
    const Engine& base() const;
};

template<class Engine, size_t w, class UIntType>
bool
operator==(
    const independent_bits_engine<Engine, w, UIntType>& x,
    const independent_bits_engine<Engine, w, UIntType>& y);

template<class Engine, size_t w, class UIntType>
bool
operator!=(
    const independent_bits_engine<Engine, w, UIntType>& x,
    const independent_bits_engine<Engine, w, UIntType>& y);

template <class charT, class traits,
          class Engine, size_t w, class UIntType>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
           const independent_bits_engine<Engine, w, UIntType>& x);

template <class charT, class traits,
          class Engine, size_t w, class UIntType>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           independent_bits_engine<Engine, w, UIntType>& x);

template<class Engine, size_t k>
class shuffle_order_engine
{
public:
    // types
    typedef typename Engine::result_type result_type;

    // engine characteristics
    static constexpr size_t table_size = k;
    static constexpr result_type min() { return Engine::min; }
    static constexpr result_type max() { return Engine::max; }

    // constructors and seeding functions
    shuffle_order_engine();
    explicit shuffle_order_engine(const Engine& e);
    explicit shuffle_order_engine(Engine&& e);
    explicit shuffle_order_engine(result_type s);
    template<class Sseq> explicit shuffle_order_engine(Sseq& q);
    void seed();
    void seed(result_type s);
    template<class Sseq> void seed(Sseq& q);

    // generating functions
    result_type operator()();
    void discard(unsigned long long z);

    // property functions
    const Engine& base() const;
};

template<class Engine, size_t k>
bool
operator==(
    const shuffle_order_engine<Engine, k>& x,
    const shuffle_order_engine<Engine, k>& y);

template<class Engine, size_t k>
bool
operator!=(
    const shuffle_order_engine<Engine, k>& x,
    const shuffle_order_engine<Engine, k>& y);

template <class charT, class traits,
          class Engine, size_t k>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
           const shuffle_order_engine<Engine, k>& x);

template <class charT, class traits,
          class Engine, size_t k>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
           shuffle_order_engine<Engine, k>& x);

typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
                                                                   minstd_rand0;
typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
                                                                    minstd_rand;
typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
                                0x9908b0df,
                                11, 0xffffffff,
                                7,  0x9d2c5680,
                                15, 0xefc60000,
                                18, 1812433253>                         mt19937;
typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
                                0xb5026f5aa96619e9,
                                29, 0x5555555555555555,
                                17, 0x71d67fffeda60000,
                                37, 0xfff7eee000000000,
                                43, 6364136223846793005>             mt19937_64;
typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>     ranlux24_base;
typedef subtract_with_carry_engine<uint_fast64_t, 48,  5, 12>     ranlux48_base;
typedef discard_block_engine<ranlux24_base, 223, 23>                   ranlux24;
typedef discard_block_engine<ranlux48_base, 389, 11>                   ranlux48;
typedef shuffle_order_engine<minstd_rand0, 256>                         knuth_b;
typedef minstd_rand0                                      default_random_engine;

// Generators

class random_device
{
public:
    // types
    typedef unsigned int result_type;

    // generator characteristics
    static constexpr result_type min() { return numeric_limits<result_type>::min(); }
    static constexpr result_type max() { return numeric_limits<result_type>::max(); }

    // constructors
    explicit random_device(const string& token = "/dev/urandom");

    // generating functions
    result_type operator()();

    // property functions
    double entropy() const;

    // no copy functions
    random_device(const random_device& ) = delete;
    void operator=(const random_device& ) = delete;
};

// Utilities

class seed_seq
{
public:
    // types
    typedef uint_least32_t result_type;

    // constructors
    seed_seq();
    template<class T>
        seed_seq(initializer_list<T> il);
    template<class InputIterator>
        seed_seq(InputIterator begin, InputIterator end);

    // generating functions
    template<class RandomAccessIterator>
        void generate(RandomAccessIterator begin, RandomAccessIterator end);

    // property functions
    size_t size() const;
    template<class OutputIterator>
        void param(OutputIterator dest) const;

    // no copy functions
    seed_seq(const seed_seq&) = delete;
    void operator=(const seed_seq& ) = delete;
};

template<class RealType, size_t bits, class URNG>
    RealType generate_canonical(URNG& g);

// Distributions

template<class IntType = int>
class uniform_int_distribution
{
public:
    // types
    typedef IntType result_type;

    class param_type
    {
    public:
        typedef uniform_int_distribution distribution_type;

        explicit param_type(IntType a = 0,
                                    IntType b = numeric_limits<IntType>::max());

        result_type a() const;
        result_type b() const;

        friend bool operator==(const param_type& x, const param_type& y);
        friend bool operator!=(const param_type& x, const param_type& y);
    };

    // constructors and reset functions
    explicit uniform_int_distribution(IntType a = 0,
                                    IntType b = numeric_limits<IntType>::max());
    explicit uniform_int_distribution(const param_type& parm);
    void reset();

    // generating functions
    template<class URNG> result_type operator()(URNG& g);
    template<class URNG> result_type operator()(URNG& g, const param_type& parm);

    // property functions
    result_type a() const;
    result_type b() const;

    param_type param() const;
    void param(const param_type& parm);

    result_type min() const;
    result_type max() const;

    friend bool operator==(const uniform_int_distribution& x,
                           const uniform_int_distribution& y);
    friend bool operator!=(const uniform_int_distribution& x,
                           const uniform_int_distribution& y);

    template <class charT, class traits>
    friend
    basic_ostream<charT, traits>&
    operator<<(basic_ostream<charT, traits>& os,
               const uniform_int_distribution& x);
    
    template <class charT, class traits>
    friend
    basic_istream<charT, traits>&
    operator>>(basic_istream<charT, traits>& is,
               uniform_int_distribution& x);
};

template<class RealType = double>
class uniform_real_distribution
{
public:
    // types
    typedef RealType result_type;

    class param_type
    {
    public:
        typedef uniform_real_distribution distribution_type;

        explicit param_type(RealType a = 0,
                            RealType b = 1);

        result_type a() const;
        result_type b() const;

        friend bool operator==(const param_type& x, const param_type& y);
        friend bool operator!=(const param_type& x, const param_type& y);
    };

    // constructors and reset functions
    explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
    explicit uniform_real_distribution(const param_type& parm);
    void reset();

    // generating functions
    template<class URNG> result_type operator()(URNG& g);
    template<class URNG> result_type operator()(URNG& g, const param_type& parm);

    // property functions
    result_type a() const;
    result_type b() const;

    param_type param() const;
    void param(const param_type& parm);

    result_type min() const;
    result_type max() const;

    friend bool operator==(const uniform_real_distribution& x,
                           const uniform_real_distribution& y);
    friend bool operator!=(const uniform_real_distribution& x,
                           const uniform_real_distribution& y);

    template <class charT, class traits>
    friend
    basic_ostream<charT, traits>&
    operator<<(basic_ostream<charT, traits>& os,
               const uniform_real_distribution& x);
    
    template <class charT, class traits>
    friend
    basic_istream<charT, traits>&
    operator>>(basic_istream<charT, traits>& is,
               uniform_real_distribution& x);
};

class bernoulli_distribution
{
public:
    // types
    typedef bool result_type;

    class param_type
    {
    public:
        typedef bernoulli_distribution distribution_type;

        explicit param_type(double p = 0.5);

        double p() const;

        friend bool operator==(const param_type& x, const param_type& y);
        friend bool operator!=(const param_type& x, const param_type& y);
    };

    // constructors and reset functions
    explicit bernoulli_distribution(double p = 0.5);
    explicit bernoulli_distribution(const param_type& parm);
    void reset();

    // generating functions
    template<class URNG> result_type operator()(URNG& g);
    template<class URNG> result_type operator()(URNG& g, const param_type& parm);

    // property functions
    double p() const;

    param_type param() const;
    void param(const param_type& parm);

    result_type min() const;
    result_type max() const;

    friend bool operator==(const bernoulli_distribution& x,
                           const bernoulli_distribution& y);
    friend bool operator!=(const bernoulli_distribution& x,
                           const bernoulli_distribution& y);

    template <class charT, class traits>
    friend
    basic_ostream<charT, traits>&
    operator<<(basic_ostream<charT, traits>& os,
               const bernoulli_distribution& x);
    
    template <class charT, class traits>
    friend
    basic_istream<charT, traits>&
    operator>>(basic_istream<charT, traits>& is,
               bernoulli_distribution& x);
};

template<class IntType = int>
    class binomial_distribution;

template<class IntType = int>
    class geometric_distribution;

template<class IntType = int>
    class negative_binomial_distribution;

template<class IntType = int>
    class poisson_distribution;

template<class RealType = double>
    class exponential_distribution;

template<class RealType = double>
    class gamma_distribution;

template<class RealType = double>
    class weibull_distribution;

template<class RealType = double>
    class extreme_value_distribution;

template<class RealType = double>
    class normal_distribution;

template<class RealType = double>
    class lognormal_distribution;

template<class RealType = double>
    class chi_squared_distribution;

template<class RealType = double>
    class cauchy_distribution;

template<class RealType = double>
    class fisher_f_distribution;

template<class RealType = double>
    class student_t_distribution;

template<class IntType = int>
    class discrete_distribution;

template<class RealType = double>
    class piecewise_constant_distribution;

template<class RealType = double>
    class piecewise_linear_distribution;

} // std
*/

#include <__config>
#include <cstddef>
#include <type_traits>
#include <initializer_list>
#include <cstdint>
#include <limits>
#include <algorithm>
#include <vector>
#include <string>
#include <istream>
#include <ostream>

#pragma GCC system_header

_LIBCPP_BEGIN_NAMESPACE_STD

// linear_congruential_engine

template <unsigned long long __a, unsigned long long __c,
          unsigned long long __m, unsigned long long _M,
          bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
struct __lce_ta;

// 64

template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
{
    typedef unsigned long long result_type;
    static result_type next(result_type __x)
    {
        // Schrage's algorithm
        const result_type __q = __m / __a;
        const result_type __r = __m % __a;
        const result_type __t0 = __a * (__x % __q);
        const result_type __t1 = __r * (__x / __q);
        __x = __t0 + (__t0 < __t1) * __m - __t1;
        __x += __c - (__x >= __m - __c) * __m;
        return __x;
    }
};

template <unsigned long long __a, unsigned long long __m>
struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
{
    typedef unsigned long long result_type;
    static result_type next(result_type __x)
    {
        // Schrage's algorithm
        const result_type __q = __m / __a;
        const result_type __r = __m % __a;
        const result_type __t0 = __a * (__x % __q);
        const result_type __t1 = __r * (__x / __q);
        __x = __t0 + (__t0 < __t1) * __m - __t1;
        return __x;
    }
};

template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
{
    typedef unsigned long long result_type;
    static result_type next(result_type __x)
    {
        return (__a * __x + __c) % __m;
    }
};

template <unsigned long long __a, unsigned long long __c>
struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
{
    typedef unsigned long long result_type;
    static result_type next(result_type __x)
    {
        return __a * __x + __c;
    }
};

// 32

template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
struct __lce_ta<_A, _C, _M, unsigned(~0), true>
{
    typedef unsigned result_type;
    static result_type next(result_type __x)
    {
        const result_type __a = static_cast<result_type>(_A);
        const result_type __c = static_cast<result_type>(_C);
        const result_type __m = static_cast<result_type>(_M);
        // Schrage's algorithm
        const result_type __q = __m / __a;
        const result_type __r = __m % __a;
        const result_type __t0 = __a * (__x % __q);
        const result_type __t1 = __r * (__x / __q);
        __x = __t0 + (__t0 < __t1) * __m - __t1;
        __x += __c - (__x >= __m - __c) * __m;
        return __x;
    }
};

template <unsigned long long _A, unsigned long long _M>
struct __lce_ta<_A, 0, _M, unsigned(~0), true>
{
    typedef unsigned result_type;
    static result_type next(result_type __x)
    {
        const result_type __a = static_cast<result_type>(_A);
        const result_type __m = static_cast<result_type>(_M);
        // Schrage's algorithm
        const result_type __q = __m / __a;
        const result_type __r = __m % __a;
        const result_type __t0 = __a * (__x % __q);
        const result_type __t1 = __r * (__x / __q);
        __x = __t0 + (__t0 < __t1) * __m - __t1;
        return __x;
    }
};

template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
struct __lce_ta<_A, _C, _M, unsigned(~0), false>
{
    typedef unsigned result_type;
    static result_type next(result_type __x)
    {
        const result_type __a = static_cast<result_type>(_A);
        const result_type __c = static_cast<result_type>(_C);
        const result_type __m = static_cast<result_type>(_M);
        return (__a * __x + __c) % __m;
    }
};

template <unsigned long long _A, unsigned long long _C>
struct __lce_ta<_A, _C, 0, unsigned(~0), false>
{
    typedef unsigned result_type;
    static result_type next(result_type __x)
    {
        const result_type __a = static_cast<result_type>(_A);
        const result_type __c = static_cast<result_type>(_C);
        return __a * __x + __c;
    }
};

// 16

template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
{
    typedef unsigned short result_type;
    static result_type next(result_type __x)
    {
        return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
    }
};

template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
class linear_congruential_engine;

template <class _CharT, class _Traits,
          class _U, _U _A, _U _C, _U _N>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const linear_congruential_engine<_U, _A, _C, _N>&);

template <class _CharT, class _Traits,
          class _U, _U _A, _U _C, _U _N>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           linear_congruential_engine<_U, _A, _C, _N>& __x);

template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
class linear_congruential_engine
{
public:
    // types
    typedef _UIntType result_type;

private:
    result_type __x_;

    static const result_type _M = result_type(~0);

    static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
    static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
public:
    static const result_type _Min = __c == 0u ? 1u: 0u;
    static const result_type _Max = __m - 1u;
    static_assert(_Min < _Max,           "linear_congruential_engine invalid parameters");

    // engine characteristics
    static const/*expr*/ result_type multiplier = __a;
    static const/*expr*/ result_type increment = __c;
    static const/*expr*/ result_type modulus = __m;
    static const/*expr*/ result_type min() {return _Min;}
    static const/*expr*/ result_type max() {return _Max;}
    static const/*expr*/ result_type default_seed = 1u;

    // constructors and seeding functions
    explicit linear_congruential_engine(result_type __s = default_seed)
        {seed(__s);}
    template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
        {seed(__q);}
    void seed(result_type __s = default_seed)
        {seed(integral_constant<bool, __m == 0>(),
              integral_constant<bool, __c == 0>(), __s);}
    template<class _Sseq>
        typename enable_if
        <
            !is_convertible<_Sseq, result_type>::value,
            void
        >::type
        seed(_Sseq& __q)
            {__seed(__q, integral_constant<unsigned,
                1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
                             :  (__m-1) / 0x100000000ull)>());}

    // generating functions
    result_type operator()()
        {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}

    friend bool operator==(const linear_congruential_engine& __x,
                           const linear_congruential_engine& __y)
        {return __x.__x_ == __y.__x_;}
    friend bool operator!=(const linear_congruential_engine& __x,
                           const linear_congruential_engine& __y)
        {return !(__x == __y);}

private:

    void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
    void seed(true_type, false_type, result_type __s) {__x_ = __s;}
    void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
                                                                 1 : __s % __m;}
    void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}

    template<class _Sseq>
        void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
    template<class _Sseq>
        void __seed(_Sseq& __q, integral_constant<unsigned, 2>);

    template <class _CharT, class _Traits,
              class _U, _U _A, _U _C, _U _N>
    friend
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os,
               const linear_congruential_engine<_U, _A, _C, _N>&);
    
    template <class _CharT, class _Traits,
              class _U, _U _A, _U _C, _U _N>
    friend
    basic_istream<_CharT, _Traits>&
    operator>>(basic_istream<_CharT, _Traits>& __is,
               linear_congruential_engine<_U, _A, _C, _N>& __x);
};

template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
template<class _Sseq>
void
linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
                                                 integral_constant<unsigned, 1>)
{
    const unsigned __k = 1;
    uint32_t __ar[__k+3];
    __q.generate(__ar, __ar + __k + 3);
    result_type __s = static_cast<result_type>(__ar[3] % __m);
    __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
}

template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
template<class _Sseq>
void
linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
                                                 integral_constant<unsigned, 2>)
{
    const unsigned __k = 2;
    uint32_t __ar[__k+3];
    __q.generate(__ar, __ar + __k + 3);
    result_type __s = static_cast<result_type>((__ar[3] +
                                                (uint64_t)__ar[4] << 32) % __m);
    __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
}

template <class _CharT, class _Traits>
class __save_flags
{
    typedef basic_ios<_CharT, _Traits> __stream_type;
    typedef typename __stream_type::fmtflags fmtflags;

    __stream_type& __stream_;
    fmtflags       __fmtflags_;
    _CharT         __fill_;

    __save_flags(const __save_flags&);
    __save_flags& operator=(const __save_flags&);
public:
    explicit __save_flags(__stream_type& __stream)
        : __stream_(__stream),
          __fmtflags_(__stream.flags()),
          __fill_(__stream.fill())
        {}
    ~__save_flags()
    {
        __stream_.flags(__fmtflags_);
        __stream_.fill(__fill_);
    }
};

template <class _CharT, class _Traits,
          class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
inline
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
{
    __save_flags<_CharT, _Traits> _(__os);
    __os.flags(ios_base::dec | ios_base::left);
    __os.fill(__os.widen(' '));
    return __os << __x.__x_;
}

template <class _CharT, class _Traits,
          class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
{
    __save_flags<_CharT, _Traits> _(__is);
    __is.flags(ios_base::dec | ios_base::skipws);
    _UIntType __t;
    __is >> __t;
    if (!__is.fail())
        __x.__x_ = __t;
    return __is;
}

typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
                                                                   minstd_rand0;
typedef minstd_rand0                                      default_random_engine;
typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
                                                                    minstd_rand;
// mersenne_twister_engine

template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
class mersenne_twister_engine;

template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
          _UI _A, size_t _U, _UI _D, size_t _S,
          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
bool
operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __x,
           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __y);

template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
          _UI _A, size_t _U, _UI _D, size_t _S,
          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
bool
operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __x,
           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __y);

template <class _CharT, class _Traits,
          class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
          _UI _A, size_t _U, _UI _D, size_t _S,
          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __x);

template <class _CharT, class _Traits,
          class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
          _UI _A, size_t _U, _UI _D, size_t _S,
          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                   _B, _T, _C, _L, _F>& __x);

template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
class mersenne_twister_engine
{
public:
    // types
    typedef _UIntType result_type;

private:
    result_type __x_[__n];
    size_t      __i_;

    static_assert(  0 <  __m, "mersenne_twister_engine invalid parameters");
    static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
    static const result_type _Dt = numeric_limits<result_type>::digits;
    static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
    static_assert(  2 <= __w, "mersenne_twister_engine invalid parameters");
    static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
    static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
    static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
    static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
    static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
public:
    static const result_type _Min = 0;
    static const result_type _Max = __w == _Dt ? result_type(~0) :
                                   (result_type(1) << __w) - result_type(1);
    static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
    static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
    static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
    static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
    static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
    static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");

    // engine characteristics
    static const/*expr*/ size_t word_size = __w;
    static const/*expr*/ size_t state_size = __n;
    static const/*expr*/ size_t shift_size = __m;
    static const/*expr*/ size_t mask_bits = __r;
    static const/*expr*/ result_type xor_mask = __a;
    static const/*expr*/ size_t tempering_u = __u;
    static const/*expr*/ result_type tempering_d = __d;
    static const/*expr*/ size_t tempering_s = __s;
    static const/*expr*/ result_type tempering_b = __b;
    static const/*expr*/ size_t tempering_t = __t;
    static const/*expr*/ result_type tempering_c = __c;
    static const/*expr*/ size_t tempering_l = __l;
    static const/*expr*/ result_type initialization_multiplier = __f;
    static const/*expr*/ result_type min() { return _Min; }
    static const/*expr*/ result_type max() { return _Max; }
    static const/*expr*/ result_type default_seed = 5489u;

    // constructors and seeding functions
    explicit mersenne_twister_engine(result_type __sd = default_seed)
        {seed(__sd);}
    template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
        {seed(__q);}
    void seed(result_type __sd = default_seed);
    template<class _Sseq>
        typename enable_if
        <
            !is_convertible<_Sseq, result_type>::value,
            void
        >::type
        seed(_Sseq& __q)
            {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}

    // generating functions
    result_type operator()();
    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}

    template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
              _UI _A, size_t _U, _UI _D, size_t _S,
              _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
    friend
    bool
    operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                             _B, _T, _C, _L, _F>& __x,
               const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                             _B, _T, _C, _L, _F>& __y);
    
    template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
              _UI _A, size_t _U, _UI _D, size_t _S,
              _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
    friend
    bool
    operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                             _B, _T, _C, _L, _F>& __x,
               const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                             _B, _T, _C, _L, _F>& __y);

    template <class _CharT, class _Traits,
              class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
              _UI _A, size_t _U, _UI _D, size_t _S,
              _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
    friend
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os,
               const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                             _B, _T, _C, _L, _F>& __x);

    template <class _CharT, class _Traits,
              class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
              _UI _A, size_t _U, _UI _D, size_t _S,
              _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
    friend
    basic_istream<_CharT, _Traits>&
    operator>>(basic_istream<_CharT, _Traits>& __is,
               mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                       _B, _T, _C, _L, _F>& __x);
private:

    template<class _Sseq>
        void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
    template<class _Sseq>
        void __seed(_Sseq& __q, integral_constant<unsigned, 2>);

    template <size_t __count>
        static
        typename enable_if
        <
            __count < __w,
            result_type
        >::type
        __lshift(result_type __x) {return (__x << __count) & _Max;}

    template <size_t __count>
        static
        typename enable_if
        <
            (__count >= __w),
            result_type
        >::type
        __lshift(result_type __x) {return result_type(0);}

    template <size_t __count>
        static
        typename enable_if
        <
            __count < _Dt,
            result_type
        >::type
        __rshift(result_type __x) {return __x >> __count;}

    template <size_t __count>
        static
        typename enable_if
        <
            (__count >= _Dt),
            result_type
        >::type
        __rshift(result_type __x) {return result_type(0);}
};

template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
void
mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
    __t, __c, __l, __f>::seed(result_type __sd)
{   // __w >= 2
    __x_[0] = __sd & _Max;
    for (size_t __i = 1; __i < __n; ++__i)
        __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
    __i_ = 0;
}

template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
template<class _Sseq>
void
mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
    __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
{
    const unsigned __k = 1;
    uint32_t __ar[__n * __k];
    __q.generate(__ar, __ar + __n * __k);
    for (size_t __i = 0; __i < __n; ++__i)
        __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
    const result_type __mask = __r == _Dt ? result_type(~0) :
                                       (result_type(1) << __r) - result_type(1);
    __i_ = 0;
    if ((__x_[0] & ~__mask) == 0)
    {
        for (size_t __i = 1; __i < __n; ++__i)
            if (__x_[__i] != 0)
                return;
        __x_[0] = _Max;
    }
}

template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
template<class _Sseq>
void
mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
    __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
{
    const unsigned __k = 2;
    uint32_t __ar[__n * __k];
    __q.generate(__ar, __ar + __n * __k);
    for (size_t __i = 0; __i < __n; ++__i)
        __x_[__i] = static_cast<result_type>(
            (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
    const result_type __mask = __r == _Dt ? result_type(~0) :
                                       (result_type(1) << __r) - result_type(1);
    __i_ = 0;
    if ((__x_[0] & ~__mask) == 0)
    {
        for (size_t __i = 1; __i < __n; ++__i)
            if (__x_[__i] != 0)
                return;
        __x_[0] = _Max;
    }
}

template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
_UIntType
mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
    __t, __c, __l, __f>::operator()()
{
    const size_t __j = (__i_ + 1) % __n;
    const result_type __mask = __r == _Dt ? result_type(~0) :
                                       (result_type(1) << __r) - result_type(1);
    const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
    const size_t __k = (__i_ + __m) % __n;
    __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
    result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
    __i_ = __j;
    __z ^= __lshift<__s>(__z) & __b;
    __z ^= __lshift<__t>(__z) & __c;
    return __z ^ __rshift<__l>(__z);
}

template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
          _UI _A, size_t _U, _UI _D, size_t _S,
          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
bool
operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __x,
           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __y)
{
    if (__x.__i_ == __y.__i_)
        return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
    if (__x.__i_ == 0 || __y.__i_ == 0)
    {
        size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
        if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
                         __y.__x_ + __y.__i_))
            return false;
        if (__x.__i_ == 0)
            return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
        return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
    }
    if (__x.__i_ < __y.__i_)
    {
        size_t __j = _N - __y.__i_;
        if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
                         __y.__x_ + __y.__i_))
            return false;
        if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
                         __y.__x_))
            return false;
        return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
                           __y.__x_ + (_N - (__x.__i_ + __j)));
    }
    size_t __j = _N - __x.__i_;
    if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
                     __x.__x_ + __x.__i_))
        return false;
    if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
                     __x.__x_))
        return false;
    return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
                       __x.__x_ + (_N - (__y.__i_ + __j)));
}

template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
          _UI _A, size_t _U, _UI _D, size_t _S,
          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
inline
bool
operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __x,
           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __y)
{
    return !(__x == __y);
}

template <class _CharT, class _Traits,
          class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
          _UI _A, size_t _U, _UI _D, size_t _S,
          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                         _B, _T, _C, _L, _F>& __x)
{
    __save_flags<_CharT, _Traits> _(__os);
    __os.flags(ios_base::dec | ios_base::left);
    _CharT __sp = __os.widen(' ');
    __os.fill(__sp);
    __os << __x.__x_[__x.__i_];
    for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
        __os << __sp << __x.__x_[__j];
    for (size_t __j = 0; __j < __x.__i_; ++__j)
        __os << __sp << __x.__x_[__j];
    return __os;
}

template <class _CharT, class _Traits,
          class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
          _UI _A, size_t _U, _UI _D, size_t _S,
          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
                                   _B, _T, _C, _L, _F>& __x)
{
    __save_flags<_CharT, _Traits> _(__is);
    __is.flags(ios_base::dec | ios_base::skipws);
    _UI __t[_N];
    for (size_t __i = 0; __i < _N; ++__i)
        __is >> __t[__i];
    if (!__is.fail())
    {
        for (size_t __i = 0; __i < _N; ++__i)
            __x.__x_[__i] = __t[__i];
        __x.__i_ = 0;
    }
    return __is;
}

typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
                                0x9908b0df, 11, 0xffffffff,
                                7,  0x9d2c5680,
                                15, 0xefc60000,
                                18, 1812433253>                         mt19937;
typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
                                0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
                                17, 0x71d67fffeda60000ULL,
                                37, 0xfff7eee000000000ULL,
                                43, 6364136223846793005ULL>          mt19937_64;

// subtract_with_carry_engine

template<class _UIntType, size_t __w, size_t __s, size_t __r>
class subtract_with_carry_engine;

template<class _UI, size_t _W, size_t _S, size_t _R>
bool
operator==(
    const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
    const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);

template<class _UI, size_t _W, size_t _S, size_t _R>
bool
operator!=(
    const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
    const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);

template <class _CharT, class _Traits,
          class _UI, size_t _W, size_t _S, size_t _R>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);

template <class _CharT, class _Traits,
          class _UI, size_t _W, size_t _S, size_t _R>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           subtract_with_carry_engine<_UI, _W, _S, _R>& __x);

template<class _UIntType, size_t __w, size_t __s, size_t __r>
class subtract_with_carry_engine
{
public:
    // types
    typedef _UIntType result_type;

private:
    result_type __x_[__r];
    result_type  __c_;
    size_t      __i_;

    static const result_type _Dt = numeric_limits<result_type>::digits;
    static_assert(  0 <  __w, "subtract_with_carry_engine invalid parameters");
    static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
    static_assert(  0 <  __s, "subtract_with_carry_engine invalid parameters");
    static_assert(__s <  __r, "subtract_with_carry_engine invalid parameters");
public:
    static const result_type _Min = 0;
    static const result_type _Max = __w == _Dt ? result_type(~0) :
                                   (result_type(1) << __w) - result_type(1);
    static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");

    // engine characteristics
    static const/*expr*/ size_t word_size = __w;
    static const/*expr*/ size_t short_lag = __s;
    static const/*expr*/ size_t long_lag = __r;
    static const/*expr*/ result_type min() { return _Min; }
    static const/*expr*/ result_type max() { return _Max; }
    static const/*expr*/ result_type default_seed = 19780503u;

    // constructors and seeding functions
    explicit subtract_with_carry_engine(result_type __sd = default_seed)
        {seed(__sd);}
    template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
        {seed(__q);}
    void seed(result_type __sd = default_seed)
        {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
    template<class _Sseq>
        typename enable_if
        <
            !is_convertible<_Sseq, result_type>::value,
            void
        >::type
        seed(_Sseq& __q)
            {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}

    // generating functions
    result_type operator()();
    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}

    template<class _UI, size_t _W, size_t _S, size_t _R>
    friend
    bool
    operator==(
        const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
        const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
    
    template<class _UI, size_t _W, size_t _S, size_t _R>
    friend
    bool
    operator!=(
        const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
        const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
    
    template <class _CharT, class _Traits,
              class _UI, size_t _W, size_t _S, size_t _R>
    friend
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os,
               const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
    
    template <class _CharT, class _Traits,
              class _UI, size_t _W, size_t _S, size_t _R>
    friend
    basic_istream<_CharT, _Traits>&
    operator>>(basic_istream<_CharT, _Traits>& __is,
               subtract_with_carry_engine<_UI, _W, _S, _R>& __x);

private:

    void seed(result_type __sd, integral_constant<unsigned, 1>);
    void seed(result_type __sd, integral_constant<unsigned, 2>);
    template<class _Sseq>
        void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
    template<class _Sseq>
        void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
};

template<class _UIntType, size_t __w, size_t __s, size_t __r>
void
subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
        integral_constant<unsigned, 1>)
{
    linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
        __e(__sd == 0u ? default_seed : __sd);
    for (size_t __i = 0; __i < __r; ++__i)
        __x_[__i] = static_cast<result_type>(__e() & _Max);
    __c_ = __x_[__r-1] == 0;
    __i_ = 0;
}

template<class _UIntType, size_t __w, size_t __s, size_t __r>
void
subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
        integral_constant<unsigned, 2>)
{
    linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
        __e(__sd == 0u ? default_seed : __sd);
    for (size_t __i = 0; __i < __r; ++__i)
        __x_[__i] = static_cast<result_type>(
                                    (__e() + ((uint64_t)__e() << 32)) & _Max);
    __c_ = __x_[__r-1] == 0;
    __i_ = 0;
}

template<class _UIntType, size_t __w, size_t __s, size_t __r>
template<class _Sseq>
void
subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
        integral_constant<unsigned, 1>)
{
    const unsigned __k = 1;
    uint32_t __ar[__r * __k];
    __q.generate(__ar, __ar + __r * __k);
    for (size_t __i = 0; __i < __r; ++__i)
        __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
    __c_ = __x_[__r-1] == 0;
    __i_ = 0;
}

template<class _UIntType, size_t __w, size_t __s, size_t __r>
template<class _Sseq>
void
subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
        integral_constant<unsigned, 2>)
{
    const unsigned __k = 2;
    uint32_t __ar[__r * __k];
    __q.generate(__ar, __ar + __r * __k);
    for (size_t __i = 0; __i < __r; ++__i)
        __x_[__i] = static_cast<result_type>(
                  (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
    __c_ = __x_[__r-1] == 0;
    __i_ = 0;
}

template<class _UIntType, size_t __w, size_t __s, size_t __r>
_UIntType
subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
{
    const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
    result_type& __xr = __x_[__i_];
    result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
    __xr = (__xs - __xr - __c_) & _Max;
    __c_ = __new_c;
    __i_ = (__i_ + 1) % __r;
    return __xr;
}

template<class _UI, size_t _W, size_t _S, size_t _R>
bool
operator==(
    const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
    const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
{
    if (__x.__c_ != __y.__c_)
        return false;
    if (__x.__i_ == __y.__i_)
        return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
    if (__x.__i_ == 0 || __y.__i_ == 0)
    {
        size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
        if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
                         __y.__x_ + __y.__i_))
            return false;
        if (__x.__i_ == 0)
            return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
        return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
    }
    if (__x.__i_ < __y.__i_)
    {
        size_t __j = _R - __y.__i_;
        if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
                         __y.__x_ + __y.__i_))
            return false;
        if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
                         __y.__x_))
            return false;
        return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
                           __y.__x_ + (_R - (__x.__i_ + __j)));
    }
    size_t __j = _R - __x.__i_;
    if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
                     __x.__x_ + __x.__i_))
        return false;
    if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
                     __x.__x_))
        return false;
    return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
                       __x.__x_ + (_R - (__y.__i_ + __j)));
}

template<class _UI, size_t _W, size_t _S, size_t _R>
inline
bool
operator!=(
    const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
    const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
{
    return !(__x == __y);
}

template <class _CharT, class _Traits,
          class _UI, size_t _W, size_t _S, size_t _R>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
{
    __save_flags<_CharT, _Traits> _(__os);
    __os.flags(ios_base::dec | ios_base::left);
    _CharT __sp = __os.widen(' ');
    __os.fill(__sp);
    __os << __x.__x_[__x.__i_];
    for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
        __os << __sp << __x.__x_[__j];
    for (size_t __j = 0; __j < __x.__i_; ++__j)
        __os << __sp << __x.__x_[__j];
    __os << __sp << __x.__c_;
    return __os;
}

template <class _CharT, class _Traits,
          class _UI, size_t _W, size_t _S, size_t _R>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
{
    __save_flags<_CharT, _Traits> _(__is);
    __is.flags(ios_base::dec | ios_base::skipws);
    _UI __t[_R+1];
    for (size_t __i = 0; __i < _R+1; ++__i)
        __is >> __t[__i];
    if (!__is.fail())
    {
        for (size_t __i = 0; __i < _R; ++__i)
            __x.__x_[__i] = __t[__i];
        __x.__c_ = __t[_R];
        __x.__i_ = 0;
    }
    return __is;
}

typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>     ranlux24_base;
typedef subtract_with_carry_engine<uint_fast64_t, 48,  5, 12>     ranlux48_base;

// discard_block_engine

template<class _Engine, size_t __p, size_t __r>
class discard_block_engine
{
    _Engine __e_;
    int     __n_;

    static_assert(  0 <  __r, "discard_block_engine invalid parameters");
    static_assert(__r <= __p, "discard_block_engine invalid parameters");
public:
    // types
    typedef typename _Engine::result_type result_type;

    // engine characteristics
    static const/*expr*/ size_t block_size = __p;
    static const/*expr*/ size_t used_block = __r;

    // Temporary work around for lack of constexpr
    static const result_type _Min = _Engine::_Min;
    static const result_type _Max = _Engine::_Max;

    static const/*expr*/ result_type min() { return _Engine::min(); }
    static const/*expr*/ result_type max() { return _Engine::max(); }

    // constructors and seeding functions
    discard_block_engine() : __n_(0) {}
//     explicit discard_block_engine(const _Engine& __e);
//     explicit discard_block_engine(_Engine&& __e);
    explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
    template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
        : __e_(__q), __n_(0) {}
    void seed() {__e_.seed(); __n_ = 0;}
    void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
    template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}

    // generating functions
    result_type operator()();
    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}

    // property functions
    const _Engine& base() const {return __e_;}

    template<class _Eng, size_t _P, size_t _R>
    friend
    bool
    operator==(
        const discard_block_engine<_Eng, _P, _R>& __x,
        const discard_block_engine<_Eng, _P, _R>& __y);
    
    template<class _Eng, size_t _P, size_t _R>
    friend
    bool
    operator!=(
        const discard_block_engine<_Eng, _P, _R>& __x,
        const discard_block_engine<_Eng, _P, _R>& __y);
    
    template <class _CharT, class _Traits,
              class _Eng, size_t _P, size_t _R>
    friend
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os,
               const discard_block_engine<_Eng, _P, _R>& __x);
    
    template <class _CharT, class _Traits,
              class _Eng, size_t _P, size_t _R>
    friend
    basic_istream<_CharT, _Traits>&
    operator>>(basic_istream<_CharT, _Traits>& __is,
               discard_block_engine<_Eng, _P, _R>& __x);
};

template<class _Engine, size_t __p, size_t __r>
typename discard_block_engine<_Engine, __p, __r>::result_type
discard_block_engine<_Engine, __p, __r>::operator()()
{
    if (__n_ >= __r)
    {
        __e_.discard(__p - __r);
        __n_ = 0;
    }
    ++__n_;
    return __e_();
}

template<class _Eng, size_t _P, size_t _R>
inline
bool
operator==(const discard_block_engine<_Eng, _P, _R>& __x,
           const discard_block_engine<_Eng, _P, _R>& __y)
{
    return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
}

template<class _Eng, size_t _P, size_t _R>
inline
bool
operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
           const discard_block_engine<_Eng, _P, _R>& __y)
{
    return !(__x == __y);
}

template <class _CharT, class _Traits,
          class _Eng, size_t _P, size_t _R>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const discard_block_engine<_Eng, _P, _R>& __x)
{
    __save_flags<_CharT, _Traits> _(__os);
    __os.flags(ios_base::dec | ios_base::left);
    _CharT __sp = __os.widen(' ');
    __os.fill(__sp);
    return __os << __x.__e_ << __sp << __x.__n_;
}

template <class _CharT, class _Traits,
          class _Eng, size_t _P, size_t _R>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           discard_block_engine<_Eng, _P, _R>& __x)
{
    __save_flags<_CharT, _Traits> _(__is);
    __is.flags(ios_base::dec | ios_base::skipws);
    _Eng __e;
    int __n;
    __is >> __e >> __n;
    if (!__is.fail())
    {
        __x.__e_ = __e;
        __x.__n_ = __n;
    }
    return __is;
}

typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;

// independent_bits_engine

template <unsigned long long _X, size_t _R>
struct __log2_imp
{
    static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
                                           : __log2_imp<_X, _R - 1>::value;
};

template <unsigned long long _X>
struct __log2_imp<_X, 0>
{
    static const size_t value = 0;
};

template <size_t _R>
struct __log2_imp<0, _R>
{
    static const size_t value = _R + 1;
};

template <class _UI, _UI _X>
struct __log2
{
    static const size_t value = __log2_imp<_X,
                                         sizeof(_UI) * __CHAR_BIT__ - 1>::value;
};

template<class _Engine, size_t __w, class _UIntType>
class independent_bits_engine
{
    template <class _UI, _UI _R0, size_t _W, size_t _M>
    class __get_n
    {
        static const size_t _Dt = numeric_limits<_UI>::digits;
        static const size_t _N = _W / _M + (_W % _M != 0);
        static const size_t _W0 = _W / _N;
        static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
    public:
        static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
    };
public:
    // types
    typedef _UIntType result_type;

private:
    _Engine __e_;

    static const result_type _Dt = numeric_limits<result_type>::digits;
    static_assert(  0 <  __w, "independent_bits_engine invalid parameters");
    static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");

    typedef typename _Engine::result_type _Engine_result_type;
    typedef typename conditional
        <
            sizeof(_Engine_result_type) <= sizeof(result_type),
                result_type,
                _Engine_result_type
        >::type _Working_result_type;
    // Temporary work around for lack of constexpr
    static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
                                                         + _Working_result_type(1);
    static const size_t __m = __log2<_Working_result_type, _R>::value;
    static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
    static const size_t __w0 = __w / __n;
    static const size_t __n0 = __n - __w % __n;
    static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
    static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
    static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
                                                   (_R >> __w0) << __w0;
    static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
                                                   (_R >> (__w0+1)) << (__w0+1);
    static const _Engine_result_type __mask0 = __w0 > 0 ?
                                _Engine_result_type(~0) >> (_EDt - __w0) :
                                _Engine_result_type(0);
    static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
                                _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
                                _Engine_result_type(~0);
public:
    static const result_type _Min = 0;
    static const result_type _Max = __w == _Dt ? result_type(~0) :
                                   (result_type(1) << __w) - result_type(1);
    static_assert(_Min < _Max, "independent_bits_engine invalid parameters");

    // engine characteristics
    static const/*expr*/ result_type min() { return _Min; }
    static const/*expr*/ result_type max() { return _Max; }

    // constructors and seeding functions
    independent_bits_engine() {}
//    explicit independent_bits_engine(const _Engine& __e);
//    explicit independent_bits_engine(_Engine&& __e);
    explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
    template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
         : __e_(__q) {}
    void seed() {__e_.seed();}
    void seed(result_type __sd) {__e_.seed(__sd);}
    template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}

    // generating functions
    result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}

    // property functions
    const _Engine& base() const {return __e_;}

    template<class _Eng, size_t _W, class _UI>
    friend
    bool
    operator==(
        const independent_bits_engine<_Eng, _W, _UI>& __x,
        const independent_bits_engine<_Eng, _W, _UI>& __y);
    
    template<class _Eng, size_t _W, class _UI>
    friend
    bool
    operator!=(
        const independent_bits_engine<_Eng, _W, _UI>& __x,
        const independent_bits_engine<_Eng, _W, _UI>& __y);
    
    template <class _CharT, class _Traits,
              class _Eng, size_t _W, class _UI>
    friend
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os,
               const independent_bits_engine<_Eng, _W, _UI>& __x);
    
    template <class _CharT, class _Traits,
              class _Eng, size_t _W, class _UI>
    friend
    basic_istream<_CharT, _Traits>&
    operator>>(basic_istream<_CharT, _Traits>& __is,
               independent_bits_engine<_Eng, _W, _UI>& __x);

private:
    result_type __eval(false_type);
    result_type __eval(true_type);

    template <size_t __count>
        static
        typename enable_if
        <
            __count < _Dt,
            result_type
        >::type
        __lshift(result_type __x) {return __x << __count;}

    template <size_t __count>
        static
        typename enable_if
        <
            (__count >= _Dt),
            result_type
        >::type
        __lshift(result_type __x) {return result_type(0);}
};

template<class _Engine, size_t __w, class _UIntType>
inline
_UIntType
independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
{
    return static_cast<result_type>(__e_() & __mask0);
}

template<class _Engine, size_t __w, class _UIntType>
_UIntType
independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
{
    result_type _S = 0;
    for (size_t __k = 0; __k < __n0; ++__k)
    {
        _Engine_result_type __u;
        do
        {
            __u = __e_() - _Engine::min();
        } while (__u >= __y0);
        _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
    }
    for (size_t __k = __n0; __k < __n; ++__k)
    {
        _Engine_result_type __u;
        do
        {
            __u = __e_() - _Engine::min();
        } while (__u >= __y1);
        _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
    }
    return _S;
}

template<class _Eng, size_t _W, class _UI>
inline
bool
operator==(
    const independent_bits_engine<_Eng, _W, _UI>& __x,
    const independent_bits_engine<_Eng, _W, _UI>& __y)
{
    return __x.base() == __y.base();
}

template<class _Eng, size_t _W, class _UI>
inline
bool
operator!=(
    const independent_bits_engine<_Eng, _W, _UI>& __x,
    const independent_bits_engine<_Eng, _W, _UI>& __y)
{
    return !(__x == __y);
}

template <class _CharT, class _Traits,
          class _Eng, size_t _W, class _UI>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const independent_bits_engine<_Eng, _W, _UI>& __x)
{
    return __os << __x.base();
}

template <class _CharT, class _Traits,
          class _Eng, size_t _W, class _UI>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           independent_bits_engine<_Eng, _W, _UI>& __x)
{
    _Eng __e;
    __is >> __e;
    if (!__is.fail())
        __x.__e_ = __e;
    return __is;
}

// shuffle_order_engine

template <uint64_t _Xp, uint64_t _Yp>
struct __ugcd
{
    static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
};

template <uint64_t _Xp>
struct __ugcd<_Xp, 0>
{
    static const uint64_t value = _Xp;
};

template <uint64_t _N, uint64_t _D>
class __uratio
{
    static_assert(_D != 0, "__uratio divide by 0");
    static const uint64_t __gcd = __ugcd<_N, _D>::value;
public:
    static const uint64_t num = _N / __gcd;
    static const uint64_t den = _D / __gcd;

    typedef __uratio<num, den> type;
};

template<class _Engine, size_t __k>
class shuffle_order_engine
{
    static_assert(0 < __k, "shuffle_order_engine invalid parameters");
public:
    // types
    typedef typename _Engine::result_type result_type;

private:
    _Engine __e_;
    result_type _V_[__k];
    result_type _Y_;

public:
    // engine characteristics
    static const/*expr*/ size_t table_size = __k;
    
    static const result_type _Min = _Engine::_Min;
    static const result_type _Max = _Engine::_Max;
    static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
    static const/*expr*/ result_type min() { return _Min; }
    static const/*expr*/ result_type max() { return _Max; }

    static const unsigned long long _R = _Max - _Min + 1ull;

    // constructors and seeding functions
    shuffle_order_engine() {__init();}
//    explicit shuffle_order_engine(const _Engine& __e);
//    explicit shuffle_order_engine(_Engine&& e);
    explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
    template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
         : __e_(__q) {__init();}
    void seed() {__e_.seed(); __init();}
    void seed(result_type __sd) {__e_.seed(__sd); __init();}
    template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}

    // generating functions
    result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}

    // property functions
    const _Engine& base() const {return __e_;}

private:
    template<class _Eng, size_t _K>
    friend
    bool
    operator==(
        const shuffle_order_engine<_Eng, _K>& __x,
        const shuffle_order_engine<_Eng, _K>& __y);
    
    template<class _Eng, size_t _K>
    friend
    bool
    operator!=(
        const shuffle_order_engine<_Eng, _K>& __x,
        const shuffle_order_engine<_Eng, _K>& __y);
    
    template <class _CharT, class _Traits,
              class _Eng, size_t _K>
    friend
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os,
               const shuffle_order_engine<_Eng, _K>& __x);
    
    template <class _CharT, class _Traits,
              class _Eng, size_t _K>
    friend
    basic_istream<_CharT, _Traits>&
    operator>>(basic_istream<_CharT, _Traits>& __is,
               shuffle_order_engine<_Eng, _K>& __x);

    void __init()
    {
        for (size_t __i = 0; __i < __k; ++__i)
            _V_[__i] = __e_();
        _Y_ = __e_();
    }

    result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
    result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}

    result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
    result_type __eval2(true_type) {return __evalf<__k, 0>();}

    template <uint64_t _N, uint64_t _D>
        typename enable_if
        <
            (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
            result_type
        >::type
        __eval(__uratio<_N, _D>)
            {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}

    template <uint64_t _N, uint64_t _D>
        typename enable_if
        <
            __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
            result_type
        >::type
        __eval(__uratio<_N, _D>)
        {
            const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
                                                   / __uratio<_N, _D>::den);
            _Y_ = _V_[__j];
            _V_[__j] = __e_();
            return _Y_;
        }

    template <uint64_t __n, uint64_t __d>
        result_type __evalf()
        {
            const double _F = __d == 0 ?
                __n / (2. * 0x8000000000000000ull) :
                __n / (double)__d;
            const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
            _Y_ = _V_[__j];
            _V_[__j] = __e_();
            return _Y_;
        }
};

template<class _Eng, size_t _K>
bool
operator==(
    const shuffle_order_engine<_Eng, _K>& __x,
    const shuffle_order_engine<_Eng, _K>& __y)
{
    return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
           __x.__e_ == __y.__e_;
}

template<class _Eng, size_t _K>
inline
bool
operator!=(
    const shuffle_order_engine<_Eng, _K>& __x,
    const shuffle_order_engine<_Eng, _K>& __y)
{
    return !(__x == __y);
}

template <class _CharT, class _Traits,
          class _Eng, size_t _K>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const shuffle_order_engine<_Eng, _K>& __x)
{
    __save_flags<_CharT, _Traits> _(__os);
    __os.flags(ios_base::dec | ios_base::left);
    _CharT __sp = __os.widen(' ');
    __os.fill(__sp);
    __os << __x.__e_ << __sp << __x._V_[0];
    for (size_t __i = 1; __i < _K; ++__i)
        __os << __sp << __x._V_[__i];
    return __os << __sp << __x._Y_;
}

template <class _CharT, class _Traits,
          class _Eng, size_t _K>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           shuffle_order_engine<_Eng, _K>& __x)
{
    typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
    __save_flags<_CharT, _Traits> _(__is);
    __is.flags(ios_base::dec | ios_base::skipws);
    _Eng __e;
    result_type _V[_K+1];
    __is >> __e;
    for (size_t __i = 0; __i < _K+1; ++__i)
        __is >> _V[__i];
    if (!__is.fail())
    {
        __x.__e_ = __e;
        for (size_t __i = 0; __i < _K; ++__i)
            __x._V_[__i] = _V[__i];
        __x._Y_ = _V[_K];
    }
    return __is;
}

typedef shuffle_order_engine<minstd_rand0, 256>                         knuth_b;

// random_device

class random_device
{
    int __f_;
public:
    // types
    typedef unsigned result_type;

    // generator characteristics
    static const result_type _Min = 0;
    static const result_type _Max = 0xFFFFFFFFu;

    static const/*expr*/ result_type min() { return _Min;}
    static const/*expr*/ result_type max() { return _Max;}

    // constructors
    explicit random_device(const string& __token = "/dev/urandom");
    ~random_device();

    // generating functions
    result_type operator()();

    // property functions
    double entropy() const;

private:
    // no copy functions
    random_device(const random_device&); // = delete;
    random_device& operator=(const random_device&); // = delete;
};

// seed_seq

class seed_seq
{
public:
    // types
    typedef uint32_t result_type;

private:
    vector<result_type> __v_;

    template<class _InputIterator>
        void init(_InputIterator __first, _InputIterator __last);
public:
    // constructors
    seed_seq() {}
    template<class _Tp>
        seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
            
    template<class _InputIterator>
        seed_seq(_InputIterator __first, _InputIterator __last)
             {init(__first, __last);}

    // generating functions
    template<class _RandomAccessIterator>
        void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);

    // property functions
    size_t size() const {return __v_.size();}
    template<class _OutputIterator>
        void param(_OutputIterator __dest) const
            {_STD::copy(__v_.begin(), __v_.end(), __dest);}

private:
    // no copy functions
    seed_seq(const seed_seq&); // = delete;
    void operator=(const seed_seq&); // = delete;

    static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
};

template<class _InputIterator>
void
seed_seq::init(_InputIterator __first, _InputIterator __last)
{
    for (_InputIterator __s = __first; __s != __last; ++__s)
        __v_.push_back(*__s & 0xFFFFFFFF);
}

template<class _RandomAccessIterator>
void
seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
{
    if (__first != __last)
    {
        _STD::fill(__first, __last, 0x8b8b8b8b);
        const size_t __n = static_cast<size_t>(__last - __first);
        const size_t __s = __v_.size();
        const size_t __t = (__n >= 623) ? 11
                         : (__n >= 68) ? 7
                         : (__n >= 39) ? 5
                         : (__n >= 7)  ? 3
                         : (__n - 1) / 2;
        const size_t __p = (__n - __t) / 2;
        const size_t __q = __p + __t;
        const size_t __m = _STD::max(__s + 1, __n);
        // __k = 0;
        {
            result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
                                                      ^  __first[__n - 1]);
            __first[__p] += __r;
            __r += __s;
            __first[__q] += __r;
            __first[0] = __r;
        }
        for (size_t __k = 1; __k <= __s; ++__k)
        {
            const size_t __kmodn = __k % __n;
            const size_t __kpmodn = (__k + __p) % __n;
            result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
                                           ^ __first[(__k - 1) % __n]);
            __first[__kpmodn] += __r;
            __r +=  __kmodn + __v_[__k-1];
            __first[(__k + __q) % __n] += __r;
            __first[__kmodn] = __r;
        }
        for (size_t __k = __s + 1; __k < __m; ++__k)
        {
            const size_t __kmodn = __k % __n;
            const size_t __kpmodn = (__k + __p) % __n;
            result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
                                           ^ __first[(__k - 1) % __n]);
            __first[__kpmodn] += __r;
            __r +=  __kmodn;
            __first[(__k + __q) % __n] += __r;
            __first[__kmodn] = __r;
        }
        for (size_t __k = __m; __k < __m + __n; ++__k)
        {
            const size_t __kmodn = __k % __n;
            const size_t __kpmodn = (__k + __p) % __n;
            result_type __r = 1566083941 * _T(__first[__kmodn] +
                                              __first[__kpmodn] +
                                              __first[(__k - 1) % __n]);
            __first[__kpmodn] ^= __r;
            __r -= __kmodn;
            __first[(__k + __q) % __n] ^= __r;
            __first[__kmodn] = __r;
        }
    }
}

template<class _RealType, size_t __bits, class _URNG>
_RealType
generate_canonical(_URNG& __g)
{
    const size_t _Dt = numeric_limits<_RealType>::digits;
    const size_t __b = _Dt < __bits ? _Dt : __bits;
    const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
    const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
    const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
    _RealType __base = _R;
    _RealType _S = __g() - _URNG::_Min;
    for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
        _S += (__g() - _URNG::_Min) * __base;
    return _S / __base;
}

// __independent_bits_engine

template<class _Engine, class _UIntType>
class __independent_bits_engine
{
public:
    // types
    typedef _UIntType result_type;

private:
    typedef typename _Engine::result_type _Engine_result_type;
    typedef typename conditional
        <
            sizeof(_Engine_result_type) <= sizeof(result_type),
                result_type,
                _Engine_result_type
        >::type _Working_result_type;

    _Engine& __e_;
    size_t __w_;
    size_t __w0_;
    size_t __n_;
    size_t __n0_;
    _Working_result_type __y0_;
    _Working_result_type __y1_;
    _Engine_result_type __mask0_;
    _Engine_result_type __mask1_;

    static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
                                                         + _Working_result_type(1);
    static const size_t __m = __log2<_Working_result_type, _R>::value;
    static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
    static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;

public:
    // constructors and seeding functions
    __independent_bits_engine(_Engine& __e, size_t __w);

    // generating functions
    result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}

private:
    result_type __eval(false_type);
    result_type __eval(true_type);
};

template<class _Engine, class _UIntType>
__independent_bits_engine<_Engine, _UIntType>
    ::__independent_bits_engine(_Engine& __e, size_t __w)
        : __e_(__e),
          __w_(__w)
{
    __n_ = __w_ / __m + (__w_ % __m != 0);
    __w0_ = __w_ / __n_;
    if (_R == 0)
        __y0_ = _R;
    else if (__w0_ < _WDt)
        __y0_ = (_R >> __w0_) << __w0_;
    else
        __y0_ = 0;
    if (_R - __y0_ > __y0_ / __n_)
    {
        ++__n_;
        __w0_ = __w_ / __n_;
        if (__w0_ < _WDt)
            __y0_ = (_R >> __w0_) << __w0_;
        else
            __y0_ = 0;
    }
    __n0_ = __n_ - __w_ % __n_;
    if (__w0_ < _WDt - 1)
        __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
    else
        __y1_ = 0;
    __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
                          _Engine_result_type(0);
    __mask1_ = __w0_ < _EDt - 1 ?
                               _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
                               _Engine_result_type(~0);
}

template<class _Engine, class _UIntType>
inline
_UIntType
__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
{
    return static_cast<result_type>(__e_() & __mask0_);
}

template<class _Engine, class _UIntType>
_UIntType
__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
{
    result_type _S = 0;
    for (size_t __k = 0; __k < __n0_; ++__k)
    {
        _Engine_result_type __u;
        do
        {
            __u = __e_() - _Engine::min();
        } while (__u >= __y0_);
        if (__w0_ < _EDt)
            _S <<= __w0_;
        else
            _S = 0;
        _S += __u & __mask0_;
    }
    for (size_t __k = __n0_; __k < __n_; ++__k)
    {
        _Engine_result_type __u;
        do
        {
            __u = __e_() - _Engine::min();
        } while (__u >= __y1_);
        if (__w0_ < _EDt - 1)
            _S <<= __w0_ + 1;
        else
            _S = 0;
        _S += __u & __mask1_;
    }
    return _S;
}

template<class _IntType = int>
class uniform_int_distribution
{
public:
    // types
    typedef _IntType result_type;

    class param_type
    {
        result_type __a_;
        result_type __b_;
    public:
        typedef uniform_int_distribution distribution_type;

        explicit param_type(result_type __a = 0,
                            result_type __b = numeric_limits<result_type>::max())
            : __a_(__a), __b_(__b) {}

        result_type a() const {return __a_;}
        result_type b() const {return __b_;}

        friend bool operator==(const param_type& __x, const param_type& __y)
            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
        friend bool operator!=(const param_type& __x, const param_type& __y)
            {return !(__x == __y);}
    };

private:
    param_type __p_;

public:
    // constructors and reset functions
    explicit uniform_int_distribution(result_type __a = 0,
                                      result_type __b = numeric_limits<result_type>::max())
        : __p_(param_type(__a, __b)) {}
    explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
    void reset() {}

    // generating functions
    template<class _URNG> result_type operator()(_URNG& __g)
        {return (*this)(__g, __p_);}
    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);

    // property functions
    result_type a() const {return __p_.a();}
    result_type b() const {return __p_.b();}

    param_type param() const {return __p_;}
    void param(const param_type& __p) {__p_ = __p;}

    result_type min() const {return a();}
    result_type max() const {return b();}

    friend bool operator==(const uniform_int_distribution& __x,
                           const uniform_int_distribution& __y)
        {return __x.__p_ == __y.__p_;}
    friend bool operator!=(const uniform_int_distribution& __x,
                           const uniform_int_distribution& __y)
            {return !(__x == __y);}
};

template<class _IntType>
template<class _URNG>
typename uniform_int_distribution<_IntType>::result_type
uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
{
    typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
                                            uint32_t, uint64_t>::type _UIntType;
    const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
    if (_R == 1)
        return __p.a();
    const size_t _Dt = numeric_limits<_UIntType>::digits;
    typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
    if (_R == 0)
        return static_cast<result_type>(_Eng(__g, _Dt)());
    size_t __w = _Dt - __clz(_R) - 1;
    if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
        ++__w;
    _Eng __e(__g, __w);
    _UIntType __u;
    do
    {
        __u = __e();
    } while (__u >= _R);
    return static_cast<result_type>(__u + __p.a());
}

template <class _CharT, class _Traits, class _IT>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const uniform_int_distribution<_IT>& __x)
{
    __save_flags<_CharT, _Traits> _(__os);
    __os.flags(ios_base::dec | ios_base::left);
    _CharT __sp = __os.widen(' ');
    __os.fill(__sp);
    return __os << __x.a() << __sp << __x.b();
}

template <class _CharT, class _Traits, class _IT>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           uniform_int_distribution<_IT>& __x)
{
    typedef uniform_int_distribution<_IT> _Eng;
    typedef typename _Eng::result_type result_type;
    typedef typename _Eng::param_type param_type;
    __save_flags<_CharT, _Traits> _(__is);
    __is.flags(ios_base::dec | ios_base::skipws);
    result_type __a;
    result_type __b;
    __is >> __a >> __b;
    if (!__is.fail())
        __x.param(param_type(__a, __b));
    return __is;
}

template<class _RealType = double>
class uniform_real_distribution
{
public:
    // types
    typedef _RealType result_type;

    class param_type
    {
        result_type __a_;
        result_type __b_;
    public:
        typedef uniform_real_distribution distribution_type;

        explicit param_type(result_type __a = 0,
                            result_type __b = 1)
            : __a_(__a), __b_(__b) {}

        result_type a() const {return __a_;}
        result_type b() const {return __b_;}

        friend bool operator==(const param_type& __x, const param_type& __y)
            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
        friend bool operator!=(const param_type& __x, const param_type& __y)
            {return !(__x == __y);}
    };

private:
    param_type __p_;

public:
    // constructors and reset functions
    explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
        : __p_(param_type(__a, __b)) {}
    explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
    void reset() {}

    // generating functions
    template<class _URNG> result_type operator()(_URNG& __g)
        {return (*this)(__g, __p_);}
    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);

    // property functions
    result_type a() const {return __p_.a();}
    result_type b() const {return __p_.b();}

    param_type param() const {return __p_;}
    void param(const param_type& __p) {__p_ = __p;}

    result_type min() const {return a();}
    result_type max() const {return b();}

    friend bool operator==(const uniform_real_distribution& __x,
                           const uniform_real_distribution& __y)
        {return __x.__p_ == __y.__p_;}
    friend bool operator!=(const uniform_real_distribution& __x,
                           const uniform_real_distribution& __y)
        {return !(__x == __y);}
};

template<class _RealType>
template<class _URNG>
inline
typename uniform_real_distribution<_RealType>::result_type
uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
{
    return (__p.b() - __p.a())
        * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
        + __p.a();
}

template <class _CharT, class _Traits, class _RT>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const uniform_real_distribution<_RT>& __x)
{
    __save_flags<_CharT, _Traits> _(__os);
    __os.flags(ios_base::dec | ios_base::left);
    _CharT __sp = __os.widen(' ');
    __os.fill(__sp);
    return __os << __x.a() << __sp << __x.b();
}

template <class _CharT, class _Traits, class _RT>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           uniform_real_distribution<_RT>& __x)
{
    typedef uniform_real_distribution<_RT> _Eng;
    typedef typename _Eng::result_type result_type;
    typedef typename _Eng::param_type param_type;
    __save_flags<_CharT, _Traits> _(__is);
    __is.flags(ios_base::dec | ios_base::skipws);
    result_type __a;
    result_type __b;
    __is >> __a >> __b;
    if (!__is.fail())
        __x.param(param_type(__a, __b));
    return __is;
}

class bernoulli_distribution
{
public:
    // types
    typedef bool result_type;

    class param_type
    {
        double __p_;
    public:
        typedef bernoulli_distribution distribution_type;

        explicit param_type(double __p = 0.5) : __p_(__p) {}

        double p() const {return __p_;}

        friend bool operator==(const param_type& __x, const param_type& __y)
            {return __x.__p_ == __y.__p_;}
        friend bool operator!=(const param_type& __x, const param_type& __y)
            {return !(__x == __y);}
    };

private:
    param_type __p_;

public:
    // constructors and reset functions
    explicit bernoulli_distribution(double __p = 0.5)
        : __p_(param_type(__p)) {}
    explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
    void reset() {}

    // generating functions
    template<class _URNG> result_type operator()(_URNG& __g)
        {return (*this)(__g, __p_);}
    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
        {
            return _STD::generate_canonical<double, numeric_limits<double>::digits>(__g)
                   < __p.p();
        }

    // property functions
    double p() const {return __p_.p();}

    param_type param() const {return __p_;}
    void param(const param_type& __p) {__p_ = __p;}

    result_type min() const {return false;}
    result_type max() const {return true;}

    friend bool operator==(const bernoulli_distribution& __x,
                          const bernoulli_distribution& __y)
        {return __x.__p_ == __y.__p_;}
    friend bool operator!=(const bernoulli_distribution& __x,
                          const bernoulli_distribution& __y)
        {return !(__x == __y);}
};

template <class _CharT, class _Traits>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
{
    __save_flags<_CharT, _Traits> _(__os);
    __os.flags(ios_base::dec | ios_base::left);
    _CharT __sp = __os.widen(' ');
    __os.fill(__sp);
    return __os << __x.p();
}

template <class _CharT, class _Traits>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
{
    typedef bernoulli_distribution _Eng;
    typedef typename _Eng::param_type param_type;
    __save_flags<_CharT, _Traits> _(__is);
    __is.flags(ios_base::dec | ios_base::skipws);
    double __p;
    __is >> __p;
    if (!__is.fail())
        __x.param(param_type(__p));
    return __is;
}

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP_RANDOM
OpenPOWER on IntegriCloud