📌  相关文章
📜  根据给定条件计算最多N个可以形成非循环图的整数的排列

📅  最后修改于: 2021-06-25 17:54:53             🧑  作者: Mango

给定整数N,任务是根据以下条件从可以形成无环图的范围[1,N]中查找整数的排列数目:

  • 对于每1≤i≤N ,找到最大j ,使1≤j A [j]> A [i ],并在节点i和节点j之间添加无向边。
  • 对于每1≤i≤N ,找到最小的j ,使i A [j]> A [i] ,并在节点i和节点j之间添加无向边

例子:

方法:请按照以下步骤解决问题:

  • 因此,打印2 N – 1作为要求的答案。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Find the count of possible graphs
void possibleAcyclicGraph(int N)
{
    cout << pow(2, N - 1);
    return;
}
 
// Driver Code
int main()
{
 
    int N = 4;
    possibleAcyclicGraph(N);
 
    return 0;
}


Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Find the count of
// possible graphs
static void possibleAcyclicGraph(int N)
{
  System.out.print((int)Math.pow(2,
                                 N - 1));
  return;
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 4;
  possibleAcyclicGraph(N);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of above approach
 
# Find the count of possible graphs
def possibleAcyclicGraph(N):
     
    print(pow(2, N - 1))
    return
 
# Driver code
if __name__ == '__main__':
     
    N = 4
     
    possibleAcyclicGraph(N)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of
// the above approach
using System;
 
class GFG{
     
// Find the count of
// possible graphs
static void possibleAcyclicGraph(int N)
{
    Console.Write((int)Math.Pow(2, N - 1));
     
    return;
}
  
// Driver Code
public static void Main()
{
    int N = 4;
     
    possibleAcyclicGraph(N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出
8

时间复杂度:O(logN)
空间复杂度:O(1)