Пронађите јединствене парове тако да је сваки елемент мањи или једнак Н
За цео број Н пронађите и покажите број парова који задовољава следеће услове:
- Квадрат удаљености између та два броја једнак је ЛЦМ од та два броја.
- Тхе ГЦД та два броја једнак је производу два узастопна цела броја.
- Оба броја у пару треба да буду мања или једнака Н.
НАПОМЕНА: Треба приказати само оне парове који истовремено прате оба горња услова и ти бројеви морају бити мањи или једнаки Н.
Примери:
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)
Објашњење:
Доле приказане табеле ће дати јасан увид у оно што се може наћи:
Горе наведене табеле показују ГЦД формиран производом два узастопна броја и одговарајућих вишекратника у којима постоји ЈЕДИНСТВЕН ПАР који одговара свакој вредности. Зелени уноси у сваком реду формирају јединствени пар за одговарајући ГЦД.
Напомена: У горњим табелама
- За 1. унос ГЦД=2 1. и 2. вишекратник од 2 чине јединствени пар (2 4)
- Слично за 2. унос ГЦД=6 2. и 3. вишекратник од 6 чине јединствени пар (12 18)
- На сличан начин идемо даље за Зтх унос, тј. за ГЦД = З*(З+1) јасно је да ће се јединствени пар састојати од Зтх и (З+1)-тог вишекратника ГЦД = З*(З+1). Сада је З-ти вишекратник ГЦД-а З * (З*(З+1)) и (З+1)-ти вишекратник ГЦД-а ће бити (З + 1) * (З*(З+1)).
- А пошто је граница Н, други број у јединственом пару мора бити мањи или једнак Н. Дакле (З + 1) * (З*(З+1)) <= N. Simplifying it further the desired relation is derived Z 3 + (2*З 2 ) + З <=N
Ово формира образац и из математичког прорачуна се изводи да ће за дато Н укупан број таквих јединствених парова (рецимо З) пратити математичку релацију приказану у наставку:
Z 3 + (2*Z 2 ) + Z <= N
Испод је потребна имплементација:
// 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 )
Помоћни простор : О(1)