📜  找到办法在N个球之间排列K个绿色球,这样我就需要准确地移动来收集所有K个绿色球

📅  最后修改于: 2021-04-22 07:49:33             🧑  作者: Mango

给定两个整数NK。连续放置N个球。其中K是绿色,而N – K是黑色。任务是找到排列N个球的方式的数量,这样一来,就需要准确地移动i(1≤i≤K)来收集所有绿色的球。一口气,我们可以收集任何组连续的绿色球。请注意,答案可能非常大。因此,输出答案取模10 9 + 7

例子:

方法:需要执行i步即可收集K个绿色球,这意味着K个绿色球被黑球分隔到i个位置。因此,让我们考虑以下组合。

  • 首先,连续排列N – K个黑球。
  • 在这些黑球之间,从左端到右端选择i个位置,并考虑在其中放置K个绿色球。有N – K + 1个C I的方式来选择这些。
  • 对于每个选择,请考虑将多少个绿色球分配给每个间隙。由于必须给每个分配一个或多个,因此有K – 1 C i – 1种方法来确定这一点。

因此,对于每个i ,答案为N – K + 1 C i * K – 1 C i – 1 。在这里讨论寻找n C r。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define N 100005
#define mod (int)(1e9 + 7)
  
// To store the factorial and the
// factorial mod inverse of a number
int factorial[N], modinverse[N];
  
// Function to find (a ^ m1) % mod
int power(int a, int m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (1LL * a * a) % mod;
    else if (m1 & 1)
        return (1LL * a
                * power(power(a, m1 / 2), 2))
               % mod;
    else
        return power(power(a, m1 / 2), 2) % mod;
}
  
// Function to find factorial
// of all the numbers
void factorialfun()
{
    factorial[0] = 1;
    for (int i = 1; i < N; i++)
        factorial[i] = (1LL
                        * factorial[i - 1] * i)
                       % mod;
}
  
// Function to find the factorial
// mod inverse of all the numbers
void modinversefun()
{
    modinverse[N - 1]
        = power(factorial[N - 1], mod - 2) % mod;
  
    for (int i = N - 2; i >= 0; i--)
        modinverse[i] = (1LL * modinverse[i + 1]
                         * (i + 1))
                        % mod;
}
  
// Function to return nCr
int binomial(int n, int r)
{
    if (r > n)
        return 0;
  
    int a = (1LL * factorial[n]
             * modinverse[n - r])
            % mod;
  
    a = (1LL * a * modinverse[r]) % mod;
    return a;
}
  
// Function to find ways to arrange K green
// balls among N balls such that we need
// exactly i moves to collect all K green balls
void arrange_balls(int n, int k)
{
    factorialfun();
    modinversefun();
  
    for (int i = 1; i <= k; i++)
        cout << (1LL * binomial(n - k + 1, i)
                 * binomial(k - 1, i - 1))
                    % mod
             << " ";
}
  
// Driver code
int main()
{
    int n = 5, k = 3;
  
    // Function call
    arrange_balls(n, k);
  
    return 0;
}


Python3
# Python3 implementation of the approach 
N = 100005
mod = (int)(1e9 + 7) 
  
# To store the factorial and the 
# factorial mod inverse of a number 
factorial = [0] * N;
modinverse = [0] * N; 
  
# Function to find (a ^ m1) % mod 
def power(a, m1) : 
      
    if (m1 == 0) :
        return 1; 
    elif (m1 == 1) :
        return a; 
    elif (m1 == 2) :
        return (a * a) % mod; 
    elif (m1 & 1) :
        return (a * power(power(a, m1// 2), 2)) % mod; 
    else :
        return power(power(a, m1 // 2), 2) % mod; 
  
# Function to find factorial 
# of all the numbers 
def factorialfun() :
  
    factorial[0] = 1; 
    for i in range(1, N) :
        factorial[i] = (factorial[i - 1] * i) % mod; 
  
# Function to find the factorial 
# mod inverse of all the numbers 
def modinversefun() :
    modinverse[N - 1] = power(factorial[N - 1], 
                                mod - 2) % mod;
      
    for i in range(N - 2 , -1, -1) :
        modinverse[i] = (modinverse[i + 1] * 
                                   (i + 1)) % mod; 
  
# Function to return nCr 
def binomial(n, r) : 
  
    if (r > n) :
        return 0; 
  
    a = (factorial[n] * 
         modinverse[n - r]) % mod; 
  
    a = (a * modinverse[r]) % mod; 
    return a; 
  
# Function to find ways to arrange K green 
# balls among N balls such that we need 
# exactly i moves to collect all K green balls 
def arrange_balls(n, k) :
    factorialfun(); 
    modinversefun(); 
  
    for i in range(1, k + 1) :
        print((binomial(n - k + 1, i) * 
               binomial(k - 1, i - 1)) % mod, 
                                  end = " "); 
  
# Driver code 
if __name__ == "__main__" : 
  
    n = 5; k = 3; 
  
    # Function call 
    arrange_balls(n, k); 
  
# This code is contributed by AnkitRai01


输出:
3 6 1