Count of distinct values till C formed by adding or subtracting A , B , or 0 any number of times | C ++ program for the above approach ; Function to calculate gcd ; Function to find number of possible final values ; Find the gcd of two numbers ; Calculate number of distinct values ; Return values ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int A , int B ) { if ( B == 0 ) return A ; else return gcd ( B , A % B ) ; } int getDistinctValues ( int A , int B , int C ) { int g = gcd ( A , B ) ; int num_values = C / g ; return num_values ; } int main ( ) { int A = 2 ; int B = 3 ; int C = 10 ; cout << ( getDistinctValues ( A , B , C ) ) ; return 0 ; }