Count of distinct integers belonging to first N terms of at least one of given GPs | C ++ 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 ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int UniqueGeometricTerms ( int N , int a1 , int r1 , int a2 , int r2 ) { set < int > 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 << UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ; return 0 ; }