Beta

Count Empty Triangles in the Future

6 of 8EAirPeter

Description:

We call a triangle on XY-plane an empty triangle, if and only if:

  • All three vertices of the triangle are integer points, which is to say their x and y coordinates are both integer.
  • Except for its three vertices, there is no integer point inside it or on the edge of it.

Given two positive integer N and M, find the number of empty triangles in the rectangular area with bottom-left corner (0,0) and top-right corner (N,M).

For example: For N = 1 and M = 2, there are 12 such triangles:

{(0,0),(1,0),(0,1)} {(0,0),(1,0),(1,1)} {(0,0),(1,1),(0,1)} {(1,1),(0,1),(1,0)}
{(0,1),(1,1),(1,2)} {(0,1),(1,1),(0,2)} {(0,1),(1,2),(0,2)} {(1,2),(0,2),(1,1)}
{(0,0),(1,1),(1,2)} {(1,0),(1,1),(0,2)} {(0,2),(0,1),(1,0)} {(1,2),(0,1),(0,0)}

But the triangle {(0,0),(1,1),(0,2)} is not counted, because an integer point (0,1) lies on one of its edges.

Since the answer can be very large, you are only required to return the original answer modulo P, where P = 1000000007.

(That is to say: if the number of empty triangles is 1000000100, you only need to return 93)

It is guaranteed that:

  • 1 <= N,M <= 5000000000
  • There will be 5 cases Max(N,M) > 1000000000

Hint:

  • You may want a solution running faster than linear time or something like that.
  • You can make use of P, which may lead up many things.
  • You may want to make use of many mathematical theorems.
  • If you find this kata is too hard for you, you may try the present or past one.

Note:

  • Past is the lowest difficulty in a game called Arcaea.
  • Present is the moderate difficulty in a game called Arcaea.
  • Future is the hardest difficulty in a game called Arcaea.
Mathematics
Performance
Algorithms

Similar Kata:

More By Author:

Check out these other kata created by EAirPeter

Stats:

CreatedFeb 27, 2019
PublishedFeb 27, 2019
Warriors Trained95
Total Skips15
Total Code Submissions246
Total Times Completed8
C++ Completions6
Java Completions4
Total Stars8
% of votes with a positive feedback rating100% of 5
Total "Very Satisfied" Votes5
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Total Rank Assessments5
Average Assessed Rank
1 kyu
Highest Assessed Rank
1 kyu
Lowest Assessed Rank
2 kyu
Ad
Contributors
  • EAirPeter Avatar
  • Voile Avatar
Ad