📜  集合上既非自反又反对称的关系数

📅  最后修改于: 2022-05-13 01:56:09.260000             🧑  作者: Mango

集合上既非自反又反对称的关系数

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

例子:

方法:可以根据以下观察解决给定的问题:

  • 集合A上的关系R是集合的笛卡尔积的子集,即具有N 2 个元素的 A * A。
  • 子集中不应包含(x, x)对,以确保关系是不自反的。
  • 对于剩余的(N 2 – N)对,将它们分成(N 2 – N)/2 组,其中每组由一对(x, y)及其对称对(y, x)组成。
  • 现在,有三个选择,要么包括有序对中的一个,要么不包括一组中的任何一个。
  • 因此,反自反和反对称的可能关系的总数由3 (N2 – N)/2给出。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
const int mod = 1000000007;
 
// Function to calculate
// x ^ y % mod 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;
 
    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 value of x^y
    return res;
}
 
// Function to count relations that
// are  irreflexive and antisymmetric
// in a set consisting of N elements
int numberOfRelations(int N)
{
    // Return the resultant count
    return power(3, (N * N - N) / 2);
}
 
// Driver Code
int main()
{
    int N = 2;
    cout << numberOfRelations(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
static int mod = 1000000007;
 
// Function to calculate
// x ^ y % mod in O(log y)
static int power(long x, int y)
{
     
    // Stores the result of x^y
    int res = 1;
 
    // Update x if it exceeds mod
    x = x % mod;
 
    while (y > 0)
    {
 
        // If y is odd, then multiply
        // x with result
        if (y % 2 == 1)
            res = (int)(res  * x) % mod;
 
        // Divide y by 2
        y = y >> 1;
 
        // Update the value of x
        x = (x * x) % mod;
    }
 
    // Return the value of x^y
    return res;
}
 
// Function to count relations that
// are  irreflexive and antisymmetric
// in a set consisting of N elements
static int numberOfRelations(int N)
{
     
    // Return the resultant count
    return power(3, (N * N - N) / 2);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 2;
    System.out.print(numberOfRelations(N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program for the above approach
mod = 1000000007
 
# Function to calculate
# x ^ y % mod in O(log y)
def power(x,  y):
 
    # Stores the result of x^y
    res = 1
 
    # Update x if it exceeds mod
    x = x % mod
 
    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 value of x^y
    return res
 
# Function to count relations that
# are  irreflexive and antisymmetric
# in a set consisting of N elements
def numberOfRelations(N):
 
    # Return the resultant count
    return power(3, (N * N - N) // 2)
 
# Driver Code
if __name__ == "__main__":
 
    N = 2
    print(numberOfRelations(N))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
class GFG{
 
static int mod = 1000000007;
 
// Function to calculate
// x ^ y % mod in O(log y)
static int power(long x, int y)
{
     
    // Stores the result of x^y
    int res = 1;
 
    // Update x if it exceeds mod
    x = x % mod;
 
    while (y > 0)
    {
 
        // If y is odd, then multiply
        // x with result
        if (y % 2 == 1)
            res = (int)(res  * x) % mod;
 
        // Divide y by 2
        y = y >> 1;
 
        // Update the value of x
        x = (x * x) % mod;
    }
 
    // Return the value of x^y
    return res;
}
 
// Function to count relations that
// are  irreflexive and antisymmetric
// in a set consisting of N elements
static int numberOfRelations(int N)
{
     
    // Return the resultant count
    return power(3, (N * N - N) / 2);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 2;
    Console.Write(numberOfRelations(N));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
3

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