Count numbers up to C that can be reduced to 0 by adding or subtracting A or B | C ++ program for the above approach ; Function to calculate GCD of the two numbers a and b ; Base Case ; Recursively find the GCD ; Function to count the numbers up to C that can be reduced to 0 by adding or subtracting A or B ; Stores GCD of A and B ; Stores the count of multiples of g in the range ( 0 , C ] ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long gcd ( long long a , long long b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void countDistinctNumbers ( long long A , long long B , long long C ) { long long g = gcd ( A , B ) ; long long count = C / g ; cout << count ; } int main ( ) { long long A = 2 , B = 3 , C = 5 ; countDistinctNumbers ( A , B , C ) ; return 0 ; }