📜  一组N个元素上的反对称关系数

📅  最后修改于: 2021-04-18 02:32:36             🧑  作者: Mango

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

例子:

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

  • 考虑对集合S的反对称关系R,a,b∈A 如果a b,关系R不能同时包含(a,b)(b,a) 。它可能包含有序对之一,也可能都不包含。
  • 所有对都有3种可能的选择。
  • 因此,这些选择的所有组合的计数等于3 (N *(N – 1))/ 2
  • (a,a)形式的对的子集数等于2 N。

因此,可能的反对称关系的总数等于2 N * 3 (N *(N – 1))/ 2

下面是上述方法的实现:

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


Python3
# Python3 program for the above approach
mod = 1000000007
 
# Function to calculate the
# value of 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 resultant
    # value of x^y
    return res
 
# Function to count the number of
# antisymmetric relations in a set
# consisting of N elements
def antisymmetricRelation(N):
 
    # Print the count of
    # antisymmetric relations
    return (power(2, N) *
            power(3, (N * N - N) // 2)) % mod
 
# Driver Code
if __name__ == "__main__":
 
    N = 2
     
    print(antisymmetricRelation(N))
 
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static int mod = 1000000007;
 
// Function to calculate the
// value of x ^ y % mod 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;
 
    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
// antisymmetric relations in a set
// consisting of N elements
static int antisymmetricRelation(int N)
{
 
    // Print the count of
    // antisymmetric relations
    return (power(2, N) *
            power(3, (N * N - N) / 2)) % mod;
}
 
// Driver Code
public static void Main()
{
    int N = 2;
     
    Console.Write(antisymmetricRelation(N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


输出:
12

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