Despite being a CS major student, Little B has a very good mathematical foundation and a special interest in numerical computing, and likes to use computer programs to solve mathematical problems, and now she is playing a game of numerical transformation. She found that computers often use different binary representation of a number, such as the decimal number 123 is expressed as 16 binary only contains two digits 7, one (B), with octal representation of three digits 1, 7, 3, according to the different binary expression, each single digit and also different, In the example above, the numbers in hexadecimal and octal are 18 and 11, respectively. Small B is interested in, a number a if the 2 to A-1 in the expression, the sum of the average number of single digits? She wants you to help her solve the problem? All calculations are based on decimal, and the result is expressed in decimal as a fractional form of irreducible simplicity.
1 #Coding=utf-82 3 defgcd (x, y):4 ifx>=y:5Ma=x6Mi=y7 Else:8Ma=y9Mi=xTen One ifma%mi==0: A returnmi - Else: - returnGCD (mi,ma%mi) the -A=Int (raw_input ()) -sum=0 - forIinchRange (2, a): +m=a - while(m>0): +r=m%I Am=m/I atSum + =R - - PrintSTR (SUM/GCD (sum,a-2)) +'/'+str ((a-2)/GCD (sum,a-2))
The 18-23-line code solves the sum of the digits in the number a represented as 2 into the A-1 binary
Because the topic requires the result to be reduced to fractions, so the numerator is sum, the denominator is a-2, need to require their two greatest common divisor (the division method to achieve, see 3-14 lines), and then the numerator denominator divided by greatest common divisor, the final printing with a string connection form, '/' The numerator and denominator are connected together to output.
"Algorithm 5" to find the mean value of the sum of the digits of the different binary representations