📌  相关文章
📜  从以循环方式排列的数字中计算不相邻的子集

📅  最后修改于: 2021-06-25 10:33:14             🧑  作者: Mango

假设有N个人坐在一个从1N的循环队列中,那么任务就是计算选择其中一个子集的方式的数目,这样就不会有两个连续的人坐在一起。答案可能很大,因此请以10 9 + 7为模计算答案。请注意,空子集也是有效的子集。

例子:

方法:让我们找到N的较小值的答案。

N = 1- >所有可能的子集为{},{1}
N = 2- >所有可能的子集是{},{1},{2}
N = 3- >所有可能的子集为{},{1},{2},{3}
N = 4- >所有可能的子集为{},{1},{2},{3},{4},{1、3},{2、4}
因此顺序将是2、3、4、7…
N = 5时,计数将为11 ;如果N = 6,则计数将为18
现在可以观察到,该序列类似于从第二项开始的斐波那契数列,前两项为3和4。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long
  
const ll N = 10000;
const ll MOD = 1000000007;
  
ll F[N];
  
// Function to pre-compute the sequence
void precompute()
{
  
    // For N = 1 the answer will be 2
    F[1] = 2;
  
    // Starting two terms of the sequence
    F[2] = 3;
    F[3] = 4;
  
    // Comute the rest of the sequence
    // with the relation
    // F[i] = F[i - 1] + F[i - 2]
    for (int i = 4; i < N; i++)
        F[i] = (F[i - 1] + F[i - 2]) % MOD;
}
  
// Driver code
int main()
{
    int n = 8;
  
    // Pre-compute the sequence
    precompute();
  
    cout << F[n];
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
static int N = 10000;
static int MOD = 1000000007;
  
static int []F = new int[N];
  
// Function to pre-compute the sequence
static void precompute()
{
  
    // For N = 1 the answer will be 2
    F[1] = 2;
  
    // Starting two terms of the sequence
    F[2] = 3;
    F[3] = 4;
  
    // Comute the rest of the sequence
    // with the relation
    // F[i] = F[i - 1] + F[i - 2]
    for (int i = 4; i < N; i++)
        F[i] = (F[i - 1] + F[i - 2]) % MOD;
}
  
// Driver code
public static void main(String []args)
{
    int n = 8;
  
    // Pre-compute the sequence
    precompute();
  
    System.out.println(F[n]);
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python implementation of the approach
N = 10000;
MOD = 1000000007;
  
F = [0] * N;
  
# Function to pre-compute the sequence
def precompute():
  
    # For N = 1 the answer will be 2
    F[1] = 2;
  
    # Starting two terms of the sequence
    F[2] = 3;
    F[3] = 4;
  
    # Comute the rest of the sequence
    # with the relation
    # F[i] = F[i - 1] + F[i - 2]
    for i in range(4,N):
        F[i] = (F[i - 1] + F[i - 2]) % MOD;
  
# Driver code
n = 8;
  
# Pre-compute the sequence
precompute();
print(F[n]);
  
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
      
class GFG 
{
static int N = 10000;
static int MOD = 1000000007;
  
static int []F = new int[N];
  
// Function to pre-compute the sequence
static void precompute()
{
  
    // For N = 1 the answer will be 2
    F[1] = 2;
  
    // Starting two terms of the sequence
    F[2] = 3;
    F[3] = 4;
  
    // Comute the rest of the sequence
    // with the relation
    // F[i] = F[i - 1] + F[i - 2]
    for (int i = 4; i < N; i++)
        F[i] = (F[i - 1] + F[i - 2]) % MOD;
}
  
// Driver code
public static void Main(String []args)
{
    int n = 8;
  
    // Pre-compute the sequence
    precompute();
  
    Console.WriteLine(F[n]);
}
}
  
// This code is contributed by 29AjayKumar


输出:
47