fork(1) download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. unsigned long long divisors[] = {
  5. 1000000000,
  6. 1000000000,
  7. 1000000000,
  8. 1000000000,
  9. 100000000,
  10. 100000000,
  11. 100000000,
  12. 10000000,
  13. 10000000,
  14. 10000000,
  15. 1000000,
  16. 1000000,
  17. 1000000,
  18. 1000000,
  19. 100000,
  20. 100000,
  21. 100000,
  22. 10000,
  23. 10000,
  24. 10000,
  25. 1000,
  26. 1000,
  27. 1000,
  28. 1000,
  29. 100,
  30. 100,
  31. 100,
  32. 10,
  33. 10,
  34. 10,
  35. 1,
  36. 1,
  37. 1
  38. };
  39.  
  40.  
  41. int main()
  42. {
  43. unsigned long long int i, j, res;
  44.  
  45. unsigned char inbuff[2500000]; /* To be certain there's no overflow here */
  46. unsigned char *in = inbuff;
  47. char outbuff[2500000]; /* To be certain there's no overflow here */
  48. char *out = outbuff;
  49.  
  50. int c = 0;
  51.  
  52. while(1) {
  53. i = j = 0;
  54.  
  55. inbuff[fread(inbuff, 1, 2500000, stdin)] = '\0';
  56.  
  57. /* Skip whitespace before first number and check if end of input */
  58. do {
  59. c = *(in++);
  60. } while(c != '\0' && !(c >= '0' && c <= '9'));
  61.  
  62. /* If end of input, print answer and return */
  63. if(c == '\0') {
  64. *(--out) = '\0';
  65. puts(outbuff);
  66. return 0;
  67. }
  68.  
  69. /* Read first integer */
  70. do {
  71. i = 10 * i + (c - '0');
  72. c = *(in++);
  73. } while(c >= '0' && c <= '9');
  74.  
  75. /* Skip whitespace between first and second integer */
  76. do {
  77. c = *(in++);
  78. } while(!(c >= '0' && c <= '9'));
  79.  
  80. /* Read second integer */
  81. do {
  82. j = 10 * j + (c - '0');
  83. c = *(in++);
  84. } while(c >= '0' && c <= '9');
  85.  
  86. if(i > j)
  87. res = i-j;
  88. else
  89. res = j-i;
  90.  
  91.  
  92.  
  93. /* Buffer answer */
  94. if(res == 0) {
  95. *(out++) = '0';
  96. } else {
  97. unsigned long long divisor = divisors[__builtin_clzll(res)-31];
  98. /* Skip trailing 0 */
  99. if(res < divisor) {
  100. divisor /= 10;
  101. }
  102. /* Buffer digits */
  103. while(divisor != 0) {
  104. unsigned long long digit = res / divisor;
  105. *(out++) = digit + '0';
  106. res -= divisor * digit;
  107. divisor /= 10;
  108. }
  109. }
  110. *(out++) = '\n';
  111. }
  112. }
Success #stdin #stdout 0s 15064KB
stdin
10 12
10 14
100 200
stdout
2
4
100