📜  [P, Q] 范围内的对数为 R 的倍数,它们的乘积在 [P*Q/4, P*Q] 范围内

📅  最后修改于: 2022-05-13 01:56:07.288000             🧑  作者: Mango

[P, Q] 范围内的对数为 R 的倍数,它们的乘积在 [P*Q/4, P*Q] 范围内

给定3 个正整数PQR ,任务是找到对的数量,使得两个元素都在[P, Q]范围内,并且数字应该是R的倍数,并且数字的乘积应该位于在[P × Q / 4, P × Q]范围内。如果不存在这样的对,则打印-1。

例子:

方法:为了解决这个问题,首先找到可以存在的对的最小和最大范围,然后找到满足上述条件的对。请按照以下步骤解决问题:

  • 初始化向量说, v来存储所有在[P, Q]范围内并且是R的倍数的数和一个对向量,比如ans来存储满足上述条件的对。
  • 使用变量i[P, Q]范围内迭代并检查i是否可被R整除,然后将i插入向量v中。
  • 使用变量i[0, v.size()-1]范围内迭代并执行以下步骤:
    • 使用变量j[i+1, v.size()-1]范围内迭代并检查是否v[j] * v[i] <= P * Qv[j] * v[i] >= P * Q/4然后将该对插入ans中。
  • 如果ans.size()等于0 ,则打印-1。
  • 否则,打印ans中的对。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number of
// pairs such that both the elements
// are in the range [P, Q] and the
// numbers should be multiple of R,
// and the product of numbers should
// lie in the range [P*Q/4, P*Q]
void findPairs(int p, int q, int r)
{
    // Store multiple of r
    // in range of [P, Q]
    vector v;
 
    // Iterate in the range [p, q]
    for (int i = p; i <= q; i++) {
        if (i % r == 0) {
            v.push_back(i);
        }
    }
 
    // Vector to store pair of answer
    vector > ans;
 
    // Iterate through the vector v
    for (int i = 0; i < v.size(); i++) {
 
        // Iterate in the range [i+1, v.size()-1]
        for (int j = i + 1; j < v.size(); j++) {
 
            // If pair follow this condition
            // insert the pair in vector ans
            if (v[i] * v[j] >= p * q / 4
                && v[i] * v[j] <= p * q) {
                ans.push_back({ v[i], v[j] });
            }
        }
    }
 
    // If no pair satisfy the conditions, print -1
    if (ans.size() == 0) {
        cout << -1 << endl;
    }
    else {
 
        // Print the pairs
        // which satisfy the given condition
        for (int i = 0; i < ans.size(); i++) {
 
            cout << ans[i].first << " "
                 << ans[i].second << endl;
        }
    }
}
 
// Driver Code
int main()
{
 
    // Given Input
    int p = 14, q = 30, r = 5;
 
    // Function Call
    findPairs(p, q, r);
    return 0;
}


Java
//Java program for above approach
import java.awt.*;
import java.util.*;
class GFG{
    static class pair< T, V>{
        T first;
        V second;
    }
 
    // Function to find the number of
    // pairs such that both the elements
    // are in the range [P, Q] and the
    // numbers should be multiple of R,
    // and the product of numbers should
    // lie in the range [P*Q/4, P*Q]
    static void findPairs(int p, int q, int r)
    {
       
        // Store multiple of r
        // in range of [P, Q]
        ArrayList v = new ArrayList<>();
 
        // Iterate in the range [p, q]
        for (int i = p; i <= q; i++) {
            if (i % r == 0) {
                v.add(i);
            }
        }
 
        // Vector to store pair of answer
        ArrayList > ans = new ArrayList<>();
 
        // Iterate through the vector v
        for (int i = 0; i < v.size(); i++) {
 
            // Iterate in the range [i+1, v.size()-1]
            for (int j = i + 1; j < v.size(); j++) {
 
                // If pair follow this condition
                // insert the pair in vector ans
                if (v.get(i) * v.get(j) >= p * q / 4
                        && v.get(i) * v.get(j) <= p * q) {
                    pair x = new pair<>();
                    x.first = v.get(i);
                    x.second = v.get(j);
                    ans.add(x);
                }
            }
        }
 
        // If no pair satisfy the conditions, print -1
        if (ans.size() == 0) {
            System.out.println(-1);
        }
        else {
 
            // Print the pairs
            // which satisfy the given condition
            for (int i = 0; i < ans.size(); i++) {
                System.out.println(ans.get(i).first +
                        " " + ans.get(i).second);
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Given Input
        int p = 14, q = 30, r = 5;
 
        // Function Call
        findPairs(p, q, r);
    }
}
 
// This code is contributed by hritikrommie.


Python3
# Python3 program for the above approach
 
# Function to find the number of
# pairs such that both the elements
# are in the range [P, Q] and the
# numbers should be multiple of R,
# and the product of numbers should
# lie in the range [P*Q/4, P*Q]
def findPairs(p, q, r):
     
    # Store multiple of r
    # in range of [P, Q]
    v = []
 
    # Iterate in the range [p, q]
    for i in range(p, q + 1):
        if (i % r == 0):
            v.append(i)
 
    # Vector to store pair of answer
    ans = []
  
    # Iterate through the vector v
    for i in range(len(v)):
         
        # Iterate in the range [i+1, v.size()-1]
        for j in range(i + 1, len(v)):
             
            # If pair follow this condition
            # insert the pair in vector ans
            if (v[i] * v[j] >= p * q // 4 and
                v[i] * v[j] <= p * q):
                ans.append([v[i], v[j]])
 
    # If no pair satisfy the conditions, pr-1
    if (len(ans) == 0):
        print (-1)
    else:
         
        # Print the pairs
        # which satisfy the given condition
        for i in range(len(ans)):
            print(ans[i][0], ans[i][1])
 
# Driver Code
if __name__ == '__main__':
 
    # Given Input
    p = 14
    q = 30
    r = 5
 
    # Function Call
    findPairs(p, q, r)
     
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the number of
// pairs such that both the elements
// are in the range [P, Q] and the
// numbers should be multiple of R,
// and the product of numbers should
// lie in the range [P*Q/4, P*Q]
static void findPairs(int p, int q, int r)
{
    // Store multiple of r
    // in range of [P, Q]
    List v = new List();
 
    // Iterate in the range [p, q]
    for (int i = p; i <= q; i++) {
        if (i % r == 0) {
            v.Add(i);
        }
    }
 
    // Vector to store pair of answer
    List> ans = new List>();
 
    // Iterate through the vector v
    for(int i = 0; i < v.Count; i++) {
 
        // Iterate in the range [i+1, v.size()-1]
        for (int j = i + 1; j < v.Count; j++) {
 
            // If pair follow this condition
            // insert the pair in vector ans
            if (v[i] * v[j] >= p * q / 4
                && v[i] * v[j] <= p * q) {
                List temp = new List();
                temp.Add(v[i]);
                temp.Add(v[j]);
                ans.Add(temp);
            }
        }
    }
 
    // If no pair satisfy the conditions, print -1
    if (ans.Count == 0) {
        Console.Write(-1);
    }
    else {
          
         foreach (List subList in ans)
        {
            foreach (int item in subList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }
        // Print the pairs
        // which satisfy the given condition
    }
}
 
// Driver Code
public static void Main()
{
 
    // Given Input
    int p = 14, q = 30, r = 5;
 
    // Function Call
    findPairs(p, q, r);
 
}
 
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
15 20
15 25

时间复杂度: O(N 2 ),其中NQ – P + 1。
辅助空间: O(N)