📜  欧拉数

📅  最后修改于: 2021-06-25 16:41:30             🧑  作者: Mango

在组合学中,欧拉数A(n,m)是数字1到n的排列数,其中正好m个元素大于前一个元素。
例如,存在数字1到3的4个排列,其中正好1个元素大于先前的元素。

欧拉

例子 :

Input : n = 3, m = 1
Output : 4
Please see above diagram (There
are 4 permutations where 1 no. is
greater.

Input : n = 4, m = 1
Output : 11

欧拉数是下述欧拉多项式的系数。

欧拉多项式由指数生成函数定义

欧拉多项式可以通过递归来计算

A(n,m)的一个明确公式是

我们可以通过递归关系来计算A(n,m):

例子:
假设n = 3和m = 1。
所以,
A(3,1)
=(3 – 1)* A(2,0)+(1 +1)* A(2,1)
= 2 * A(2,0)+ 2 * A(2,1)
= 2 * 1 + 2 *((2-1)* A(1,0)+(1 +1)* A(1,1))
= 2 + 2 *(1 * 1 + 2 *((1-1)* A(0,0)+(1 +1)* A(0,1)))
= 2 + 2 *(1 + 2 *(0 * 1 + 2 * 0)
= 2 + 2 *(1 + 2 * 0)
= 2 + 2 * 1
= 2 + 2
= 4
我们可以使用上面显示的示例进行验证。
下面是找到A(n,m)的实现:

C++
// CPP Program to find Eulerian number A(n, m)
#include 
using namespace std;
 
// Return euleriannumber A(n, m)
int eulerian(int n, int m)
{
    if (m >= n || n == 0)
        return 0;
 
    if (m == 0)
        return 1;
 
    return (n - m) * eulerian(n - 1, m - 1) +
           (m + 1) * eulerian(n - 1, m);
}
 
// Driven Program
int main()
{
    int n = 3, m = 1;
    cout << eulerian(n, m) << endl;
    return 0;
}


Java
// Java rogram to find Eulerian number A(n, m)
import java.util.*;
 
class Eulerian
{
    // Return eulerian number A(n, m)
    public static int eulerian(int n, int m)
    {
        if (m >= n || n == 0)
            return 0;
 
        if (m == 0)
            return 1;
 
        return (n - m) * eulerian(n - 1, m - 1) +
            (m + 1) * eulerian(n - 1, m);
    }
     
    // driver code   
    public static void main(String[] args)
    {
        int n = 3, m = 1;
        System.out.print( eulerian(n, m) );
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 Program to find Eulerian number A(n, m)
 
# Return euleriannumber A(n, m)
def eulerian(n, m):
    if (m >= n or n == 0):
        return 0;
 
    if (m == 0):
        return 1;
 
    return ((n - m) * eulerian(n - 1, m - 1) +
            (m + 1) * eulerian(n - 1, m))
 
# Driver code
n = 3
m = 1
print( eulerian(n, m) )
 
# This code is contributed by rishabh_jain


C#
// C# rogram to find Eulerian number A(n, m)
using System;
 
class Eulerian {
     
    // Return eulerian number A(n, m)
    public static int eulerian(int n, int m)
    {
        if (m >= n || n == 0)
            return 0;
 
        if (m == 0)
            return 1;
 
        return (n - m) * eulerian(n - 1, m - 1) +
                    (m + 1) * eulerian(n - 1, m);
    }
 
    // driver code
    public static void Main()
    {
        int n = 3, m = 1;
        Console.WriteLine(eulerian(n, m));
    }
}
 
// This code is contributed by vt_m


PHP
= $n || $n == 0)
        return 0;
 
    if ($m == 0)
        return 1;
 
    return ($n - $m) * eulerian($n - 1, $m - 1) +
                 ($m + 1) * eulerian($n - 1, $m);
}
 
// Driven Code
$n = 3; $m = 1;
echo eulerian($n, $m);
 
// This code is contributed by anuj_67.
?>


Javascript


C++
// CPP Program to find Eulerian number A(n, m)
#include 
using namespace std;
 
// Return euleriannumber A(n, m)
int eulerian(int n, int m)
{
    int dp[n + 1][m + 1];
 
    memset(dp, 0, sizeof(dp));
 
    // For each row from 1 to n
    for (int i = 1; i <= n; i++) {
 
        // For each column from 0 to m
        for (int j = 0; j <= m; j++) {
 
            // If i is greater than j
            if (i > j) {
 
                // If j is 0, then make that
                // state as 1.
                if (j == 0)
                    dp[i][j] = 1;
 
                // basic recurrence relation.
                else
                    dp[i][j] = ((i - j) *
                     dp[i - 1][j - 1]) +
                    ((j + 1) * dp[i - 1][j]);
            }
        }
    }
 
    return dp[n][m];
}
 
// Driven Program
int main()
{
    int n = 3, m = 1;
    cout << eulerian(n, m) << endl;
    return 0;
}


Java
// Java rogram to find Eulerian number A(n, m)
import java.util.*;
 
class Eulerian
{
    // Return euleriannumber A(n, m)
    public static int eulerian(int n, int m)
    {
        int[][] dp = new int[n+1][m+1];
 
        // For each row from 1 to n
        for (int i = 1; i <= n; i++) {
     
            // For each column from 0 to m
            for (int j = 0; j <= m; j++) {
 
                // If i is greater than j
                if (i > j) {
 
                    // If j is 0, then make
                    // that state as 1.
                    if (j == 0)
                        dp[i][j] = 1;
 
                    // basic recurrence relation.
                    else
                        dp[i][j] = ((i - j) *
                            dp[i - 1][j - 1]) +
                        ((j + 1) * dp[i - 1][j]);
                }
            }
        }
 
        return dp[n][m];
    }
     
    // driver code
    public static void main(String[] args)
    {
        int n = 3, m = 1;
        System.out.print( eulerian(n, m) );
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 Program to find Eulerian
# number A(n, m)
 
# Return euleriannumber A(n, m)
def eulerian(n, m):
    dp = [[0 for x in range(m+1)]
             for y in range(n+1)]
 
    # For each row from 1 to n
    for i in range(1, n+1):
 
        # For each column from 0 to m
        for j in range(0, m+1):
 
            # If i is greater than j
            if (i > j):
                # If j is 0, then make that
                # state as 1.
 
                if (j == 0):
                    dp[i][j] = 1
 
                # basic recurrence relation.
                else :
                    dp[i][j] = (((i - j) *
                       dp[i - 1][j - 1]) +
                       ((j + 1) * dp[i - 1][j]))
                     
    return dp[n][m]
 
# Driven Program
n = 3
m = 1
print(eulerian(n, m))
 
# This code is contributed by Prasad Kshirsagar


C#
// C# rogram to find Eulerian number A(n, m)
using System;
 
class Eulerian {
     
    // Return euleriannumber A(n, m)
    public static int eulerian(int n, int m)
    {
        int[, ] dp = new int[n + 1, m + 1];
 
        // For each row from 1 to n
        for (int i = 1; i <= n; i++) {
 
            // For each column from 0 to m
            for (int j = 0; j <= m; j++) {
 
                // If i is greater than j
                if (i > j) {
 
                    // If j is 0, then make
                    // that state as 1.
                    if (j == 0)
                        dp[i, j] = 1;
 
                    // basic recurrence relation.
                    else
                        dp[i, j] = ((i - j) * dp[i - 1, j - 1]) +
                                        ((j + 1) * dp[i - 1, j]);
                }
            }
        }
 
        return dp[n, m];
    }
 
    // driver code
    public static void Main()
    {
        int n = 3, m = 1;
        Console.WriteLine(eulerian(n, m));
    }
}
 
// This code is contributed by vt_m


PHP
 $j)
            {
 
                // If j is 0, then make that
                // state as 1.
                if ($j == 0)
                    $dp[$i][$j] = 1;
 
                // basic recurrence relation.
                else
                    $dp[$i][$j] = (($i - $j) *
                                    $dp[$i - 1][$j - 1]) +
                                  (($j + 1) * $dp[$i - 1][$j]);
            }
        }
    }
 
    return $dp[$n][$m];
}
 
