各要素が N 以下となる一意のペアを検索します。

各要素が N 以下となる一意のペアを検索します。

整数 N が与えられると、次の条件を満たすペアの数を見つけて表示します。

  • これら 2 つの数値間の距離の 2 乗は、 LCM これら 2 つの数字のうち。
  • の GCD これら 2 つの数値のうちの 2 つは、連続する 2 つの整数の積に等しくなります。
  • ペアの両方の数値は N 以下である必要があります。

注記: 上記の両方の条件を同時に満たすペアのみが表示され、それらの数は N 以下である必要があります。

例:   

  Input:   10   Output:   No. of pairs = 1 Pair no. 1 --> (2 4)   Input:   500   Output:   No. of pairs = 7 Pair no. 1 --> (2 4) Pair no. 2 --> (12 18) Pair no. 3 --> (36 48) Pair no. 4 --> (80 100) Pair no. 5 --> (150 180) Pair no. 6 --> (252 294) Pair no. 7 --> (392 448) 

説明:
以下に示す表は、何が見つかるかを明確に示しています。  

各要素が N 以下となる一意のペアを検索します。

上の表は、2 つの連続する数値とその対応する倍数の積によって形成される GCD を示しており、各値に対応する UNIQUE PAIR が存在します。各行の緑色のエントリは、対応する GCD の一意のペアを形成します。
注記: 上の表では  

  1. 1 番目のエントリ GCD=2 の場合、2 の 1 番目と 2 番目の倍数が一意のペア (2 4) を形成します。
  2. 同様に、2 番目のエントリ GCD=6 についても、6 の 2 番目と 3 番目の倍数が一意のペア (12 18) を形成します。
  3. 同様に Z 番目のエントリ、つまり GCD = Z*(Z+1) について進むと、一意のペアが GCD = Z*(Z+1) の Z 番目と (Z+1) 番目の倍数で構成されることは明らかです。ここで、GCD の Z 番目の倍数は Z * (Z*(Z+1)) となり、GCD の (Z+1) 番目の倍数は (Z + 1) * (Z*(Z+1)) となります。
  4. また、制限は N であるため、一意のペアの 2 番目の数値は N 以下である必要があります。つまり、(Z + 1) * (Z*(Z+1)) <= N. Simplifying it further the desired relation is derived Z 3 + (2*Z 2 ) + Z <=N

これはパターンを形成し、数学的計算から、特定の N に対して、そのような一意のペア (たとえば Z) の総数は、以下に示す数学的関係に従うことが導かれます。 

Z 3  + (2*Z 2 ) + Z  <= N 


必要な実装は次のとおりです。  

C
   // C program for finding the required pairs   #include         #include         // Finding the number of unique pairs   int     No_Of_Pairs  (  int     N  )   {      int     i     =     1  ;      // Using the derived formula      while     ((  i     *     i     *     i  )     +     (  2     *     i     *     i  )     +     i      <=     N  )      i  ++  ;      return     (  i     -     1  );   }   // Printing the unique pairs   void     print_pairs  (  int     pairs  )   {      int     i     =     1       mul  ;      for     (  i     =     1  ;     i      <=     pairs  ;     i  ++  )     {      mul     =     i     *     (  i     +     1  );      printf  (  'Pair no. %d --> (%d %d)  n  '        i       (  mul     *     i  )     mul     *     (  i     +     1  ));      }   }   // Driver program to test above functions   int     main  ()   {      int     N     =     500       pairs       mul       i     =     1  ;      pairs     =     No_Of_Pairs  (  N  );      printf  (  'No. of pairs = %d   n  '       pairs  );      print_pairs  (  pairs  );      return     0  ;   }   
Java
   // Java program for finding   // the required pairs   import     java.io.*  ;   class   GFG      {          // Finding the number      // of unique pairs      static     int     No_Of_Pairs  (  int     N  )      {      int     i     =     1  ;          // Using the derived formula      while     ((  i     *     i     *     i  )     +         (  2     *     i     *     i  )     +     i      <=     N  )      i  ++  ;          return     (  i     -     1  );      }          // Printing the unique pairs      static     void     print_pairs  (  int     pairs  )      {      int     i     =     1       mul  ;      for     (  i     =     1  ;     i      <=     pairs  ;     i  ++  )      {      mul     =     i     *     (  i     +     1  );      System  .  out  .  println  (  'Pair no. '     +     i     +     ' --> ('     +         (  mul     *     i  )     +     ' '     +         mul     *     (  i     +     1  )     +     ')'  );         }      }          // Driver code      public     static     void     main     (  String  []     args  )      {      int     N     =     500       pairs       mul       i     =     1  ;      pairs     =     No_Of_Pairs  (  N  );          System  .  out  .  println  (  'No. of pairs = '     +     pairs  );      print_pairs  (  pairs  );      }   }   // This code is contributed by Mahadev.   
Python3
   # Python3 program for finding the required pairs   # Finding the number of unique pairs   def   No_Of_Pairs  (  N  ):   i   =   1  ;   # Using the derived formula   while   ((  i   *   i   *   i  )   +   (  2   *   i   *   i  )   +   i    <=   N  ):   i   +=   1  ;   return   (  i   -   1  );   # Printing the unique pairs   def   print_pairs  (  pairs  ):   i   =   1  ;   mul   =   0  ;   for   i   in   range  (  1     pairs   +   1  ):   mul   =   i   *   (  i   +   1  );   print  (  'Pair no.'      i     ' --> ('     (  mul   *   i  )   ' '     mul   *   (  i   +   1  )   ')'  );   # Driver Code   N   =   500  ;   i   =   1  ;   pairs   =   No_Of_Pairs  (  N  );   print  (  'No. of pairs = '     pairs  );   print_pairs  (  pairs  );   # This code is contributed   # by mits   
