给定正整数N ,任务是在给定的N个元素集合上找到反对称关系的数量。由于关系的数量可能非常大,因此请以10 9 +7为模数进行打印。
A relation R on a set A is called Antisymmetric if and only if (a, b) € R and (b, a) € R, then a = b is called antisymmetric, i.e., the relation R = {(a, b)→ R | a ≤ b} is anti-symmetric, since a ≤ b and b ≤ a implies a = b.
例子:
Input: N = 2
Output: 12
Explanation: Considering the set {a, b}, all possible antisymmetric relations are:
{}, {(a, b)}, {(b, a)}, {(a, a)}, {(a, a), (a, b)}, {(a, a), (b, a)}, {(b, b)}, {(b, b), (a, b)}, {(b, b), (b, a)}, {(a, a), (b, b)}, {(a, a), (b, b), (a, b)}, {(a, a), (b, b), (b, a)}.
Input: N = 5
Output: 1889568
方法:可以根据以下观察结果解决给定问题:
- 考虑对集合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;
}
Java
// Java program for the above approach
import java.util.*;
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(String []args)
{
int N = 2;
System.out.print(antisymmetricRelation(N));
}
}
// This code is contributed by ipg2016107
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
Javascript
12
时间复杂度: O(log N)
辅助空间: O(1)