| Bytes | Lang | Time | Link |
|---|---|---|---|
| 019 | Husk | 250309T143915Z | Glory2Uk |
| 094 | JavaScript ES7 | 241101T110746Z | Arnauld |
| 090 | JavaScript Node.js | 241102T090335Z | l4m2 |
| 137 | Python 3 | 241031T224831Z | Lucenapo |
| 010 | Jelly | 241031T181735Z | Jonathan |
| 014 | Japt x | 241031T171333Z | Shaggy |
| 097 | Wolfram Language Mathematica | 241031T180056Z | ZaMoC |
| 061 | Charcoal | 241101T064926Z | Neil |
| 008 | Nekomata + n | 241101T022133Z | alephalp |
| 012 | 05AB1E | 241101T005058Z | Kevin Cr |
| 011 | Vyxal | 241031T221424Z | emanresu |
| 024 | Uiua | 241031T204841Z | nyxbird |
Husk, 19 bytes
L₁⁴δ#<₁²δ#>Pḣ⁰
§fo=
This could possibly be improved further
Takes 3 arguments in this order: n, b, c.
Another ugly but a more clear 19-bytes version: §#o=⁴δ#>§fo=²δ#<Pḣ⁰
Helper function:
§fo= -- defines a partial fuction that filters a list of permutations
-- based on the match of the arguments
Main program
Pḣ⁰ -- generate a list of permutations for the sequence 1..n
₁²δ#> -- filter the permutations, having the number of their corresponding elements greater than in the original list equal to the argument b
₁⁴δ#< -- filter again, matching the counts of the elements that are less than the reference numbers with the argument c
L -- get the length of the resulting list
JavaScript (ES7), 94 bytes
-1 thanks to @l4m2
Expects (n)(b,c) (t is ignored). Simple brute-force search.
n=>g=(b,c,k=0,m=2**n-1)=>m?(h=j=>m>>j&&h(-~j)+(m>>j&1&&g(b-(j<k),c-(j>k),k+1,m^1<<j)))``:!b&!c
JavaScript (Node.js), 90 bytes
f=(n,g,l,F=2**n-1,G,b=F&-F)=>b?f(n,g,l,F^=b,G^b)+f(--n,g-(2**n>b),l-(2**n<b),F^G):!(n|g|l)
n: input (length)
g: input (greater)
l: input (less)
F: flags (available bits)
G: flags (bits not used here because using here considered in somewhere else)
b: pick a bit from F
Python 3, 154 147 137 bytes
lambda n,t,b,c,R=range:sum(sorted((r[a]>a)-(r[a]<a)for a in R(n))==[-1]*c+[0]*t+b*[1]for r in I.permutations(R(n)))
import itertools as I
Jelly, 13 11 10 bytes
-2 thanks to emanresu A (count occurrences instead of filtering and taking the length)
Œ!>ż<ɗR{§ċ
A dyadic Link that accepts n on the left and [b, c] on the right (t being implicit due to the \$t + b + c = n\$ guarantee).
Try it online! (see the permutations by removing the trailing L)
How?
Œ!>ż<ɗR{§ċ - Link: n; [b, c]
Œ! - all permutations of {[1..n]}
R{ - range of {n} -> [1..n]
ɗ - last three links as a dyad - f(Pemutations, [1..n]):
> - {Pemutations} greater than {[1..n]} (vectorises)
< - {Pemutations} less than {[1..n]} (vectorises)
ż - zip these two lists of lists
§ - sums -> [[#higher, #lower] for each Permutation]
ċ - count occurrences of {[b, c]} in {that}
Potential faster approach
I believe this is correct but have not rigorously proved it to myself...
First calculate the number, \$P_{n,t}\$, of permutations of \$n\$ elements with exactly \$t\$ fixed points as
$$P_{n,t} = n! \sum_{i=t}^n \frac{-1^{(i-t)}}{t!(i-t)!}$$
Then calculate the number of derangements of the remaining \$n-t\$ elements (since they all need to move), \$D_{n-t}\$ as:
$$D_{n-t} = (n-t)! \sum_{i=0}^{n-t}\frac{-1^{(n-t-i)}}{(n-t-i)!}$$
(Or use the fact that \$D_{n-t}\$ is the nearest integer to \$\frac{(n-t)!}{e}\$, if you can get \$e\$ to arbitrary precision.)
Then calculate the number of derangements of \$(n-t)\$ with exactly \$b\$ exceedances (or equivalently \$c=t-n-b\$ exceedances as this is symmetric), \$D_{n-t,b}\$ using the recursive formula:
$$D_{s+1,x} = x D_{s,x} + (s+1-x) D_{s,x-1} + sD_{s-1,x-1}$$
(As per the OEIS entry A046739 - is there a closed form of this?)
Now the final count is given by:
$$\frac{P_{n,t}D_{n-t,b}}{D_{n-t}}$$
or, equivalently
$$\frac{P_{n,t}D_{n-t,c}}{D_{n-t}}$$
I'm not up to golfing that all in Jelly right now, although one way to golf the approach would be to use the fact that:
$$D_z = \sum_{i=0}^{z} D_{z,i}$$
Japt -x, 22 20 14 bytes
Takes the targets as an array in the order c,t,b.
o á ËüÈgYÃmÊeV
o á ËüÈgYÃmÊeV :Implicit input of integer U=n and array V=[c,t,b]
o :Range [0,U)
á :Permutations
Ë :Map
ü : Group & sort by
È : The following function
g : Sign of difference with
Y : 0-based index
à : End grouping
m : Map
Ê : Length
eV : Is equal to V?
:Implicit output of sum of resulting array
Original 20 bytes
Preserving this one as I quite liked the construction of the array of comparison operators. Takes t, b & c as an array in reverse order.
o á £V˶XèEg§¬o³i>Ã×
o á £V˶XèEg§¬o³i>Ã× :Implicit input of integer U=n and array V=[c,b,t]
o :Range [0,U)
á :Permutation
£ :Map each X
VË : Map each D in V
¶ : Is D equal to
Xè : Count the elements in X that return true when compared to their 0-based indices thusly
Eg : Index E into
§ : "<="
¬ : Split
o : Modify the last element
³ : Triplicate (to give the strictly-equal-to comparison operator. Duplicating would also work, to give the loosely-equal-to operator.)
i> : Prepend ">"
à : End inner map
× : Reduce by multplication
:Implicit output of sum of resulting array
Wolfram Language (Mathematica), 97 bytes
Tr[1^Select[b={##2};s=Range@#;#-s&/@Permutations@s,(f=#;Count[f,u_/;Sign@u==#]&/@{0,1,-1}==b)&]]&
Charcoal, 61 bytes
⊞υ⊞OE³N…·¹NFυ«≔⊟ιηFη«≔⊕⁻›κLη‹κLηζ¿§ιζ⊞υ⊞OEι⁻λ⁼μζ⁻η⟦κ⟧»M¬η→»Iⅈ
Try it online! Link is to verbose version of code. Takes inputs in the order c t b n. Explanation:
⊞υ⊞OE³N…·¹NFυ«
Start a breadth-first search with an array c t b and a range from 1 to n.
≔⊟ιηFη«
Loop over the remaining elements in the range.
≔⊕⁻›κLη‹κLηζ
See whether this element would be fixed, higher or lower when placed at the next last position.
¿§ιζ
If there is an available place for an element of that type, then...
⊞υ⊞OEι⁻λ⁼μζ⁻η⟦κ⟧
... decrement the number of places and remove that element from the range, and push the result to the search list.
»M¬η→
Keep count of completely valid permutations.
»Iⅈ
Output the final count.
Nekomata + -n, 8 bytes
↕x-±→Ħ-ž
Take input as n [b,t,c].
↕x-±→Ħ-ž
↕ Find a permutation of [0,1,...,n-1]
x Push [0,1,...,n-1]
- Subtract
± Signum
→ Increment
Ħ Histogram; count occurrences of 0,1,2,...
- Subtract [b,t,c]
ž Check if all elements are 0
-n counts the number of solutions.
05AB1E, 12 bytes
Lœʒā.S1Ý¢Q}g
Inputs as \$n\$ and \$[t,b]\$ (without \$c\$, since \$t+b+c=n\$ is guaranteed).
Try it online or verify all test cases.
If we would take all four values as inputs, it's 1 byte longer by replacing 1Ý ([0,1]) with ®1Ÿ ([-1,0,1]) and taking the second input in the order \$[c,t,b]\$:
Try it online or verify all test cases.
Explanation:
L # Push a list in the range [1, first (implicit) input-integer n]
œ # Get all permutations of this list
ʒ # Filter this list of permutation-lists by:
ā # Push a list in the range [1,length] (without popping)
.S # Vectorized compare each value at the same positions:
# -1 if a<b; 0 if a==b; 1 if a>b
1Ý # Push pair [0,1]
¢ # Count how many times those occur in the list of compares
Q # Check whether this list equals the second (implicit) input-pair
}g # After the filter: pop and push the length
# (which is output implicitly as result)
Vyxal, 11 bytes
ɾṖƛż₍><Ṡ;$O
$O # Count the number of times [b, c] occurs in
ɾṖ # Permutations of 1...n
ƛ ; # For each
ż # Take the range from 1 to n
₍-- # Pair
> # Whether each is higher than its index
< # or lower
Ṡ # Sum these, giving [b, c]
Uiua, 24 bytes
/+≡(/↧⬚0=°⊚+1±-°⊏)⧅≠⟜⇡⊙¤
/+≡(/↧⬚0=°⊚+1±-°⊏)⧅≠⟜⇡⊙¤
⧅≠⟜⇡ # generate all permutations of 0..n-1
≡( ) # for each row:
±-°⊏ # get the sign of the comparison to the original indices
°⊚+1 # find the counts of [>, =, <]
/↧⬚0= ⊙¤ # do they match [b, t, c] (filling empty spaces with 0)?
/+ # count those which are true