C, 748 746 651 bytes
#define C(v,n) for(v=n==D;v<=(n<=D);v++)
void p(Z){unsigned long long R,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D=Z*2,E,F;C(C,30)C(B,29)C(A,28)C(z,27)C(y,26)C(x,25)C(w,24)C(v,23)C(u,22)C(t,21)C(s,20)C(r,19)C(q,18)C(p,17)C(o,16)C(n,15)C(m,14)C(l,13)C(k,12)C(j,11)C(i,10)C(h,9)C(g,8)C(f,7)C(e,6)C(d,5)C(c,4)C(b,3)C(a,2){R=C<<29|B<<28|A<<27|z<<26|y<<25|x<<24|w<<23|v<<22|u<<21|t<<20|s<<19|r<<18|q<<17|p<<16|o<<15|n<<14|m<<13|l<<12|k<<11|j<<10|i<<9|h<<8|g<<7|f<<6|e<<5|d<<4|c<<3|b<<2|a<<1;if(__builtin_popcount(R)!=Z)goto X;F=0;for(E=D-1;E;E--)if(R&1<<E)F++;else if(F)F--;else goto X;for(;E<D;E++)putchar(40+!(R&1<<D-E-1));puts("");X:;}}
At a whopping >600 bytes, this isn't really much of a contender...but it's pretty fast and memory efficient.
Works by encoding the parentheses strings as integers, with a set bit representing (
and an unset bit representing )
. For instance, (())
would be 1100
. I made the following observations:
- All permutations must begin with
(
and end with )
; otherwise, it won't be valid.
- Every valid permutation must have
n
left parentheses and n
right parentheses, where n
is the input number. I used __builtin_popcount
for this.
When built via icc -march=native -O3
, it takes 17s on my computer, though others have reported better times (~3-7s) on their (presumably faster) computers. In addition, it uses around 1.5 KB of memory.
Here's the code with explanatory comments:
// Z is the input number.
void p(Z){
// All the variables.
// R is the resulting paren number, with 1 bits for open parens and 0s for closed ones.
// a-C are used to hold the state of each paren (1 if open, 0 if closed).
// D holds Z*2, or the total length of the paren string.
// E and F are used later on.
unsigned long long R,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D=Z*2,E,F;
/* Each loop performs a basic check. If D is equal to the corresponding
number, then the variable will start at 1 (since the first paren must
always be open). If D is greater than or equal to the number, then the loop
will only be run once with the variable set to 0, and a later check will
cause the variable to be ignored. Otherwise, it will loop twice; one for a
closed paren (0), and one for an open one (1). Of course, if D == the
number, then it will only loop for the open paren (as stated above). */
for (C = (D == 30); C <= (30 <= D); C++)
for (B = (D == 29); B <= (29 <= D); B++)
for (A = (D == 28); A <= (28 <= D); A++)
for (z = (D == 27); z <= (27 <= D); z++)
for (y = (D == 26); y <= (26 <= D); y++)
for (x = (D == 25); x <= (25 <= D); x++)
for (w = (D == 24); w <= (24 <= D); w++)
for (v = (D == 23); v <= (23 <= D); v++)
for (u = (D == 22); u <= (22 <= D); u++)
for (t = (D == 21); t <= (21 <= D); t++)
for (s = (D == 20); s <= (20 <= D); s++)
for (r = (D == 19); r <= (19 <= D); r++)
for (q = (D == 18); q <= (18 <= D); q++)
for (p = (D == 17); p <= (17 <= D); p++)
for (o = (D == 16); o <= (16 <= D); o++)
for (n = (D == 15); n <= (15 <= D); n++)
for (m = (D == 14); m <= (14 <= D); m++)
for (l = (D == 13); l <= (13 <= D); l++)
for (k = (D == 12); k <= (12 <= D); k++)
for (j = (D == 11); j <= (11 <= D); j++)
for (i = (D == 10); i <= (10 <= D); i++)
for (h = (D == 9); h <= (9 <= D); h++)
for (g = (D == 8); g <= (8 <= D); g++)
for (f = (D == 7); f <= (7 <= D); f++)
for (e = (D == 6); e <= (6 <= D); e++)
for (d = (D == 5); d <= (5 <= D); d++)
for (c = (D == 4); c <= (4 <= D); c++)
for (b = (D == 3); b <= (3 <= D); b++)
for (a = (D == 2); a <= (2 <= D); a++) {
/* R is the result here. Each variable is shifted and bitwise-ORd with the
others to generate a number representing the string.
For instance, (()) would be 1100, and ()(()) would be 101100. */
R=C<<29|B<<28|A<<27|z<<26|y<<25|x<<24|w<<23|v<<22|u<<21|t<<20|s<<19|r<<18|
q<<17|p<<16|o<<15|n<<14|m<<13|l<<12|k<<11|j<<10|i<<9|h<<8|g<<7|f<<6|
e<<5|d<<4|c<<3|b<<2|a<<1;
// If there is an uneven balance of parens, then just jump to the loop end.
if(__builtin_popcount(R)!=Z) goto X;
// F here is the number of opening parens.
F=0;
// E is a counter used to extract each bit.
for(E=D-1;E;E--)
// If the bit is set, then increment the number of open parens.
if(R&1<<E) F++;
// If the bit wasn't set, then decrement the number of open parens...
else if(F) F--;
// ...but, if there were no open parens, then it's unbalanced; jump to the end
else goto X;
// E (the bit counter) is already 0.
// Loop through each bit...
for(;E<D;E++)
/* ...and print it out. 40 is the ASCII code for `(`. If the bit
is set, the ! will negate it, and 0 will be added to the `(`.
Otherwise, it will turn the left paren into a right paren. */
putchar(40+!(R&1<<D-E-1));
// Newline.
puts("");
// End of loop.
X:;
}
}
746-byte version
#define C(v,n) for(v=(n==D);v<=(n<=D);v++){
void p(Z){unsigned long long R,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D=Z*2,E,F;C(C,30)C(B,29)C(A,28)C(z,27)C(y,26)C(x,25)C(w,24)C(v,23)C(u,22)C(t,21)C(s,20)C(r,19)C(q,18)C(p,17)C(o,16)C(n,15)C(m,14)C(l,13)C(k,12)C(j,11)C(i,10)C(h,9)C(g,8)C(f,7)C(e,6)C(d,5)C(c,4)C(b,3)C(a,2)R=(C<<29)|(B<<28)|(A<<27)|(z<<26)|(y<<25)|(x<<24)|(w<<23)|(v<<22)|(u<<21)|(t<<20)|(s<<19)|(r<<18)|(q<<17)|(p<<16)|(o<<15)|(n<<14)|(m<<13)|(l<<12)|(k<<11)|(j<<10)|(i<<9)|(h<<8)|(g<<7)|(f<<6)|(e<<5)|(d<<4)|(c<<3)|(b<<2)|(a<<1);if(__builtin_popcount(R)!=Z)continue;F=0;for(E=D-1;E;E--)if(R&(1<<E))F++;else if(F)F--;else goto X;for(;E<D;E++)putchar('('+!(R&(1<<D-E-1)));puts("");X:;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
You may not write recursive functions
Why not?Similarly, your program should not call itself.
Why not?Your program may not read or write files.
Submissions are unlikely to, but why not? That also goes against our default allowed IO methods. Also, it is standard to allow functions rather than just full programs. \$\endgroup\$