// Driver Code
$n = 3 ;
$m = 1;
echo eulerian($n, $m) ;
 
// This code is contributed by Ryuga
?>


Javascript


输出 :

4

以下是使用动态编程查找A(n,m)的实现:

C++

// CPP Program to find Eulerian number A(n, m)
#include 
using namespace std;
 
// Return euleriannumber A(n, m)
int eulerian(int n, int m)
{
    int dp[n + 1][m + 1];
 
    memset(dp, 0, sizeof(dp));
 
    // For each row from 1 to n
    for (int i = 1; i <= n; i++) {
 
        // For each column from 0 to m
        for (int j = 0; j <= m; j++) {
 
            // If i is greater than j
            if (i > j) {
 
                // If j is 0, then make that
                // state as 1.
                if (j == 0)
                    dp[i][j] = 1;
 
                // basic recurrence relation.
                else
                    dp[i][j] = ((i - j) *
                     dp[i - 1][j - 1]) +
                    ((j + 1) * dp[i - 1][j]);
            }
        }
    }
 
    return dp[n][m];
}
 
// Driven Program
int main()
{
    int n = 3, m = 1;
    cout << eulerian(n, m) << endl;
    return 0;
}

Java

// Java rogram to find Eulerian number A(n, m)
import java.util.*;
 
