📌  相关文章
📜  Q的可能值,使得对于R的任何值,其乘积等于其乘积的X倍

📅  最后修改于: 2021-04-22 06:55:19             🧑  作者: Mango

给定一个整数X ,任务是找到该对(Q,R)Q的可能值,以使它们的乘积等于X乘以它们的总和,其中Q≤R且X <10 7 。打印这些Q值的总数以及值。

例子:

方法:想法是理解问题以形成一个方程,即(Q x R)= X(Q + R)。

  • 这个想法是从1迭代到X并检查是否每个if ((((X + i)* X)%i)== 0
  • 初始化结果向量,并从1迭代X的所有值。
  • 检查以上条件是否成立。如果确实存在,则将值X + i推入向量中。
  • 让我们打破等式,以便更清楚地理解它,
  • 因此,观察到(X + i)是Q的可能值,而(X + i)* X是R的可能值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible values of Q
void values_of_Q(int X)
{
    // Vector initialization
    // to store all numbers
    // satisfying the given condition
    vector val_Q;
 
    // Iterate for all the values of X
    for (int i = 1; i <= X; i++) {
 
        // Check if condition satisfied
        // then push the number
        if ((((X + i) * X)) % i == 0) {
 
            // Possible value of Q
            val_Q.push_back(X + i);
        }
    }
 
    cout << val_Q.size() << endl;
 
    // Print all the numbers
    for (int i = 0; i < val_Q.size(); i++) {
        cout << val_Q[i] << " ";
    }
}
 
// Driver code
int main()
{
    int X = 3;
 
    values_of_Q(X);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
   
// Function to find all possible values of Q
static void values_of_Q(int X)
{
    // Vector initialization
    // to store all numbers
    // satisfying the given condition
    ArrayList val_Q = new ArrayList();
  
    // Iterate for all the values of X
    for (int i = 1; i <= X; i++)
    {
  
        // Check if condition satisfied
        // then push the number
        if ((((X + i) * X)) % i == 0)
        {
  
            // Possible value of Q
            val_Q.add(X + i);
        }
    }
  
    System.out.println(val_Q.size());
  
    // Print all the numbers
    for (int i = 0; i < val_Q.size(); i++)
    {
        System.out.print(val_Q.get(i)+" ");
    }
}
  
// Driver code
public static void main(String[] args)
{
    int X = 3;
  
    values_of_Q(X);
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 program for the above approach
 
# Function to find all possible values of Q
def values_of_Q(X):
 
    # Vector initialization
    # to store all numbers
    # satisfying the given condition
    val_Q = []
 
    # Iterate for all the values of X
    for i in range(1, X + 1):
 
        # Check if condition satisfied
        # then push the number
        if ((((X + i) * X)) % i == 0):
 
            # Possible value of Q
            val_Q.append(X + i)
 
    print(len(val_Q))
 
    # Print all the numbers
    for i in range(len(val_Q)):
        print(val_Q[i], end = " ")
 
# Driver Code       
X = 3
 
values_of_Q(X)
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find all possible
// values of Q
static void values_of_Q(int X)
{
     
    // List initialization
    // to store all numbers
    // satisfying the given condition
    List val_Q = new List();
 
    // Iterate for all the values of X
    for(int i = 1; i <= X; i++)
    {
         
        // Check if condition satisfied
        // then push the number
        if ((((X + i) * X)) % i == 0)
        {
             
            // Possible value of Q
            val_Q.Add(X + i);
        }
    }
     
    Console.WriteLine(val_Q.Count);
 
    // Print all the numbers
    for(int i = 0; i < val_Q.Count; i++)
    {
        Console.Write(val_Q[i] + " ");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int X = 3;
 
    values_of_Q(X);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
2
4 6

时间复杂度: O(N)

辅助空间: O(X)