Count of distinct integers belonging to first N terms of at least one of given GPs

Given two Geometric Progressions (a1, r1) and (a2, r2) where (x, y) represents GP with initial term x and common ratio y and an integer N, the task is to find the count of the distinct integers that belong to the first N terms of at least one of the given geometric progressions.Examples:Input: N = 5, a1 = 3, r1 = 2, a2 = 6, r2 = 2Output: 6Explanation: The first 5 terms of the given geometric progressions are {3, 6, 12, 24, 48} and {6, 12, 24, 48, 96} respectively. Hence, the total count of distinct integers in the GP is 6.Input: N = 5, a1 = 3, r1 = 2, a2 = 2, r2 = 3Output: 9Explanation: The first 5 terms of the given geometric progressions are {3, 6, 12, 24, 48} and {2, 6, 18, 54, 162} respectively. Hence, the total count of distinct integers in the GP is 9.Approach: The given problem can be solved by the observation that the total count of distinct integers can be calculated by generating the first N terms of both the Geometric Progressions and removing the duplicates terms. This can be achieved by the use of the set data structure. Firstly, generate all the N terms of the 1st GP and insert the terms into a set S. Similarly, insert the terms of the 2nd GP into the set S. The size of the set S is the required answer.Below is the implementation of the above approach:C++  #include using namespace std;  int UniqueGeometricTerms(int N, int a1,                         int r1, int a2,                         int r2){            set S;              long long p1 = a1;          for (int i = 0; i < N; i++) {                  S.insert(p1);        p1 = (long long)(p1 * r1);    }              long long p2 = a2;          for (int i = 0; i < N; i++) {        S.insert(p2);        p2 = (long long)(p2 * r2);    }          return S.size();}  int main(){    int N = 5;    int a1 = 3, r1 = 2, a2 = 2, r2 = 3;      cout