📜  从1到N的整数的所有不相交子集的总数

📅  最后修改于: 2021-04-22 08:29:04             🧑  作者: Mango

给定整数N。考虑前N个自然数A = {1,2,3,…,N}的集合。令M和P为A的两个非空子集。任务是计算(M,P)的无序对的数量,以使M和P为不交集。请注意,M和P的顺序无关紧要。

例子:

方法:

  1. 假设集合{1、2、3、4、5、6}中只有6个元素。
  2. 当您以1作为第一子集的元素之一来计算子集的数量时,得出的结果是211。
  3. 用2作为第一个子集的元素之一来计算子集的数量,得出的结果是65,因为不包括1,因为集的顺序无关紧要。
  4. 以3作为第一个集合的元素之一的子集数得出65,在这里可以观察到一个模式。
  5. 图案:
  6. 扩展到n-> 2(表示元素n-2 + 1 = n-1的数量)
    2 (n-2) * 3 (0) + 2 (n – 3) * 3 1 + 2 (n – 4) * 3 2 + 2 (n – 5) * 3 3 +…+ 2 (0) * 3 (n – 2)
    根据几何级数,a + a * r 0 + a * r 1 +…+ a * r (n – 1) = a *(r n – 1)/(r – 1)
  7. S(n)= 3 (n – 1) – 2 (n – 1) 。请记住,S(n)是子集的个数,其中第一个子集的元素之一为1,但要获得所需的结果,用T(n)= S(1)+ S(2)+ S(3)+…表示。 + S(n)
  8. 它还形成了几何级数,因此我们通过GP的总和公式进行计算
    T(n)=(3 n – 2 n +1 + 1)/ 2
  9. 因为我们需要T(n)%p,其中p = 10 9 + 7
    我们必须使用费马斯的小定理
    a -1 = a (m – 2) (mod m)用于模除

      下面是上述方法的实现:

      C++
      // C++ implementation of the approach
        
      #include 
      using namespace std;
      #define p 1000000007
        
      // Modulo exponentiation function
      long long power(long long x, long long y)
      {
          // Function to calculate (x^y)%p in O(log(y))
          long long res = 1;
          x = x % p;
        
          while (y > 0) {
              if (y & 1)
                  res = (res * x) % p;
              y = y >> 1;
              x = (x * x) % p;
          }
        
          return res % p;
      }
        
      // Driver function
      int main()
      {
          long long n = 3;
        
          // Evaluating ((3^n-2^(n+1)+1)/2)%p
          long long x = (power(3, n) % p + 1) % p;
        
          x = (x - power(2, n + 1) + p) % p;
        
          // From  Fermats’s little theorem
          // a^-1 ? a^(m-2) (mod m)
        
          x = (x * power(2, p - 2)) % p;
          cout << x << "\n";
      }


      Java
      // Java implementation of the approach
      class GFG 
      { 
      static int p = 1000000007;
        
      // Modulo exponentiation function
      static long power(long x, long y)
      {
          // Function to calculate (x^y)%p in O(log(y))
          long res = 1;
          x = x % p;
        
          while (y > 0)
          {
              if (y % 2 == 1)
                  res = (res * x) % p;
              y = y >> 1;
              x = (x * x) % p;
          }
          return res % p;
      }
        
      // Driver Code
      public static void main(String[] args) 
      {
          long n = 3;
        
          // Evaluating ((3^n-2^(n+1)+1)/2)%p
          long x = (power(3, n) % p + 1) % p;
        
          x = (x - power(2, n + 1) + p) % p;
        
          // From Fermats's little theorem
          // a^-1 ? a^(m-2) (mod m)
        
          x = (x * power(2, p - 2)) % p;
          System.out.println(x);
      }
      }
        
      // This code is contributed by Rajput-Ji


      Python3
      # Python3 implementation of the approach
      p = 1000000007
        
      # Modulo exponentiation function
      def power(x, y):
            
          # Function to calculate (x^y)%p in O(log(y))
          res = 1
          x = x % p
        
          while (y > 0):
              if (y & 1):
                  res = (res * x) % p;
              y = y >> 1
              x = (x * x) % p
        
          return res % p
        
      # Driver Code
      n = 3
        
      # Evaluating ((3^n-2^(n+1)+1)/2)%p
      x = (power(3, n) % p + 1) % p
        
      x = (x - power(2, n + 1) + p) % p
        
      # From Fermats’s little theorem
      # a^-1 ? a^(m-2) (mod m)
      x = (x * power(2, p - 2)) % p
        
      print(x)
        
      # This code is contributed by Mohit Kumar


      C#
      // C# implementation of the approach
      using System;
        
      class GFG
      {
      static int p = 1000000007;
        
      // Modulo exponentiation function
      static long power(long x, long y)
      {
          // Function to calculate (x^y)%p in O(log(y))
          long res = 1;
          x = x % p;
        
          while (y > 0)
          {
              if (y % 2 == 1)
                  res = (res * x) % p;
              y = y >> 1;
              x = (x * x) % p;
          }
          return res % p;
      }
        
      // Driver Code
      static public void Main ()
      {
          long n = 3;
            
          // Evaluating ((3^n-2^(n+1)+1)/2)%p
          long x = (power(3, n) % p + 1) % p;
            
          x = (x - power(2, n + 1) + p) % p;
            
          // From Fermats's little theorem
          // a^-1 ? a^(m-2) (mod m)
            
          x = (x * power(2, p - 2)) % p;
          Console.Write(x);
      }
      }
        
      // This code is contributed by ajit.


      输出:
      6