反身关系 - 如果对于集合a中的每个'a',(a, a)属于关系r,则称关系r是集合a上的反身关系。例如 -
input : x = 1output : 1explanation : set = { 1 }, reflexive relations on a * a :{ { 1 } }input : x = 2output : 4explanation : set = { 1,2 }, reflexive relations on a * a : { ( 1, 1 ) , ( 2, 2 ) } { ( 1, 1 ), ( 2, 2 ), ( 1, 2 ) } { ( 1, 1 ), ( 2, 2 ), ( 1, 2 ), ( 2, 1 ) } { ( 1, 1 ), ( 2, 2 ), ( 2, 1 ) }
因此,如果对于每个元素a ∈ a,都有(a, a) ∈ r,则关系r是自反的。
解决方案的方法可以通过公式2n2−n来计算元素集上的自反关系的数量。这个通用公式是通过计算整数的自反关系数量得到的。
例子#include <iostream>using namespace std;int countreflexive(int n){ int ans = 1 << (n*n - n); return ans;}int main(){ int n ; cin >> n ; // taking input n from the user using std cin. int result = countreflexive(n); // calling function to calculate number of reflexive relations cout << "number of reflexive relations on set: " << result ; // printing the answer return 0;}
输出number of reflexive relations on set: 1
上述程序的解释这个程序很容易理解,因为我们只是从用户那里获取输入,并将其放入公式2n2−n中,我们使用左移运算符<< 来计算公式,这段代码的时间复杂度是o(1),随着n的大小增加,速度会变慢。
结论在本文中,我们解决了一个关于集合上反身关系数量的问题。我们讨论了解决给定问题的简单方法,数学家们推导出了一个计算反身关系数量的公式。
我们还学习了用c++编写这个问题的程序,其时间复杂度为o(1)。我们可以用其他语言如c、java、python和其他语言编写相同的程序。
以上就是使用c++编写,找到一个集合上的自反关系的数量的详细内容。
