📜  在第一个HEAD右边的备用位置处具有HEAD的序列数

📅  最后修改于: 2021-04-23 21:23:44             🧑  作者: Mango

假设硬币被抛了N次。任务是找到抛掷序列的总数,以使从左起第一个头部之后,右侧的所有交替位置仅由头部占据。除交替位置外,其他任何位置均可被头部或尾部占据。

例如,如果您抛硬币10次(N = 10),并且第一个头部出现在第3个位置,那么右边的所有其他交替位置是5、7、9…

例子:

方法:
如果序列以尾部开始,则长度为N-1的所有可能序列均有效。现在,如果序列从头开始,则序列中的每个奇数索引(假设序列基于1)将为头,其余N / 2位可以为头或尾。因此,上述问题的递归公式为:

f(N) = f(N-1) + 2floor(N/2)

数学计算:

Let fo(N) and fe(N) be the functions 
that are defined for the odd and even values of N respectively.

 fo(N) =  fe(N-1) + 2(N-1)/2
 fe(N) =  fo(N-1) + 2N/2

From above equation compute
 fo(N) = fo(N-2) + 2(N-1)/2 + 2(N-1)/2
 fe(N) = fe(N-2) + 2N/2 + 2(N-2)/2

Base Case: 
 fo(1) = 2
 fe(0) = 1

By using the above equation, compute the following results :
 fo(N) - fo(N-2) =  2(N-1)/2 + 2(N-1)/2
 fo(N) - fo(N-2) =  2(N+1)/2 

By taking the sum of above equation for all odd values of N, 
below thing is computed.  
 fo(N) - fo(N-2) + fo(N-1) - fo(N-3) + ...... + fo(3) - fo(1) = 
 22 + 23 + 24 + ..... + 2(N+1)/2

Hence on summation,
fo(N) - fo(1) = SUM[ n = 0 to (N+1)/2 ] 2n - 21 - 20

By using sum of geometric progression
fo(N) = 2( N + 1 ) / 2 + 2( N + 1 ) / 2 - 2

Similarly, find fe(N) :
 fe(N) = fe(N-2) + 2N/2 + 2(N-2)/2
 fe(N) - fe(0) = SUM[ n = 0 to N/2 ] 2n - 20 - SUM[ n = 0 to (N-1)/2 ] 2n

By using sum of geometric progression
fe(N) = 2 (N / 2) + 1 + 2 N / 2 - 2

最终公式如下:

下面是上述方法的实现:

C++
// C++ program to find number of sequences
#include 
using namespace std;
  
// function to calculate total sequences possible
int findAllSequence(int N)
{
  
    // Value of N is even
    if (N % 2 == 0) {
        return pow(2, N / 2 + 1) + pow(2, N / 2) - 2;
    }
    // Value of N is odd
    else {
        return pow(2, (N + 1) / 2) + pow(2, (N + 1) / 2) - 2;
    }
}
  
// Driver code
int main()
{
    int N = 2;
    cout << findAllSequence(N) << endl;
    return 0;
}


Java
// Java program to find 
// number of sequences
import java.io.*;
  
class GFG 
{
  
// function to calculate 
// total sequences possible
static int findAllSequence(int N)
{
  
    // Value of N is even
    if (N % 2 == 0) 
    {
        return (int)(Math.pow(2, N / 2 + 1) + 
                     Math.pow(2, N / 2) - 2);
    }
      
    // Value of N is odd
    else 
    {
        return (int)(Math.pow(2, (N + 1) / 2) + 
                     Math.pow(2, (N + 1) / 2) - 2);
    }
}
  
// Driver code
public static void main (String[] args) 
{
    int N = 2;
    System.out.print( findAllSequence(N));
}
}
  
// This code is contributed
// by anuj_67.


Python3
# Python3 program to find number 
# of sequences
  
# function to calculate total
# sequences possible
def findAllSequence(N):
  
    # Value of N is even
    if (N % 2 == 0):
        return (pow(2, N / 2 + 1) + 
                pow(2, N / 2) - 2);
    # Value of N is odd
    else:
        return (pow(2, (N + 1) / 2) + 
                pow(2, (N + 1) / 2) - 2);
  
# Driver code
N = 2;
print(int(findAllSequence(N)));
  
# This code is contributed by mits


C#
// C# program to find 
// number of sequences
using System; 
public class GFG{
  
   
    // function to calculate 
    // total sequences possible
    static int findAllSequence(int N)
    {
  
        // Value of N is even
        if (N % 2 == 0) 
        {
            return (int)(Math.Pow(2, N / 2 + 1) + 
                         Math.Pow(2, N / 2) - 2);
        }
  
        // Value of N is odd
        else
        {
            return (int)(Math.Pow(2, (N + 1) / 2) + 
                         Math.Pow(2, (N + 1) / 2) - 2);
        }
    }
  
    // Driver code
    public static void Main () 
    {
        int N = 2;
        Console.WriteLine( findAllSequence(N));
    }
}
  
// This code is contributed by 29AjayKumar


PHP


输出:
4

时间复杂度:O(LogN)