class Eulerian
{
    // Return euleriannumber A(n, m)
    public static int eulerian(int n, int m)
    {
        int[][] dp = new int[n+1][m+1];
 
        // For each row from 1 to n
        for (int i = 1; i <= n; i++) {
     
            // For each column from 0 to m
            for (int j = 0; j <= m; j++) {
 
                // If i is greater than j
                if (i > j) {
 
                    // If j is 0, then make
                    // that state as 1.
                    if (j == 0)
                        dp[i][j] = 1;
 
                    // basic recurrence relation.
                    else
                        dp[i][j] = ((i - j) *
                            dp[i - 1][j - 1]) +
                        ((j + 1) * dp[i - 1][j]);
                }
            }
        }
 
        return dp[n][m];
    }
     
    // driver code
    public static void main(String[] args)
    {
        int n = 3, m = 1;
        System.out.print( eulerian(n, m) );
    }
}
 
// This code is contributed by rishabh_jain

Python3

# Python3 Program to find Eulerian
# number A(n, m)
 
# Return euleriannumber A(n, m)
def eulerian(n, m):
    dp = [[0 for x in range(m+1)]
             for y in range(n+1)]
 
    # For each row from 1 to n
    for i in range(1, n+1):
 
        # For each column from 0 to m
        for j in range(0, m+1):
 
            # If i is greater than j
            if (i > j):
                # If j is 0, then make that
                # state as 1.
 
                if (j == 0):
                    dp[i][j] = 1
 
                # basic recurrence relation.
                else :
                    dp[i][j] = (((i - j) *
                       dp[i - 1][j - 1]) +
                       ((j + 1) * dp[i - 1][j]))
                     
    return dp[n][m]
 
# Driven Program
n = 3
m = 1
print(eulerian(n, m))
 
# This code is contributed by Prasad Kshirsagar

C#

// C# rogram to find Eulerian number A(n, m)
using System;
 
class Eulerian {
     
    // Return euleriannumber A(n, m)
    public static int eulerian(int n, int m)
    {
        int[, ] dp = new int[n + 1, m + 1];
 
        // For each row from 1 to n
        for (int i = 1; i <= n; i++) {
 
            // For each column from 0 to m
            for (int j = 0; j <= m; j++) {
 
                // If i is greater than j
                if (i > j) {
 
                    // If j is 0, then make
                    // that state as 1.
                    if (j == 0)
                        dp[i, j] = 1;
 
                    // basic recurrence relation.
                    else
                        dp[i, j] = ((i - j) * dp[i - 1, j - 1]) +
                                        ((j + 1) * dp[i - 1, j]);
                }
            }
        }
 
        return dp[n, m];
    }
 
    // driver code
    public static void Main()
    {
        int n = 3, m = 1;
        Console.WriteLine(eulerian(n, m));
    }
}
 
// This code is contributed by vt_m

的PHP

 $j)
            {
 
                // If j is 0, then make that
                // state as 1.
                if ($j == 0)
                    $dp[$i][$j] = 1;
 
                // basic recurrence relation.
                else
                    $dp[$i][$j] = (($i - $j) *
                                    $dp[$i - 1][$j - 1]) +
                                  (($j + 1) * $dp[$i - 1][$j]);
            }
        }
    }
 
    return $dp[$n][$m];
}
 
// Driver Code
$n = 3 ;
$m = 1;
echo eulerian($n, $m) ;
 
// This code is contributed by Ryuga
?>

Java脚本


输出 :

4