Smallest string without any multiplication sign that represents the product of two given numbers | C ++ program for the above approach ; Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ log <= B && 2 ^ ( log + 1 ) > B ; Update res to res += A X 2 ^ log ; Update res to res += A X 2 ^ 0 ; Find the remainder ; If remainder is not equal to 0 ; Return the resultant string ; Function to find the minimum length representation of A * B ; Find representation of form A << k1 + A << k2 + ... + A << kn ; Find representation of form B << k1 + B << k2 + ... + B << kn ; Compare the length of the representations ; Driver code ; Product A X B ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string len ( long A , long B ) { string res = " " ; long Log = 0 ; do { Log = ( long ) ( log ( B ) / log ( 2 ) ) ; if ( Log != 0 ) { res = res + to_string ( A ) + " < < " + to_string ( Log ) ; } else { res += A ; break ; } B = B - ( long ) pow ( 2 , Log ) ; if ( B != 0 ) { res += " + " ; } else break ; } while ( Log != 0 ) ; return res ; } void minimumString ( long A , long B ) { string res1 = len ( A , B ) ; string res2 = len ( B , A ) ; if ( res1 . length ( ) > res2 . length ( ) ) { cout << res2 << endl ; } else { cout << res1 << endl ; } } int main ( ) { long A = 6 ; long B = 10 ; minimumString ( A , B ) ; return 0 ; }