📜  一组对称关系的数量

📅  最后修改于: 2021-04-26 07:02:55             🧑  作者: Mango

给定数字n,找出前n个自然数{1,2,..n}集合上的对称关系数。
例子:

Input  : n = 2
Output : 8
Given set is {1, 2}. Below are all symmetric relation.
{}
{(1, 1)}, 
{(2, 2)},
{(1, 1), (2, 2)}, 
{(1, 2), (2, 1)} 
{(1, 1), (1, 2), (2, 1)},
{(2, 2), (1, 2), (2, 1)}, 
{(1, 1), (1, 2), (2, 1), (1, 2)} 

Input  : n = 3
Output : 64

如果xRy则yRx对每个x,y∈A,则说说集合A的关系’R’是对称的
或者如果(x,y)∈R,那么每x,y(y,x)∈R

C++
// C++ program to count total symmetric relations
// on a set of natural numbers.
#include 
 
// function find the square of n
unsigned int countSymmetric(unsigned int n)
{
    // Base case
    if (n == 0)
        return 1;
 
   // Return 2^(n(n + 1)/2)
   return 1 << ((n * (n + 1))/2);
}
 
// Driver code
int main()
{
    unsigned int n = 3;
 
    printf("%u", countSymmetric(n));
    return 0;
}


Java
// Java program to count total symmetric
// relations on a set of natural numbers.
import java.io.*;
import java.util.*;
 
class GFG {
 
    // function find the square of n
    static int countSymmetric(int n)
    {
        // Base case
        if (n == 0)
            return 1;
     
    // Return 2^(n(n + 1)/2)
    return 1 << ((n * (n + 1)) / 2);
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.println(countSymmetric(n));
    }
}
 
 
// This code is contributed by Nikita Tiwari.


Python3
# Python 3 program to count
# total symmetric relations
# on a set of natural numbers.
 
# function find the square of n
def countSymmetric(n) :
    # Base case
    if (n == 0) :
        return 1
  
    # Return 2^(n(n + 1)/2)
    return (1 << ((n * (n + 1))//2))
 
  
# Driver code
 
n = 3
print(countSymmetric(n))
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to count total symmetric
// relations on a set of natural numbers.
using System;
 
class GFG {
 
    // function find the square of n
    static int countSymmetric(int n)
    {
        // Base case
        if (n == 0)
            return 1;
     
    // Return 2^(n(n + 1)/2)
    return 1 << ((n * (n + 1)) / 2);
    }
 
    // Driver code
    public static void Main ()
    {
        int n = 3;
        Console.WriteLine(countSymmetric(n));
    }
}
 
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

64