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

📅  最后修改于: 2021-04-17 17:33:44             🧑  作者: Mango

给定正整数N ,任务是找到一组N个元素的非对称关系数。由于关系的数量可能非常多,因此请以10 9 +7为模数打印。

例子:

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

  • 集合A上的关系R是集合笛卡尔积的子集,即具有N 2个元素的A *A。
  • 笛卡尔积中总共存在N(x,x)类型,其中(x,x)中的任何一个都不应该包含在子集中。
  • 现在,剩下的就是笛卡尔积的(N 2 – N)个元素。
  • 为了满足非对称关系的性质,有三种可能性,要么仅包括类型(x,y),要么仅包括类型(y,x),或者不包括单个组中的子集。
  • 因此,可能的不对称关系的总数等于3 (N2-N)/ 2

因此,其想法是打印3 (N2-N)/ 2模10 9 + 7的结果。

下面是上述方法的实现:

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


Python3
# Python3 program for the above approach
mod = 1000000007
  
# Function to calculate
# x^y modulo (10^9 + 7)
def power(x, y):
      
    # 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 final
    # value of x ^ y
    return res
  
# Function to count the number of
# asymmetric relations in a set
# consisting of N elements
def asymmetricRelation(N):
      
    # Return the resultant count
    return power(3, (N * N - N) // 2)
  
# Driver Code
if __name__ == '__main__':
      
    N = 2
      
    print(asymmetricRelation(N))
  
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
  
class GFG{
      
const int mod = 1000000007;
  
// Function to calculate
// x^y modulo (10^9 + 7)
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 final
    // value of x ^ y
    return res;
}
  
// Function to count the number of
// asymmetric relations in a set
// consisting of N elements
static int asymmetricRelation(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.WriteLine(asymmetricRelation(N));
}
}
  
// This code is contributed by ukasp


输出:
3

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