📌  相关文章
📜  [L, R] 范围内的整数计数不与 [1, R] 中的数字形成任何三角形对

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

[L, R] 范围内的整数计数不与 [1, R] 中的数字形成任何三角形对

给定整数LR ,任务是找到范围 [L, R](比如 X)内的整数个数,它们不与范围 [1, R] 内的任何其他整数形成三角形对。

注意:如果 GCD(X, Y)、X/GCD(X, Y) 和 Y/GCD(X, Y) 可以构成三角形的边,则一对整数 {X, Y} 称为三角形对。

例子:

方法:可以根据以下观察解决问题:

上述观察可以证明如下:

按照下面提到的步骤来实现上述观察:

  • 使用 eratosthenes 筛将质数的数量存储到从 1 到 R 的每个整数。
  • 如上述观察所示计算 C1 和 C2。
  • 从 C2 中减去 C1(比如count )。
  • 返回计数作为最终答案。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
 
#include 
using namespace std;
const int M = 1e5;
 
// Array to store primes upto each
// integer from 1 to N
int p[M + 1];
 
// Array to store whether each integer from
// 1 to N is prime  or not
bool isPrime[M + 1];
 
// Function to count integers in given
// range which does not form a triangle
// with any other integer in the same
// range for all  elements of array
int countNum(int L, int R)
{
    memset(isPrime, true, sizeof(isPrime));
    isPrime[0] = false, isPrime[1] = false;
 
    // Standard sieve method to store
    // which integer is prime
    for (int i = 2; i * i <= R; i++) {
        if (isPrime[i] == true) {
            for (int j = i * i; j <= R;
                 j += i) {
                isPrime[j] = false;
            }
        }
    }
 
    // Prefix sum method to store total primes
    // upto any integer till M
    p[0] = 0;
    for (int i = 1; i <= R; i++) {
        p[i] = p[i - 1];
        if (isPrime[i]) {
            p[i]++;
        }
    }
 
    // Though 2 is prime but it is even also
    p[1] = 1;
    int count = p[R] - p[L - 1];
 
    // Returning answer
    return count;
}
 
// Driver Code
int main()
{
    int L = 1, R = 5;
 
    // Function call
    int answer = countNum(L, R);
    cout << answer;
    return 0;
}


Java
// Java code to implement the above approach
import java.io.*;
 
class GFG
{
 
  // Function to count integers in given
  // range which does not form a triangle
  // with any other integer in the same
  // range for all  elements of array
  public static int countNum(int L, int R)
  {
 
    // Array to store primes upto each
    // integer from 1 to N
    int p[] = new int[100001];
 
    // Array to store whether each integer from
    // 1 to N is prime  or not
    boolean isPrime[] = new boolean[100001];
    for (int i = 0; i < 100001; i++)
      isPrime[i] = true;
 
    isPrime[0] = false;
    isPrime[1] = false;
 
    // Standard sieve method to store
    // which integer is prime
    for (int i = 2; i * i <= R; i++) {
      if (isPrime[i] == true) {
        for (int j = i * i; j <= R; j += i) {
          isPrime[j] = false;
        }
      }
    }
 
    // Prefix sum method to store total primes
    // upto any integer till M
    p[0] = 0;
    for (int i = 1; i <= R; i++) {
      p[i] = p[i - 1];
      if (isPrime[i] == true) {
        p[i]++;
      }
    }
 
    // Though 2 is prime but it is even also
    p[1] = 1;
    int count = p[R] - p[L - 1];
 
    // Returning answer
    return count;
  }
 
  public static void main(String[] args)
  {
    int L = 1, R = 5;
 
    // Function call
    int answer = countNum(L, R);
    System.out.print(answer);
  }
}
 
// This code is contributed by Rohit Pradhan.


Python3
# python3 code to implement the above approach
import math
 
M = int(1e5)
 
# Array to store primes upto each
# integer from 1 to N
p = [0 for _ in range(M + 1)]
 
# Array to store whether each integer from
# 1 to N is prime or not
isPrime = [True for _ in range(M + 1)]
 
# Function to count integers in given
# range which does not form a triangle
# with any other integer in the same
# range for all elements of array
def countNum(L, R):
    global isPrime
 
    isPrime[0], isPrime[1] = False, False
 
    # Standard sieve method to store
    # which integer is prime
    for i in range(2, int(math.sqrt(R)) + 1):
        if (isPrime[i] == True):
            for j in range(i*i, R+1, i):
                isPrime[j] = False
 
        # Prefix sum method to store total primes
        # upto any integer till M
    p[0] = 0
 
    for i in range(1, R+1):
        p[i] = p[i - 1]
        if (isPrime[i]):
            p[i] += 1
 
        # Though 2 is prime but it is even also
    p[1] = 1
    count = p[R] - p[L - 1]
 
    # Returning answer
    return count
 
# Driver Code
if __name__ == "__main__":
 
    L, R = 1, 5
 
    # Function call
    answer = countNum(L, R)
    print(answer)
 
    # This code is contributed by rakeshsahni


C#
// C# code to implement the above approach
using System;
class GFG
{
 
  static int M = 100000;
 
  // Array to store primes upto each
  // integer from 1 to N
  static int[] p = new int[M + 1];
 
  // Array to store whether each integer from
  // 1 to N is prime  or not
  static bool[] isPrime = new bool[M + 1];
 
  // Function to count integers in given
  // range which does not form a triangle
  // with any other integer in the same
  // range for all  elements of array
  static int countNum(int L, int R)
  {
    for (int i = 0; i < M + 1; i++) {
      isPrime[i] = true;
    }
 
    isPrime[0] = false;
    isPrime[1] = false;
 
    // Standard sieve method to store
    // which integer is prime
    for (int i = 2; i * i <= R; i++) {
      if (isPrime[i] == true) {
        for (int j = i * i; j <= R; j += i) {
          isPrime[j] = false;
        }
      }
    }
 
    // Prefix sum method to store total primes
    // upto any integer till M
    p[0] = 0;
    for (int i = 1; i <= R; i++) {
      p[i] = p[i - 1];
      if (isPrime[i]) {
        p[i]++;
      }
    }
 
    // Though 2 is prime but it is even also
    p[1] = 1;
    int count = p[R] - p[L - 1];
 
    // Returning answer
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
    int L = 1, R = 5;
 
    // Function call
    int answer = countNum(L, R);
    Console.Write(answer);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
3

时间复杂度: O(M*log(log(M))),其中 M 为 1e5
辅助空间: O(M)