C#
   // C# program for finding   // the required pairs   using     System  ;   class     GFG      {       // Finding the number   // of unique pairs   static     int     No_Of_Pairs  (  int     N  )   {      int     i     =     1  ;      // Using the derived formula      while     ((  i     *     i     *     i  )     +         (  2     *     i     *     i  )     +     i      <=     N  )      i  ++  ;      return     (  i     -     1  );   }   // Printing the unique pairs   static     void     print_pairs  (  int     pairs  )   {      int     i     =     1       mul  ;      for     (  i     =     1  ;     i      <=     pairs  ;     i  ++  )      {      mul     =     i     *     (  i     +     1  );      Console  .  WriteLine  (  'Pair no. '     +     i     +     ' --> ('     +         (  mul     *     i  )     +     ' '     +         mul     *     (  i     +     1  )     +     ')'  );         }   }   // Driver code   static     void     Main  ()   {      int     N     =     500       pairs  ;      pairs     =     No_Of_Pairs  (  N  );      Console  .  WriteLine  (  'No. of pairs = '     +         pairs  );      print_pairs  (  pairs  );   }   }   // This code is contributed by mits   
PHP
      // PHP program for finding    // the required pairs   // Finding the number    // of unique pairs   function   No_Of_Pairs  (  $N  )   {   $i   =   1  ;   // Using the    // derived formula   while   ((  $i   *   $i   *   $i  )   +   (  2   *   $i   *   $i  )   +   $i    <=   $N  )   $i  ++  ;   return   (  $i   -   1  );   }   // Printing the unique pairs   function   print_pairs  (  $pairs  )   {   $i   =   1  ;   $mul  ;   for   (  $i   =   1  ;   $i    <=   $pairs  ;   $i  ++  )   {   $mul   =   $i   *   (  $i   +   1  );   echo   'Pair no.'      $i     ' --> ('      (  $mul   *   $i  )   ' '     $mul   *   (  $i   +   1  )  ')   n  '  ;   }   }   // Driver Code   $N   =   500  ;   $pairs  ;   $mul  ;   $i   =   1  ;   $pairs   =   No_Of_Pairs  (  $N  );   echo   'No. of pairs = '     $pairs      '   n  '  ;   print_pairs  (  $pairs  );   // This code is contributed   // by Akanksha Rai(Abby_akku)   ?>   
JavaScript
    <  script  >   // Javascript program for finding the    // required pairs   // Finding the number of unique pairs   function     No_Of_Pairs  (  N  )   {      let     i     =     1  ;      // Using the derived formula      while     ((  i     *     i     *     i  )     +      (  2     *     i     *     i  )     +     i      <=     N  )      i  ++  ;      return     (  i     -     1  );   }   // Printing the unique pairs   function     print_pairs  (  pairs  )   {      let     i     =     1       mul  ;      for  (  i     =     1  ;     i      <=     pairs  ;     i  ++  )         {      mul     =     i     *     (  i     +     1  );      document  .  write  (  'Pair no. '     +     i     +         ' --> ('     +     (  mul     *     i  )     +      ' '     +     mul     *     (  i     +     1  )     +         ')  
'
); } } // Driver code let N = 500 pairs mul i = 1 ; pairs = No_Of_Pairs ( N ); document . write ( 'No. of pairs = ' + pairs + '
'
); print_pairs ( pairs ); // This code is contributed by mohit kumar 29 < /script>
C++14
   // C++ code for the above approach:   #include          using     namespace     std  ;   // Finding the number of unique pairs   int     No_Of_Pairs  (  int     N  )   {      int     i     =     1  ;      // Using the derived formula      while     ((  i     *     i     *     i  )     +     (  2     *     i     *     i  )     +     i      <=     N  )      i  ++  ;      return     (  i     -     1  );   }   // Printing the unique pairs   void     print_pairs  (  int     pairs  )   {      int     i     =     1       mul  ;      for     (  i     =     1  ;     i      <=     pairs  ;     i  ++  )     {      mul     =     i     *     (  i     +     1  );      cout      < <     'Pair no. '   < <     i      < <  ' --> ('      < <     (  mul     *     i  )      < <     ' '   < <     mul     *     (  i     +     1  )      < <     ')'      < <  endl  ;;      }   }   // Driver Code   int     main  ()   {      int     N     =     500       pairs       mul       i     =     1  ;      pairs     =     No_Of_Pairs  (  N  );      cout      < <     'No. of pairs = '      < <     pairs      < <     endl  ;      print_pairs  (  pairs  );      return     0  ;   }   

出力:  
No. of pairs = 7 Pair no. 1 --> (2 4) Pair no. 2 --> (12 18) Pair no. 3 --> (36 48) Pair no. 4 --> (80 100) Pair no. 5 --> (150 180) Pair no. 6 --> (252 294) Pair no. 7 --> (392 448) 

 

時間計算量 : の上 1/3
補助スペース :O(1)