集合上既不是自反也不是自反的关系数
给定一个正整数N ,任务是在一组前N个自然数上找到既不是自反也不是反自反的关系的数量。由于关系的数量可能非常大,因此将其打印为模 10 9 + 7。
A relation R on a set A is called reflexive, if no (a, a) € R holds for every element a € A.
For Example: If set A = {a, b} then R = {(a, b), (b, a)} is irreflexive relation.
例子:
Input: N = 2
Output: 8
Explanation: Considering the set {1, 2}, the total possible relations that are neither reflexive nor irreflexive are:
- {(1, 1)}
- {(1, 1), (1, 2)}
- {(1, 1), (2, 1)}
- {(1, 1), (1, 2), (2, 1)}
- {(2, 2)}
- {(2, 2), (2, 1)}
- {(2, 2), (1, 2)}
- {(2, 2), (2, 1), (1, 2)}
Therefore, the total count is 8.
Input: N = 3
Output: 384
方法:可以根据以下观察解决给定的问题:
- 集合A上的关系R是笛卡尔积的子集 一个集合,即A * A与N 2 个元素。
- 如果关系不包含至少一对(x, x) ,则关系将是非自反的,如果它包含至少一对(x, x)其中x € R ,则关系将是非自反的。
- 可以得出结论,如果该关系包含至少一对(x, x)并且最多(N – 1)对(x, x) ,则该关系将是非自反和非自反的。
- 在N对(x, x)中,选择除0和N – 1之外的任意对的可能性总数为(2 N – 2) 。对于剩余的(N 2 – N) 个元素,每个元素都有两个选择,即在子集中包含或排除它。
从上面的观察,在一组前N个自然数上既不是自反也不是反自反的关系总数 是(谁)给的 .
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int mod = 1000000007;
// Function to calculate x^y
// modulo 10^9 + 7 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;
// If x is divisible by mod
if (x == 0)
return 0;
while (y > 0) {
// If y is odd, then
// multiply x with res
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 the number
// of relations that are neither
// reflexive nor irreflexive
void countRelations(int N)
{
// Return the resultant count
cout << (power(2, N) - 2)
* power(2, N * N - N);
}
// Driver Code
int main()
{
int N = 2;
countRelations(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int mod = 1000000007;
// Function to calculate x^y
// modulo 10^9 + 7 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;
// If x is divisible by mod
if (x == 0)
return 0;
while (y > 0)
{
// If y is odd, then
// multiply x with res
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 value of x^y
return res;
}
// Function to count the number
// of relations that are neither
// reflexive nor irreflexive
static void countRelations(int N)
{
// Return the resultant count
System.out.print((power(2, N) - 2) *
power(2, N * N - N));
}
// Driver Code
public static void main(String[] args)
{
int N = 2;
countRelations(N);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python program for the above approach
mod = 1000000007
# Function to calculate x^y
# modulo 10^9 + 7 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
# If x is divisible by mod
if(x == 0):
return 0
while(y > 0):
# If y is odd, then
# multiply x with res
if (y % 2 == 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 the number
# of relations that are neither
# reflexive nor irreflexive
def countRelations(N):
# Return the resultant count
print((power(2, N) - 2) * power(2, N * N - N))
# Driver Code
N = 2
countRelations(N)
# This code is contributed by abhinavjain194
C#
// C# program for the above approach
using System;
class GFG{
static int mod = 1000000007;
// Function to calculate x^y
// modulo 10^9 + 7 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;
// If x is divisible by mod
if (x == 0)
return 0;
while (y > 0)
{
// If y is odd, then
// multiply x with res
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 value of x^y
return res;
}
// Function to count the number
// of relations that are neither
// reflexive nor irreflexive
static void countRelations(int N)
{
// Return the resultant count
Console.Write((power(2, N) - 2) *
power(2, N * N - N));
}
// Driver Code
public static void Main(String[] args)
{
int N = 2;
countRelations(N);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
8
时间复杂度: O(log N)
辅助空间: O(1)