fork(3) download
  1. #include <iostream>
  2. #include <limits>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. const long long int A = 222222200;
  9.  
  10. // Form the starting number in the form 4xxxx with the same number of digits of A
  11. // AxB will be less than A at the beginning. This is necessary to match values of A
  12. // like 400 which will just need B = 1 to be in the correct form
  13. long long int AxB = 4;
  14. int digits = 1;
  15. while (AxB < A) {
  16. AxB *= 10;
  17. ++digits;
  18. }
  19. AxB /= 10;
  20. --digits;
  21.  
  22. // Augment AxB of one zero digit at the right at each iteration
  23. do {
  24. // Try to fill all the zeros with 4s to obtain progressively greater values, e.g.
  25. // 4000
  26. // 4400
  27. // 4440
  28. // 4444
  29. long long int tempAxB = AxB;
  30. for (int i = digits - 1; i > 0; --i) {
  31. tempAxB += 4 * (long long int)pow(10, i - 1);
  32.  
  33. if(tempAxB < 0)
  34. return 0; // Overflow
  35.  
  36. if (tempAxB % A == 0) {
  37. cout << "Minimum B is " << tempAxB / A << endl;
  38. return 0;
  39. }
  40. }
  41.  
  42. // Increase AxB
  43. AxB *= 10;
  44. ++digits;
  45. } while (AxB > 0); // Do not overflow
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Minimum B is 2