📜  集合上不自反关系的数量

📅  最后修改于: 2021-04-17 17:23:57             🧑  作者: Mango

给定正整数N ,任务是查找可以在给定元素集上形成的不反身关系的数量。由于计数可能非常大,因此将其打印为10 9 + 7模

例子:

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

  • 集合A上的关系R集合笛卡尔积集,即具有N 2个元素的A *A
  • 不自反关系:当且仅当对于A中每个元素x的x R x [(x,x)不属于R]时,集合A上的关系R称为不自反关系。
  • 笛卡尔积中总共有N对(x,x) ,不应包含在非反身关系中。因此,对于其余(N 2 – N)个元素,每个元素都有两个选择,即在子集中包含或排除它。
  • 因此,可能的不反身关系的总数为2 (N2-N)

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
const int mod = 1000000007;
 
// Function to calculate x^y
// modulo 1000000007 in O(log y)
int power(long long x, unsigned int y)
{
    // Stores the result of x^y
    int res = 1;
 
    // Update x if it exceeds mod
    x = x % mod;
 
    // If x is divisible by mod
    if (x == 0)
        return 0;
 
    while (y > 0) {
 
        // If y is odd, then
        // multiply x with result
        if (y & 1)
            res = (res * x) % mod;
 
        // Divide y by 2
        y = y >> 1;
 
        // Update the value of x
        x = (x * x) % mod;
    }
 
    // Return the resultant value of x^y
    return res;
}
 
// Function to count the number of
// irreflixive relations in a set
// consisting of N elements
int irreflexiveRelation(int N)
{
 
    // Return the resultant count
    return power(2, N * N - N);
}
 
// Driver Code
int main()
{
    int N = 2;
    cout << irreflexiveRelation(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
static int mod = 1000000007;
 
// Function to calculate x^y
// modulo 1000000007 in O(log y)
static int power(int x, int y)
{
   
    // Stores the result of x^y
    int res = 1;
 
    // Update x if it exceeds mod
    x = x % mod;
 
    // If x is divisible by mod
    if (x == 0)
        return 0;
 
    while (y > 0)
    {
 
        // If y is odd, then
        // multiply x with result
        if ((y & 1) != 0)
            res = (res * x) % mod;
 
        // Divide y by 2
        y = y >> 1;
 
        // Update the value of x
        x = (x * x) % mod;
    }
 
    // Return the resultant value of x^y
    return res;
}
 
// Function to count the number of
// irreflixive relations in a set
// consisting of N elements
static int irreflexiveRelation(int N)
{
 
    // Return the resultant count
    return power(2, N * N - N);
}
 
 
// Driver Code
public static void main(String[] args)
{
    int N = 2;
    System.out.println(irreflexiveRelation(N));
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
mod = 1000000007
 
# Function to calculate x^y
# modulo 1000000007 in O(log y)
def power(x, y):
    global mod
     
    # Stores the result of x^y
    res = 1
 
    # Update x if it exceeds mod
    x = x % mod
 
    # If x is divisible by mod
    if (x == 0):
        return 0
 
    while (y > 0):
 
        # If y is odd, then
        # multiply x with result
        if (y & 1):
            res = (res * x) % mod
 
        # Divide y by 2
        y = y >> 1
 
        # Update the value of x
        x = (x * x) % mod
 
    # Return the resultant value of x^y
    return res
 
# Function to count the number of
# irreflixive relations in a set
# consisting of N elements
def irreflexiveRelation(N):
 
    # Return the resultant count
    return power(2, N * N - N)
 
# Driver Code
if __name__ == '__main__':
    N = 2
    print(irreflexiveRelation(N))
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for above approach
using System;
 
public class GFG
{
 
  static int mod = 1000000007;
 
  // Function to calculate x^y
  // modulo 1000000007 in O(log y)
  static int power(int x, int y)
  {
 
    // Stores the result of x^y
    int res = 1;
 
    // Update x if it exceeds mod
    x = x % mod;
 
    // If x is divisible by mod
    if (x == 0)
      return 0;
 
    while (y > 0)
    {
 
      // If y is odd, then
      // multiply x with result
      if ((y & 1) != 0)
        res = (res * x) % mod;
 
      // Divide y by 2
      y = y >> 1;
 
      // Update the value of x
      x = (x * x) % mod;
    }
 
    // Return the resultant value of x^y
    return res;
  }
 
  // Function to count the number of
  // irreflixive relations in a set
  // consisting of N elements
  static int irreflexiveRelation(int N)
  {
 
    // Return the resultant count
    return power(2, N * N - N);
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int N = 2;
    Console.WriteLine(irreflexiveRelation(N));
  }
}
 
// This code is contributed by sanjoy_62.


输出:
4

时间复杂度: O(log N)
辅助空间: O(1)