-1

This was listed as a "Hard question" in my practice coding quiz and I wanted to try my luck but the second output is really throwing me for a loop. Why is the second output correct in view of the first output?

Danny has a graph paper that consists of X equally spaced horizontal lines and Y equally spaced vertical lines. Each intersection point is called an i-point. Danny draws a random triangle on the graph paper with all the three vertices coinciding with an i-point. The area of the triangle is not 0. There can be many such triangles. Assume that all the triangles have an equal probability of being drawn by Danny. Calculate the expected number of i-points on the lines of the triangle, i.e., on the perimeter of the triangle. It is guaranteed that the value can be represented as an irreducible fraction p/q where p and q are co-prime to 10^9 + 7. Let x be the modular multiplicative inverse of q with respect to modulus 10^9 + 7. Print the answer as p.x (mod 10^9 +7)

Input Format

The first and only line contains 2 integers X and Y

Constraints

2<=A,B<=10^6

Output Format

Output in a single line the answer p.x (mod 10^9 + 7)

Sample Input 1:
2 2

Sample Output 1:
3

Sample Input 2:
2 3

Sample Output 2:
333333339

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

This is the grid

 |  |  |
-a--b--c-
 |  |  |
-d--e--f-
 |  |  |

So, possible triangles are:

  • abd (3)
  • abe (3)
  • abf (3)
  • acd (4)
  • ace (4)
  • acf (4)
  • bcd (3)
  • bce (3)
  • bcf (3)
  • dea (3)
  • deb (3)
  • dec (3)
  • dfa (4)
  • dfb (4)
  • dfc (4)
  • efa (3)
  • efb (3)
  • efc (3)

The second such grid, so we see that 66.(6)% of the possible triangles touch 3 i points and 33.(3)% of the possible triangles touch 4 i points. So, the likely number of i points is 3.(3)%

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175