Дано ціле число N. Знайдіть і покажіть кількість пар, які задовольняють такі умови:
- Квадрат відстані між цими двома числами дорівнює LCM цих двох чисел.
- The НОД цих двох чисел дорівнює добутку двох послідовних цілих чисел.
- Обидва числа в парі мають бути меншими або дорівнювати 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)
Пояснення:
Наведені нижче таблиці дадуть чітке уявлення про те, що можна знайти:

що робить ravel у python
У наведених вище таблицях показано НОД, утворений добутком двох послідовних чисел і відповідних кратних, у яких існує УНІКАЛЬНА ПАРА, що відповідає кожному значенню. Зелені записи в кожному рядку утворюють унікальну пару для відповідного НОД.
Примітка: У наведених вище табл
- Для 1-го запису GCD=2 1-й і 2-й кратні 2 утворюють унікальну пару (2 4)
- Так само для 2-го запису НОД=6 2-е та 3-е кратне 6 утворюють унікальну пару (12 18)
- Подібним чином рухаючись далі для Z-го запису, тобто для НОД = Z*(Z+1), зрозуміло, що унікальна пара складатиметься з Z-го та (Z+1)-го кратного НОД = Z*(Z+1). Тепер Z-е кратне НОД дорівнює Z * (Z*(Z+1)), а (Z+1)-е кратне НОД буде (Z + 1) * (Z*(Z+1)).
- І оскільки обмеження дорівнює N, тому друге число в унікальній парі має бути меншим або дорівнювати N. Отже (Z + 1) * (Z*(Z+1))<= N. Simplifying it further the desired relation is derived Z3+ (2*Z2) + Z<=N
Це формує закономірність, і з математичних розрахунків виходить, що для даного N загальна кількість таких унікальних пар (скажімо, Z) відповідатиме математичному співвідношенню, наведеному нижче:
Z3 + (2*Z2) + 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)
Часова складність : O(N1/3)
Допоміжне приміщення : O(1)