📌  相关文章
📜  属于至少一个给定 GP 的前 N 项的不同整数的计数

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

属于至少一个给定 GP 的前 N 项的不同整数的计数

给定两个几何级数(a1, r1)(a2, r2)其中(x, y)表示具有初始项x和共同比率y和整数NGP ,任务是找到属于的不同整数的计数给定几何级数中的至少一个的前N项。

例子:

方法:给定的问题可以通过观察来解决,即可以通过生成几何级数的前 N 项并删除重复项来计算不同整数的总数。这可以通过使用集合数据结构来实现。首先,生成第一个GP的所有 N 项并将这些项插入到集合S中。类似地,将第二个 GP 的项插入到集合S中。集合S的大小是所需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the count of distinct
// integers that belong to the first N
// terms of at least one of them is GP
int UniqueGeometricTerms(int N, int a1,
                         int r1, int a2,
                         int r2)
{
    // Stores the integers that occur in
    // GPs in a set data-structure
    set S;
 
    // Stores the current integer of
    // the first GP
    long long p1 = a1;
 
    // Iterate first N terms of first GP
    for (int i = 0; i < N; i++) {
 
        // Insert the ith term of GP in S
        S.insert(p1);
        p1 = (long long)(p1 * r1);
    }
 
    // Stores the current integer
    // of the second GP
    long long p2 = a2;
 
    // Iterate first N terms of second GP
    for (int i = 0; i < N; i++) {
        S.insert(p2);
        p2 = (long long)(p2 * r2);
    }
 
    // Return Answer
    return S.size();
}
 
// Driver Code
int main()
{
    int N = 5;
    int a1 = 3, r1 = 2, a2 = 2, r2 = 3;
 
    cout << UniqueGeometricTerms(
        N, a1, r1, a2, r2);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find the count of distinct
// integers that belong to the first N
// terms of at least one of them is GP
static int UniqueGeometricTerms(int N, int a1,
                         int r1, int a2,
                         int r2)
{
    // Stores the integers that occur in
    // GPs in a set data-structure
    HashSet S=new HashSet();
 
    // Stores the current integer of
    // the first GP
    int p1 = a1;
 
    // Iterate first N terms of first GP
    for (int i = 0; i < N; i++) {
 
        // Insert the ith term of GP in S
        S.add(p1);
        p1 = (p1 * r1);
    }
 
    // Stores the current integer
    // of the second GP
    int p2 = a2;
 
    // Iterate first N terms of second GP
    for (int i = 0; i < N; i++) {
        S.add(p2);
        p2 = (p2 * r2);
    }
 
    // Return Answer
    return S.size();
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5;
    int a1 = 3, r1 = 2, a2 = 2, r2 = 3;
 
    System.out.print(UniqueGeometricTerms(
        N, a1, r1, a2, r2));
 
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 3 program for the above approach
 
# Function to find the count of distinct
# integers that belong to the first N
# terms of at least one of them is GP
def UniqueGeometricTerms(N, a1, r1, a2, r2):
   
    # Stores the integers that occur in
    # GPs in a set data-structure
    S = set()
 
    # Stores the current integer of
    # the first GP
    p1 = a1
 
    # Iterate first N terms of first GP
    for i in range(N):
       
        # Insert the ith term of GP in S
        S.add(p1)
        p1 = (p1 * r1)
 
    # Stores the current integer
    # of the second GP
    p2 = a2
 
    # Iterate first N terms of second GP
    for i in range(N):
        S.add(p2)
        p2 = (p2 * r2)
 
    # Return Answer
    return len(S)
 
# Driver Code
if __name__ == '__main__':
    N = 5
    a1 = 3
    r1 = 2
    a2 = 2
    r2 = 3
 
    print(UniqueGeometricTerms(N, a1, r1, a2, r2))
 
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Function to find the count of distinct
// integers that belong to the first N
// terms of at least one of them is GP
static int UniqueGeometricTerms(int N, int a1,
                         int r1, int a2,
                         int r2)
{
    // Stores the integers that occur in
    // GPs in a set data-structure
    HashSet S=new HashSet();
 
    // Stores the current integer of
    // the first GP
    int p1 = a1;
 
    // Iterate first N terms of first GP
    for (int i = 0; i < N; i++) {
 
        // Insert the ith term of GP in S
        S.Add(p1);
        p1 = (p1 * r1);
    }
 
    // Stores the current integer
    // of the second GP
    int p2 = a2;
 
    // Iterate first N terms of second GP
    for (int i = 0; i < N; i++) {
        S.Add(p2);
        p2 = (p2 * r2);
    }
 
    // Return Answer
    return S.Count;
}
 
// Driver Code
public static void Main(string[] args)
{
    int N = 5;
    int a1 = 3, r1 = 2, a2 = 2, r2 = 3;
 
    Console.Write(UniqueGeometricTerms(
        N, a1, r1, a2, r2));
}
}
 
// This code is contributed by AnkThon


Javascript


输出:
9

时间复杂度: O(N*log N)
辅助空间: O(N)