S(r,n)表示我们可以在长度为n的不可区分的圆周围排列r个对象的方式的数量,并且每个圆n周围都必须至少有一个对象。
例子:
Input: r = 9, n = 2
Output: 109584
Input: r = 6, n = 3
Output: 225
特殊情况是:
- S(r,0)= 0,微不足道。
- S(r,1)表示等于(r – 1)的循环置换!
- S(r,n)其中r = n等于1。
- S(r,r -1)= rC2
S(r,n)= S(r – 1,n – 1)+(r – 1)* S(r – 1,n)的斯特林数的重要标识
方法:为简单起见,用1、2,…,r表示r个不同的对象。考虑对象“ 1”。在对象的任何排列中,
- “ 1”是圆圈中的唯一对象或
- “ 1”与其他人围成一圈。
在情况1中,有s(r – 1,n – 1)种方式构成这种安排。在情况2中,首先,以s(r_1,n)的方式将r_1个对象2、3,…,r放入n个圆中;然后可以将“ 1”放置在r-1个不同对象之一中,位于相应r-1个不同对象的“直接权利”附近。根据乘法原理,在情况2中有(r-1)s(r_1,n)种形式的排列方式。现在,根据s(r,n)的定义和加法原理来确定恒等式。
使用初始值S(0,0)= 1,当r> 1且s(r,1)=(r_1)时,s(r,0)= 0!对于r> 1,并应用我们证明的身份,我们可以通过递归方式轻松计算出斯特林数。
在代码中,我们使用了三个函数来生成斯特林数,分别是nCr(n,r),这是一个计算我们所谓的函数(n –选择– r),我们可以使用r的方式数来自n个对象的对象,而没有排序的重要性。毫不奇怪,阶乘(int n)用于计算数字n的阶乘。函数Stirling number(r,n)使用上面讨论的四种基本情况递归工作,然后使用我们证明的恒等式递归工作。
下面是上述方法的实现:
C++
// C++ program to implement above approach
#include
using namespace std;
// Calculating factorial of an integer n.
long long factorial(int n)
{
// Our base cases of factorial 0! = 1! = 1
if (n == 0)
return 1;
// n can't be less than 0.
if (n < 0)
return -1;
long long res = 1;
for (int i = 2; i < n + 1; ++i)
res *= i;
return res;
}
// Function to compute the number of combination
// of r objects out of n objects.
int nCr(int n, int r)
{
// r cant be more than n so we'd like the
// program to crash if the user entered
// wrong input.
if (r > n)
return -1;
if (n == r)
return 1;
if (r == 0)
return 1;
// nCr(n, r) = nCr(n - 1, r - 1) + nCr(n - 1, r)
return nCr(n - 1, r - 1) + nCr(n - 1, r);
}
// Function to calculate the Stirling numbers.
// The base cases which were discussed above are handled
// to stop the recursive calls.
long long stirlingNumber(int r, int n)
{
// n can't be more than
// r, s(r, 0) = 0.
if (n > r)
return -1;
if (n == 0)
return 0;
if (r == n)
return 1;
if (n == 1)
return factorial(r - 1);
if (r - n == 1)
return nCr(r, 2);
else
return stirlingNumber(r - 1, n - 1)
+ (r - 1) * stirlingNumber(r - 1, n);
}
// Driver program
int main()
{
// Calculating the stirling number s(9, 2)
int r = 9, n = 2;
long long val = stirlingNumber(r, n);
if (val == -1)
cout << " No stirling number";
else
cout << "The Stirling Number s(" << r
<< ", " << n << ") is : " << val;
return 0;
}
Java
// Java program to implement
// above approach
import java.io.*;
class GFG
{
// Calculating factorial of
// an integer n.
static long factorial(int n)
{
// Our base cases of factorial
// 0! = 1! = 1
if (n == 0)
return 1;
// n can't be less than 0.
if (n < 0)
return -1;
long res = 1;
for (int i = 2; i < n + 1; ++i)
res *= i;
return res;
}
// Function to compute the number
// of combination of r objects
// out of n objects.
static int nCr(int n, int r)
{
// r cant be more than n so
// we'd like the program to
// crash if the user entered
// wrong input.
if (r > n)
return -1;
if (n == r)
return 1;
if (r == 0)
return 1;
return nCr(n - 1, r - 1) +
nCr(n - 1, r);
}
// Function to calculate the Stirling
// numbers. The base cases which were
// discussed above are handled to stop
// the recursive calls.
static long stirlingNumber(int r, int n)
{
// n can't be more than
// r, s(r, 0) = 0.
if (n > r)
return -1;
if (n == 0)
return 0;
if (r == n)
return 1;
if (n == 1)
return factorial(r - 1);
if (r - n == 1)
return nCr(r, 2);
else
return stirlingNumber(r - 1, n - 1) +
(r - 1) *
stirlingNumber(r - 1, n);
}
// Driver Code
public static void main (String[] args)
{
// Calculating the stirling number s(9, 2)
int r = 9, n = 2;
long val = stirlingNumber(r, n);
if (val == -1)
System.out.println(" No stirling number");
else
System.out.println( "The Stirling Number s(" +
r + ", " + n + ") is : " + val);
}
}
// This Code is Contributed by anuj_67
Python 3
# Python 3 program to implement above approach
# Function to compute the number of combination
# of r objects out of n objects.
# nCr(n, n) = 1, nCr(n, 0) = 1, and these are
# the base cases.
def nCr(n, r):
if(n == r):
return 1
if(r == 0):
return 1
# nCr(n, r) = nCr(n - 1, r - 1) + nCr(n - 1, r)
return nCr(n - 1, r - 1) + nCr(n - 1, r)
# This function is used to calculate the
# factorial of a number n.
def factorial(n):
res = 1
# 1 ! = 0 ! = 1
if(n <= 1):
return res
for i in range(1, n + 1):
res *= i
return res
# Main function to calculate the Stirling numbers.
# the base cases which were discussed above are
# handled to stop the recursive call, n can't be
# more than r, s(r, 0) = 0.
# s(r, r) = 1. s(r, 1) = (r - 1)!.
# s(r, r - 1) = nCr(r, 2)
# else as we proved, s(r, n) = s(r - 1, n - 1)
# + (r - 1) * s(r - 1, n)
def stirlingNumber(r, n):
if(r == n):
return 1
if(n == 0):
return 0
if(n == r -1):
return nCr(r, 2)
if(r - n == 1):
return factorial(r - 1)
return (stirlingNumber(r - 1, n - 1)
+ (r - 1) * stirlingNumber(r - 1, n))
r, n = 9, 2
print(stirlingNumber(r, n))
C#
// C# program to implement
// above approach
using System;
class GFG
{
// Calculating factorial of
// an integer n.
static long factorial(int n)
{
// Our base cases of factorial
// 0! = 1! = 1
if (n == 0)
return 1;
// n can't be less than 0.
if (n < 0)
return -1;
long res = 1;
for (int i = 2; i < n + 1; ++i)
res *= i;
return res;
}
// Function to compute the number
// of combination of r objects
// out of n objects.
static int nCr(int n, int r)
{
// r cant be more than n so
// we'd like the program to
// crash if the user entered
// wrong input.
if (r > n)
return -1;
if (n == r)
return 1;
if (r == 0)
return 1;
return nCr(n - 1, r - 1) +
nCr(n - 1, r);
}
// Function to calculate the Stirling
// numbers. The base cases which were
// discussed above are handled to stop
// the recursive calls.
static long stirlingNumber(int r, int n)
{
// n can't be more than
// r, s(r, 0) = 0.
if (n > r)
return -1;
if (n == 0)
return 0;
if (r == n)
return 1;
if (n == 1)
return factorial(r - 1);
if (r - n == 1)
return nCr(r, 2);
else
return stirlingNumber(r - 1, n - 1) +
(r - 1) *
stirlingNumber(r - 1, n);
}
// Driver Code
public static void Main ()
{
// Calculating the stirling
// number s(9, 2)
int r = 9, n = 2;
long val = stirlingNumber(r, n);
if (val == -1)
Console.WriteLine(" No stirling number");
else
Console.WriteLine( "The Stirling Number s(" +
r + ", " + n + ") is : " + val);
}
}
// This code is contributed by inder_verma..
PHP
$n)
return -1;
if ($n == $r)
return 1;
if ($r == 0)
return 1;
// nCr($n, $r) = nCr($n - 1, $r - 1) + nCr($n - 1, $r)
return nCr($n - 1, $r - 1) + nCr($n - 1, $r);
}
// Function to calculate the Stirling numbers.
// The base cases which were discussed above are handled
// to stop the recursive calls.
function stirlingNumber($r, $n)
{
// n can't be more than
// r, s(r, 0) = 0.
if ($n > $r)
return -1;
if ($n == 0)
return 0;
if ($r == $n)
return 1;
if ($n == 1)
return factorial($r - 1);
if ($r - $n == 1)
return nCr($r, 2);
else
return stirlingNumber($r - 1, $n - 1)
+ ($r - 1) * stirlingNumber($r - 1, $n);
}
// Calculating the stirling number s(9, 2)
$r = 9;
$n = 2;
$val = stirlingNumber($r, $n);
if ($val == -1)
echo " No stirling number";
else
echo "The Stirling Number s(", $r
,", " , $n , ") is : " , $val;
// This code is contributed by ANKITRAI1
?>
The Stirling Number s(9, 2) is : 109584
注意:可以使用动态编程来优化上述解决方案。例如,请参阅响铃编号(对集合进行分区的方式编号)。
请参阅第一种斯特林编号,以了解有关斯特林编号的更多信息。