-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA089839.c
5711 lines (4944 loc) · 238 KB
/
A089839.c
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
/*****************************************************************************/
/* http://www.research.att.com/~njas/sequences/a089839.c.txt */
/* */
/* a089839.c -- A lean and fast C-program to count empirically the amount of */
/* distinct non-recursive Catalan automorphisms. */
/* */
/* See the entries A089831-A089843 in Neil Sloane's Online-Encyclopedia */
/* of Integer Sequences at: http://www.research.att.com/~njas/sequences/ */
/* */
/* Coded 2003 by Antti Karttunen, http://www.iki.fi/kartturi/ */
/* and placed in Public Domain. */
/* First edited 7. Nov 2003. */
/* Edited 11. Oct 2006. (Sequences in A123XXX range). */
/* Edited 22. May 2007. (Sequences A129604-A129612.) */
/* Last edited 05 Jan 2009 (Sequences A153826-A153834 & A154121-A154126) */
/* */
/* Mirrored at: */
/* http://www.iki.fi/kartturi/matikka/Nekomorphisms/a089839.c */
/* */
/* The idea of program in short: */
/* In principle (*), iterate over all partial_sums(A089836) compositions */
/* of binary tree "transformation clauses", and examine which ones */
/* give bijections which are not encountered before. Count them, */
/* and give the results as table A089831, etc. */
/* */
/* (* in practice we can do some obvious filtering, see the functions */
/* clause_seq_next_composition and clause_seq_next) */
/* */
/* For the essence of the fast implementation, see the functions */
/* apply_clauses_to_x and select_vectors_needed */
/* */
/* To get correct results (and to avoid crashing!), the command line */
/* parameters must be large enough: */
/* Both a089839 127 8 19394489 and a089839 127 9 15485863 */
/* produce the results that are submitted to OEIS as of 5 Dec 2003. */
/* */
/* Note: This program is growing too complex, with all the low level memory */
/* allocation, hash table lookups, tricky optimizations, and so on. */
/* Currently we are in a vicious circle: this program practically defines */
/* how the table A089840 and many of the associated sequences are formed, */
/* against which the correctness of this program should be checked! */
/* (However, see the comments before the function increment_clause.) */
/* */
/* We need to write a separate, clean, non-optimized implementation in some */
/* higher level language (Prolog, Scheme, Haskell?), that might be very */
/* slow, but at least it would be easier to reckon that the program returns */
/* intended results. The said program could generate all the nonrecursive */
/* automorphisms, say, from A089840[0] to A089840[1786785] and store them to */
/* a separate file/database in some accessible format */
/* (e.g. as CLAUSESEQ's/Prolog-rules's and possibly also automatically */
/* construed/optimized Scheme-functions) and the program together with its */
/* output would then serve as a frozen reference against which the output */
/* of this program (and its future improved versions) could be compared to. */
/* */
/* Also, we need a formal proof about what is the minimum size of binary */
/* trees which need to be checked up to, before the equivalence of two */
/* nonrecursive automorphisms with similarly beginning signature */
/* permutations can be rigorously decided. (And to detect which clause */
/* sequences are not bijections.) */
/* However, intuitively it seems that it is enough to choose the size where */
/* no further differences in output (e.g. the number of distinct bijections */
/* found) occur. Thus it is certainly a positive sign that the two command */
/* examples above (with size given as 8 and 9) produce the same results, */
/* for the max. 7 node clause sequences. */
/* */
/* See also http://www.research.att.com/~njas/sequences/A089840p.txt */
/* See also http://www.research.att.com/~njas/sequences/a089408.c.txt */
/* */
/* If you have any suggestions or questions, you can mail them to me */
/* to my address <My firstname>.<My surname>@gmail.com */
/* */
/* Cheers, Antti Karttunen */
/* */
/*****************************************************************************/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAX_CLAUSES 20
#define MAX_CLAUSE_SIZE 20
/* With our 64 bits MAXSIZE is 31, with 32 bit machines it's only 15.
(because we need one extra bit for the last leaf of binary trees) */
#ifdef ONLY32BITS
#define MAXSIZE 21
#define MAXSIZE_FOR_TBBS 15
#else
#define MAXSIZE 31
#define MAXSIZE_FOR_TBBS 31
#endif
typedef unsigned char BYTE;
typedef unsigned long int ULI;
typedef unsigned short int USI;
#define FILLED_BYTE ((BYTE)((~((BYTE) 0))))
#ifdef REQUIRES_LONG_LONG
typedef unsigned long long int ULLI; /* Two longs on 64-bit Suns */
typedef long long int SLLI; /* Ditto. */
#else /* We are running on Alpha, or maybe some small 32-bit machine. */
typedef unsigned long int ULLI; /* Only one long on DEC Alpha's */
typedef long int SLLI; /* Ditto. */
#endif
typedef ULLI TBBS; /* TBBS = totally balanced binary sequence. With 32 bits only upto size n=15, with 64 upto n=31. */
typedef SLLI RANK; /* With 32 bits we can go upto size n=19, with 64 much further. */
typedef int SIZE;
typedef USI SGRANK; /* 2 bytes for "Short Global Ranks", i.e. valid up to size n=10. */
typedef USI SLRANK; /* 2 bytes for "Short Local Ranks", i.e. valid up to size n=11. */
typedef ULI PERMRANK; /* 4 bytes for permutation ranks, valid upto size n=11, i.e. 12 leaves. */
typedef ULI NORERANK; /* Ditto for the rank of distinct bijections in A089840. */
typedef BYTE SSIZE; /* One byte for short size. */
typedef ULLI LRANK; /* Local RANK, semantically different from a global rank. */
#define MAX_COMPUTATION_SIZE 10
#define MAX_BINEXP 255 /* I.e. only up to clause sequences of the total size=8. */
#define add_to_checksum(OLDCHKSUM,Y) ((31*(OLDCHKSUM))+(Y))
/**********************************************************************/
int globopt_output_cycle_lists = 0;
char *globopt_list_begin = "(";
char *globopt_list_delim = "";
char *globopt_list_end = ")\n";
char *globopt_elem_delim = ",";
char *globopt_pair_begin = "(";
char *globopt_pair_delim = " ";
char *globopt_pair_end = ")";
char *globopt_comment_begin = ";;";
int globopt_output_invariance_indices = 0;
int globopt_compute_A153830 = 0;
FILE *fpA153826 = NULL, *fpA153827 = NULL, *fpA153828 = NULL,
*fpA153829 = NULL, *fpA153830 = NULL, *fpA153831 = NULL;
int globopt_HTML = 0;
char *glob_author_info = "Antti Karttunen (His_Firstname.His_Surname(AT)gmail.com)";
char *glob_datestr = "Mon DD 20YY"; /* E.g. , Oct 28 2003 */
/************************************************************************/
/* */
/* Added 14. Oct 2003: SEXP-structure (a simple planar binary tree) */
/* because most of the Catalan automorphisms are easier to define in */
/* such Lisp-like terms, than to work directly on the binary sequences. */
/* */
/************************************************************************/
struct s_exp
{
struct s_exp *s_car; /* I.e. the left branch of the binary tree. */
struct s_exp *s_cdr; /* I.e. the right branch of the ---- "" ---- */
};
typedef struct s_exp SEXP_cell;
typedef SEXP_cell *SEXP;
#define NULLP(s) ((s) == NULL)
#define PAIR(s) (!NULLP(s))
#define CAR(s) ((s)->s_car)
#define CDR(s) ((s)->s_cdr)
#define SET_CAR(s,x) (((s)->s_car) = x)
#define SET_CDR(s,x) (((s)->s_cdr) = x)
long int glob_cons_cells_in_use = 0; /* For detecting possible leaks. */
SEXP cons(SEXP carside,SEXP cdrside)
{
SEXP newcell = ((SEXP) calloc(1,sizeof(SEXP_cell)));
if(NULL == newcell)
{
fprintf(stderr,
"cons: Couldn't allocate a chunk of %u bytes to store one cons cell!\n",
sizeof(SEXP_cell));
exit(1);
}
/* fflush(stdout); fprintf(stderr,"cons called!\n"); fflush(stderr); */
glob_cons_cells_in_use++;
SET_CAR(newcell,carside);
SET_CDR(newcell,cdrside);
return(newcell);
}
/* Taking plunge on car-side, find the first cons-cell
with either its CAR or CDR-side NULL,
and return that cell as a result,
to be reused by recons
*/
SEXP degraft(SEXP *subroot)
{
SEXP z;
if(NULLP(CAR(*subroot)))
{
z = *subroot;
*subroot = CDR(z);
return(z);
}
if(NULLP(CDR(*subroot)))
{
z = *subroot;
*subroot = CAR(z);
return(z);
}
return(degraft(&(CAR(*subroot))));
}
/* Here an all-out (not incremental like here!) deconstruction of
the previous S-expression to a separate free-list
(which just means flattening it!)
might be faster on larger S-expressions:
*/
SEXP recons(SEXP carside,SEXP cdrside,SEXP *reused)
{
if((NULL == reused) || NULLP(*reused))
{ return(cons(carside,cdrside)); }
else
{
SEXP z = degraft(reused);
SET_CAR(z,carside);
SET_CDR(z,cdrside);
return(z);
}
}
/* Recursively free the tree allocated with cons. */
void free_cons_tree(SEXP node)
{
if(NULLP(node)) { return; }
if(!NULLP(CAR(node))) { free_cons_tree(CAR(node)); }
if(!NULLP(CDR(node))) { free_cons_tree(CDR(node)); }
free(node);
glob_cons_cells_in_use--;
}
/* Recursively free the list allocated with cons, up to the upto_n:th node. */
void free_cons_list(SEXP node,int upto_n)
{
int i;
SEXP next_cdr;
for(i=0; i < upto_n; i++)
{
if(!PAIR(node))
{
fprintf(stderr,
"free_cons_list: Fatal error: the list to be freed is only %u elems long, shorter than expected %u.\n",
i,upto_n);
exit(1);
}
next_cdr = CDR(node);
free(node);
glob_cons_cells_in_use--;
node = next_cdr;
}
}
/* Taking first plunge on the cdr-side before car-side,
count all leaves (NULL's), incrementing *leafpos each time,
and when *leafpos is zero, insert a new cons(NULL,NULL) node
at that leaf, and return 1.
(We could use setjmp, longjmp, but now I'm too lazy.)
*/
int graft_scion_aux(SEXP s,SEXP scion,int *leafpos)
{
SEXP z;
if(NULLP(CDR(s)))
{
if(0 == *leafpos) { SET_CDR(s,scion); return(1); }
--*leafpos;
}
else
{
if(1 == graft_scion_aux(CDR(s),scion,leafpos)) { return(1); }
}
if(NULLP(CAR(s)))
{
if(0 == *leafpos) { SET_CAR(s,scion); return(1); }
--*leafpos;
}
else
{
if(1 == graft_scion_aux(CAR(s),scion,leafpos)) { return(1); }
}
return(0); /* No change made yet. */
}
/* See A153250. */
int graft_bud_at(SEXP s,int leafpos)
{
SEXP bud = cons(NULL,NULL);
return(graft_scion_aux(s,bud,&leafpos));
}
/* Returns 1 if the bintree t1 is wholly containd in bintree t2,
otherwise zero.
See A082858 */
int sexp_is_subtree(SEXP t1,SEXP t2)
{
if(NULLP(t1)) { return(1); } /* t1 finished, succeeding at this branch. */
if(NULLP(t2)) { return(0); } /* t2 finished, although t1 not, fail. */
if(0 == sexp_is_subtree(CAR(t1),CAR(t2))) /* If left hand side of t1 is */
{ return(0); } /* not subtree of left side of t2 then fail immediately. */
else /* Check also the right hand side. */
{ return(sexp_is_subtree(CDR(t1),CDR(t2))); }
}
/**********************************************************************/
/* Better to pass this by reference... */
struct evaluation_context
{
int upto_size_n;
int vec_size;
int bijectivity_broken_at_size_n;
int clear_next_time_upto_size_n;
ULLI checksum;
SGRANK *vals_obtained;
SGRANK *vals_obtained2; /* When we need compositions. */
USI **all_vecs[(2*MAX_CLAUSE_SIZE)+2];
USI **bud_vecs;
USI **subtree_bitvecs;
};
typedef struct evaluation_context ECOTEX;
/**********************************************************************/
#define LONG_ONE ((ULLI) 1)
#define two_to(n) (LONG_ONE << (n))
#define A000217(n) (((n)*(n+1))>>1)
#define tr2seqind(n,m) (A000217(n)+m)
#define tr2seqind11(n,m) tr2seqind(n-1,m-1)
/* tr2seqind(0,0) -> 0
tr2seqind(1,0) -> 1
tr2seqind(1,1) -> 2
tr2seqind(2,0) -> 3
tr2seqind(2,1) -> 4
tr2seqind(2,2) -> 5
tr2seqind(3,0) -> 6
etc.
*/
#define ar2seqind(row,col) (((row+col)*(row+col) + (3*row) + col)>>1)
/* ar2seqind(0,0) -> 0
ar2seqind(0,1) -> 1
ar2seqind(1,0) -> 2
ar2seqind(0,2) -> 3
ar2seqind(1,1) -> 4
ar2seqind(2,0) -> 5
ar2seqind(0,3) -> 6
etc.
*/
int binwidth(int n)
{
int i=0;
while(n != 0) { n >>= 1; i++; }
return(i);
}
/**********************************************************************/
/* Bit-operators copied from a089408.c.txt */
#define kth_byte(n) ((int) ((n) >> 3)) /* From RANK to int */
#define kth_bit(n) (((RANK)(n)) << 3) /* From int to RANK */
#define ith_bit_in_byte(n) ((BYTE) (1 << ((n) & 7))) /* From RANK to BYTE */
#define toggle_bit_on(B,n) (B[kth_byte(n)] |= ith_bit_in_byte(n))
#define toggle_bit_off(B,n) (B[kth_byte(n)] &= ~ith_bit_in_byte(n))
#define bit_is_zero(B,n) (0 == (B[kth_byte(n)] & ith_bit_in_byte(n)))
#define index2t(tsize,rank_s,rank_l) ((Cat(tsize+1)*rank_s)+(rank_l))
/**********************************************************************/
PERMRANK sA000142[13] = { 1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600 };
#define A000142(n) (sA000142[n])
#ifdef ONLY32BITS
#define SS 20
#else
#define SS 33
#endif
RANK sA00108[SS] = {1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786,
208012, 742900, 2674440, 9694845, 35357670, 129644790,
477638700, 1767263190 /* The last value < 2^32 at n=19 */
#ifndef ONLY32BITS
,6564120420, 24466267020, 91482563640, 343059613650,
1289904147324, 4861946401452, 18367353072152,
69533550916004, 263747951750360, 1002242216651368,
3814986502092304, 14544636039226909, 55534064877048198
#endif
};
#define A000108(n) (sA00108[n])
#define Cat(n) (sA00108[n])
#define A001246(n) (Cat(n)*Cat(n))
/* The last value < 2^32 at n=19 */
RANK sA014137[SS] = {1, 2, 4, 9, 23, 65, 197, 626, 2056, 6918, 23714, 82500,
290512, 1033412, 3707852, 13402697, 48760367, 178405157,
656043857, ((RANK)2423307047)
#ifndef ONLY32BITS
,8987427467, 33453694487, 124936258127, 467995871777,
1757900019101, 6619846420553, 24987199492705,
94520750408709, 358268702159069, 1360510918810437,
5175497420902741, 19720133460129650, 75254198337177848
#endif
};
#define A014137(n) (sA014137[n])
#define global2local_rank(grank,size) ((grank)-A014137(size-1))
/* Note: both rows and columns start from -1 */
/* Entry at CatTriangle(r,m) = CatTriangle(r,m-1) + CatTriangle(r-1,m) */
#define CatTriangle(r,c) (tA009766[(r+2)][(c+1)])
#ifdef ONLY32BITS
#define TR 21
#else
#define TR 34
#endif
RANK tA009766[][TR+1] = /* 34 rows in full version. */
{
{0},
{0, 0},
{0, 1, 0},
{0, 1, 1, 0},
{0, 1, 2, 2, 0},
{0, 1, 3, 5, 5, 0},
{0, 1, 4, 9, 14, 14, 0},
{0, 1, 5, 14, 28, 42, 42, 0},
{0, 1, 6, 20, 48, 90, 132, 132, 0},
{0, 1, 7, 27, 75, 165, 297, 429, 429, 0},
{0, 1, 8, 35, 110, 275, 572, 1001, 1430, 1430, 0},
{0, 1, 9, 44, 154, 429, 1001, 2002, 3432, 4862, 4862, 0},
{0, 1, 10, 54, 208, 637, 1638, 3640, 7072, 11934, 16796, 16796, 0},
{0, 1, 11, 65, 273, 910, 2548, 6188, 13260, 25194, 41990, 58786, 58786, 0},
{0, 1, 12, 77, 350, 1260, 3808, 9996, 23256, 48450, 90440, 149226, 208012,
208012, 0},
{0, 1, 13, 90, 440, 1700, 5508, 15504, 38760, 87210, 177650, 326876, 534888,
742900, 742900, 0},
{0, 1, 14, 104, 544, 2244, 7752, 23256, 62016, 149226, 326876, 653752,
1188640, 1931540, 2674440, 2674440, 0},
{0, 1, 15, 119, 663, 2907, 10659, 33915, 95931, 245157, 572033, 1225785,
2414425, 4345965, 7020405, 9694845, 9694845, 0},
{0, 1, 16, 135, 798, 3705, 14364, 48279, 144210, 389367, 961400, 2187185,
4601610, 8947575, 15967980, 25662825, 35357670, 35357670, 0},
{0, 1, 17, 152, 950, 4655, 19019, 67298, 211508, 600875, 1562275, 3749460,
8351070, 17298645, 33266625, 58929450, 94287120, 129644790, 129644790, 0},
{0, 1, 18, 170, 1120, 5775, 24794, 92092, 303600, 904475, 2466750, 6216210,
14567280, 31865925, 65132550, 124062000, 218349120, 347993910, 477638700,
477638700, 0},
{0, 1, 19, 189, 1309, 7084, 31878, 123970, 427570, 1332045, 3798795,
10015005, 24582285, 56448210, 121580760, 245642760, 463991880, 811985790,
1289624490, 1767263190, 1767263190, 0}
#ifndef ONLY32BITS
/* Then follows the first row with a value > 4294967295. */
,{0, 1, 20, 209, 1518, 8602, 40480, 164450, 592020, 1924065, 5722860,
15737865, 40320150, 96768360, 218349120, 463991880, 927983760, 1739969550,
3029594040, 4796857230, 6564120420, 6564120420, 0},
{0, 1, 21, 230, 1748, 10350, 50830, 215280, 807300, 2731365, 8454225,
24192090, 64512240, 161280600, 379629720, 843621600, 1771605360,
3511574910, 6541168950, 11338026180, 17902146600, 24466267020,
24466267020, 0},
{0, 1, 22, 252, 2000, 12350, 63180, 278460, 1085760, 3817125, 12271350,
36463440, 100975680, 262256280, 641886000, 1485507600, 3257112960,
6768687870, 13309856820, 24647883000, 42550029600, 67016296620,
91482563640, 91482563640, 0},
{0, 1, 23, 275, 2275, 14625, 77805, 356265, 1442025, 5259150, 17530500,
53993940, 154969620, 417225900, 1059111900, 2544619500, 5801732460,
12570420330, 25880277150, 50528160150, 93078189750, 160094486370,
251577050010, 343059613650, 343059613650, 0},
{0, 1, 24, 299, 2574, 17199, 95004, 451269, 1893294, 7152444, 24682944,
78676884, 233646504, 650872404, 1709984304, 4254603804, 10056336264,
22626756594, 48507033744, 99035193894, 192113383644, 352207870014,
603784920024, 946844533674, 1289904147324, 1289904147324, 0},
{0, 1, 25, 324, 2898, 20097, 115101, 566370, 2459664, 9612108, 34295052,
112971936, 346618440, 997490844, 2707475148, 6962078952, 17018415216,
39645171810, 88152205554, 187187399448, 379300783092, 731508653106,
1335293573130, 2282138106804, 3572042254128, 4861946401452,
4861946401452, 0},
{0, 1, 26, 350, 3248, 23345, 138446, 704816, 3164480, 12776588, 47071640,
160043576, 506662016, 1504152860, 4211628008, 11173706960, 28192122176,
67837293986, 155989499540, 343176898988, 722477682080, 1453986335186,
2789279908316, 5071418015120, 8643460269248, 13505406670700,
18367353072152, 18367353072152, 0},
{0, 1, 27, 377, 3625, 26970, 165416, 870232, 4034712, 16811300, 63882940,
223926516, 730588532, 2234741392, 6446369400, 17620076360, 45812198536,
113649492522, 269638992062, 612815891050, 1335293573130, 2789279908316,
5578559816632, 10649977831752, 19293438101000, 32798844771700,
51166197843852, 69533550916004, 69533550916004, 0},
{0, 1, 28, 405, 4030, 31000, 196416, 1066648, 5101360, 21912660, 85795600,
309722116, 1040310648, 3275052040, 9721421440, 27341497800, 73153696336,
186803188858, 456442180920, 1069258071970, 2404551645100, 5193831553416,
10772391370048, 21422369201800, 40715807302800, 73514652074500,
124680849918352, 194214400834356, 263747951750360, 263747951750360, 0},
{0, 1, 29, 434, 4464, 35464, 231880, 1298528, 6399888, 28312548, 114108148,
423830264, 1464140912, 4739192952, 14460614392, 41802112192, 114955808528,
301758997386, 758201178306, 1827459250276, 4232010895376, 9425842448792,
20198233818840, 41620603020640, 82336410323440, 155851062397940,
280531912316292, 474746313150648, 738494264901008, 1002242216651368,
1002242216651368, 0},
{0, 1, 30, 464, 4928, 40392, 272272, 1570800, 7970688, 36283236, 150391384,
574221648, 2038362560, 6777555512, 21238169904, 63040282096, 177996090624,
479755088010, 1237956266316, 3065415516592, 7297426411968, 16723268860760,
36921502679600, 78542105700240, 160878516023680, 316729578421620,
597261490737912, 1072007803888560, 1810502068789568, 2812744285440936,
3814986502092304, 3814986502092304, 0},
{0, 1, 31, 495, 5423, 45815, 318087, 1888887, 9859575, 46142811, 196534195,
770755843, 2809118403, 9586673915, 30824843819, 93865125915, 271861216539,
751616304549, 1989572570865, 5054988087457, 12352414499425, 29075683360185,
65997186039785, 144539291740025, 305417807763705, 622147386185325,
1219408876923237, 2291416680811797, 4101918749601365, 6914663035042301,
10729649537134605, 14544636039226909, 14544636039226909, 0},
{0, 1, 32, 527, 5950, 51765, 369852, 2258739, 12118314, 58261125, 254795320,
1025551163, 3834669566, 13421343481, 44246187300, 138111313215,
409972529754, 1161588834303, 3151161405168, 8206149492625, 20558563992050,
49634247352235, 115631433392020, 260170725132045, 565588532895750,
1187735919081075, 2407144796004312, 4698561476816109, 8800480226417474,
15715143261459775, 26444792798594380, 40989428837821289, 55534064877048198,
55534064877048198, 0}
#endif
};
USI *sA069770 = NULL;
int A069770(int n)
{
if(NULL == sA069770)
{
fprintf(stderr,"A069770: Should not call this function at this time as the sequence sA069770 has not been initialized!\n");
exit(1);
}
else { return(sA069770[n]); }
}
/*
See Frank Ruskey's thesis at:
http://www.cs.uvic.ca/~fruskey/Publications/Thesis/Thesis.html
*/
RANK CatalanRank(SIZE n,TBBS a)
{
int m = -1;
int y = 0;
LRANK r = 0;
while(a > 0)
{
if(0 == (a & 1))
{
m++;
}
else
{
y++;
r += CatTriangle(m,y);
}
a >>= 1;
}
return(r);
}
TBBS CatalanUnrank(SIZE n,LRANK r)
{
int m = n-1;
int y = n;
TBBS a = 0;
while(m >= 0)
{
LRANK c = CatTriangle(m,y);
a <<= 1;
if(r >= c)
{
y--;
a++;
r -= c;
}
else
{
m--;
}
}
return(a);
}
static int CRS_m, CRS_y;
static LRANK CRS_r;
void CatalanRankSexpAux(SEXP node)
{
if(NULLP(node)) { CRS_m--; }
else
{
CRS_r += CatTriangle(CRS_m,CRS_y);
CRS_y--;
CatalanRankSexpAux(CAR(node));
CatalanRankSexpAux(CDR(node));
}
}
RANK CatalanRankSexp(SIZE n,SEXP node)
{
CRS_m = n-1;
CRS_y = n;
CRS_r = 0;
CatalanRankSexpAux(node);
return(CRS_r);
}
SEXP CatalanUnrankSexp(SIZE n,LRANK r,SEXP *reused)
{
int m = n-1;
int y = n;
int sp = 0;
int rightson = 0;
SEXP root = NULL;
SEXP sonstack[MAXSIZE+1];
while(m >= 0)
{
LRANK c = CatTriangle(m,y);
if(r >= c)
{
SEXP newbranch = recons(NULL,NULL,reused);
if(NULLP(root)) { root = newbranch; }
else
{
if(rightson) { SET_CDR(sonstack[sp],newbranch); }
else { SET_CAR(sonstack[sp],newbranch); sp++; }
}
sonstack[sp] = newbranch;
y--;
r -= c;
rightson = 0; /* The next one is a left son. */
}
else
{
m--;
sp -= rightson;
rightson = 1; /* The next one is a right son. */
}
}
return(root);
}
RANK CatalanRankGlobal(SIZE n,TBBS a)
{
if(0 == n) { return(0); }
else { return(A014137(n-1)+CatalanRank(n,a)); }
}
RANK CatalanRankSexpGlobal(SIZE n,SEXP s)
{
if(0 == n) { return(0); }
else { return(A014137(n-1)+CatalanRankSexp(n,s)); }
}
void print_sexp(SEXP s)
{
putchar('(');
while(!NULLP(s)) { print_sexp(CAR(s)); s = CDR(s); }
putchar(')');
}
/* See the section "Number Conversion" at the end of
the excerpt: http://www.iki.fi/~kartturi/matikka/kl10exmp.txt
*/
int fprint_ulli(FILE *fp,ULLI x)
{
int s = 0;
if(x >= 10) { s = fprint_ulli(fp,(x/((ULLI)10))); }
fputc(('0' + (x%((ULLI)10))),fp);
return(s+1);
}
/* We could as well compute it on runtime, of course... */
void CheckTriangle(int upto_n)
{
int r,m;
for(r=0; r <= upto_n; r++)
{
for(m=0; m < r; m++)
{
if((CatTriangle(r,m-1) + CatTriangle(r-1,m)) != CatTriangle(r,m))
{
fprintf(stderr,"(CatTriangle(%d,%d) + CatTriangle(%d,%d)) = ",
r,(m-1),(r-1),m);
fprint_ulli(stderr,(CatTriangle(r,m-1) + CatTriangle(r-1,m)));
fprintf(stderr," differs from CatTriangle(%d,%d) = ",
r,m);
fprint_ulli(stderr,CatTriangle(r,m));
fprintf(stderr,"\n");
exit(1);
}
}
if((r > 0) && (CatTriangle(r,m) != CatTriangle(r,m-1)))
{
fprintf(stderr,"(CatTriangle(%d,%d) = ",r,m);
fprint_ulli(stderr,CatTriangle(r,m));
fprintf(stderr," differs from CatTriangle(%d,%d) = ",r,(m-1));
fprint_ulli(stderr,CatTriangle(r,m-1));
fprintf(stderr,"\n");
exit(1);
}
}
/* fprintf(stderr,"Triangle OK!\n"); */
}
void CheckRankings(int upto_n) /* Well, superficially... */
{
int n;
RANK r,r2,uplim;
for(n=0; n <= upto_n; n++)
{
uplim = Cat(n);
for(r=0; r < uplim; r++)
{
TBBS tbs = CatalanUnrank(n,r);
if(globopt_output_cycle_lists)
{
fprint_ulli(stdout,tbs); printf(" ");
}
r2 = CatalanRank(n,tbs);
if(r2 != r)
{
fflush(stdout);
fprintf(stderr,"CatalanRank(%d,",n);
fprint_ulli(stderr,tbs);
fprintf(stderr,")=");
fprint_ulli(stderr,r2);
fprintf(stderr," != ");
fprint_ulli(stderr,r);
fprintf(stderr,"\n");
exit(1);
}
}
if(globopt_output_cycle_lists) { printf("\n"); }
}
fprintf(stdout,"Ranking & Unranking OK upto n=%d.\n", upto_n);
}
void CheckSexpRankings(int upto_n) /* Well, superficially... */
{
int n;
RANK r,r2,uplim;
SEXP old_s = NULL;
for(n=0; n <= upto_n; n++)
{
uplim = Cat(n);
for(r=0; r < uplim; r++)
{
SEXP s = CatalanUnrankSexp(n,r,&old_s);
if(globopt_output_cycle_lists)
{
print_sexp(s); printf(" ");
}
r2 = CatalanRankSexp(n,s);
if(r2 != r)
{
fflush(stdout);
fprintf(stderr,"CatalanRankSexp(%d,s)=",n);
fprint_ulli(stderr,r2);
fprintf(stderr," != ");
fprint_ulli(stderr,r);
fprintf(stderr,"\n");
exit(1);
}
old_s = s;
}
if(globopt_output_cycle_lists) { printf("\n"); }
}
fprintf(stdout,"Ranking & Unranking OK upto n=%d.\n", upto_n);
}
/**********************************************************************/
ULI sA127301_up_to6917[6918];
ULI sA129593_up_to6917[6918];
ULI sA153835_up_to6917[6918];
void fprint_USI_vector(FILE *fp,USI *vec,int veclen)
{
int i;
fprintf(fp,"%s",globopt_list_begin);
for(i=0; i < veclen; i++)
{
if(i>0) { fprintf(fp,"%s",globopt_elem_delim); }
fprintf(fp,"%u",*(vec+i));
}
fprintf(fp,"%s",globopt_list_end);
}
void fprint_vector_of_USI_vectors(FILE *fp,USI **vec,int veclen)
{
while(NULL != *vec)
{
fprint_USI_vector(fp,*vec++,veclen);
}
}
void fprint_ULI_vector(FILE *fp,ULI *vec,int veclen)
{
int i;
fprintf(fp,"%s",globopt_list_begin);
for(i=0; i < veclen; i++)
{
if(i>0) { fprintf(fp,"%s",globopt_elem_delim); }
fprintf(fp,"%lu",*(vec+i));
}
fprintf(fp,"%s",globopt_list_end);
}
#define NA_SENTINEL ((ULI) -1) /* I.e. 4294967295 with 32 bits. */
void fprint_ULI_vector_with_NA_SENTINELs(FILE *fp,ULI *vec,int veclen)
{
int i;
fprintf(fp,"%s",globopt_list_begin);
for(i=0; i < veclen; i++)
{
if(i>0) { fprintf(fp,"%s",globopt_elem_delim); }
if(NA_SENTINEL == *(vec+i)) { fprintf(fp,"NA"); }
else { fprintf(fp,"%lu",*(vec+i)); }
}
fprintf(fp,"%s",globopt_list_end);
}
void fprint_ULLI_vector(FILE *fp,ULLI *vec,int veclen)
{
int i;
fprintf(fp,"%s",globopt_list_begin);
for(i=0; i < veclen; i++)
{
if(i>0) { fprintf(fp,"%s",globopt_elem_delim); }
fprint_ulli(fp,*(vec+i));
}
fprintf(fp,"%s",globopt_list_end);
}
void fprint_ULLI_vector_up_to_first_0(FILE *fp,ULLI *vec,int veclen)
{
int i;
fprintf(fp,"%s",globopt_list_begin);
for(i=0; (i < veclen) && (0 != *(vec+i)); i++)
{
if(i>0) { fprintf(fp,"%s",globopt_elem_delim); }
fprint_ulli(fp,*(vec+i));
}
fprintf(fp,"%s",globopt_list_end);
}
char *w6d(char *tb,int n)
{
sprintf(tb,"%06u",n);
return(tb);
}
void output_HTML_Alink(FILE *fp,int Anum)
{
char tb[81] = { 'A' };
char *Astr = (w6d(tb+1,Anum)-1);
fprintf(fp,"<A HREF=\"http://www.research.att.com/projects/OEIS?Anum=%s\">%s</A>",Astr,Astr);
}
void output_n_chars(int i, char c)
{
while(i--) { putchar(c); }
}
void output_n_spaces(int i) { output_n_chars(i,' '); }
/* How about templates? */
int USIvec_find_pos_of_1st_larger_than_one(USI *vec,int veclen)
{
int pos_of_1st_term_gte_2 = 0;
while((pos_of_1st_term_gte_2 < veclen)
&& (*(vec+pos_of_1st_term_gte_2) < 2))
{ pos_of_1st_term_gte_2++; }
if(pos_of_1st_term_gte_2 >= veclen) { pos_of_1st_term_gte_2 = 1; } /* Not found, use 1. */
else { pos_of_1st_term_gte_2++; } /* Because One-based. */
return(pos_of_1st_term_gte_2);
}
int ULIvec_find_pos_of_1st_larger_than_one(ULI *vec,int veclen)
{
int pos_of_1st_term_gte_2 = 0;
while((pos_of_1st_term_gte_2 < veclen)
&& (*(vec+pos_of_1st_term_gte_2) < 2))
{ pos_of_1st_term_gte_2++; }
if(pos_of_1st_term_gte_2 >= veclen) { pos_of_1st_term_gte_2 = 1; } /* Not found, use 1. */
else { pos_of_1st_term_gte_2++; } /* Because One-based. */
return(pos_of_1st_term_gte_2);
}