SRM_420_div2_1000pt
Problem Statement
You will be given two positive ints A and B.
Let C be the product of all integers between A and B, inclusive.
The number C has a unique representation of the form C = D * 10^E, where D and E are non-negative integers and the last digit of D is non-zero.
Write a method that will return the value of C formatted as a String of the form "D * 10^E" (quotes for clarity only). Substitute the actual values of D and E into the output. If D has more than 10 digits, only output the first five and the last five digits of D, and separate them by three periods.
Definition
Class: |
PrettyPrintingProduct |
Method: |
prettyPrint |
Parameters: |
int, int |
Returns: |
String |
Method signature: |
String prettyPrint(int A, int B) |
(be sure your method is public) |
Notes
-The purpose of the last constraint is to disallow inputs where precision problems could arise.
Constraints
-A will be between 1 and 1,000,000, inclusive.-B will be between A and 1,000,000, inclusive.-If C has more than 10 digits, then the sixth most significant digit of C will be neither 0 nor 9.
Examples
0)
|
||
Returns: "36288 * 10^2" |
||
|
1)
|
||
Returns: "7 * 10^0" |
||
|
2)
|
||
Returns: "2038974024 * 10^0" |
||
|
3)
|
||
Returns: "28952...24024 * 10^0" |
||
|
4)
|
||
Returns: "2923450236 * 10^1" |
||
|
5)
|
||
Returns: "14806...28928 * 10^1163" |
||
|
6)
|
||
Returns: "12164...08832 * 10^3" |
||
|
7)
|
||
Returns: "32382...26624 * 10^4" |
2 #include < cmath >
3 #include < ctime >
4 #include < iostream >
5 #include < string >
6 #include < vector >
7 using namespace std;
8
9 typedef long long i64;
10
11 class PrettyPrintingProduct {
12 public :
13 string prettyPrint( int A, int B );
14 };
15 bool check( int A, int B,i64 & D,i64 & E){
16 D = 1 ;E = 0 ;
17 for ( int i = A;i <= B;i ++ ){
18 D *= i;
19 while (D % 10LL == 0LL) {
20 D /= 10LL;
21 E ++ ;
22 }
23 if (D > 10000000000LL) return false ;
24 }
25 return true ;
26 }
27
28 string PrettyPrintingProduct::prettyPrint( int A, int B){
29 i64 D,tens;
30 char str[ 256 ];
31
32 if (check(A,B,D,tens)){
33 cout << D << " " << tens << endl;
34 sprintf(str, " %lld * 10^%lld " ,D,tens);
35 return str;
36 }
37
38 i64 a = 1 ,b = 1 ;
39 tens = 0 ;
40 for ( int i = A;i <= B;i ++ ){
41 a *= i;
42 b *= i;
43 while (b % 10LL == 0LL) b /= 10LL,tens ++ ;
44 b %= 1000000000000LL;
45 while (a >= 1000000000000LL) a /= 10LL;
46 }
47 while (a >= 100000LL) a /= 10LL;
48 b %= 100000LL;
49 cout << a << " " << b << " " << tens << endl;
50 sprintf(str, " %lld%05lld * 10^%lld " , a, b, tens);
51 return str;
52 }
53