Under construction

Still being built. Things may change

Large Banner

Time limit1sMemory limit128 MB

Summary
Count pairs of lattice posts in an (M+1) by (N+1) grid whose segment contains no other post and whose Euclidean length lies in [L, H], modulo B.
Level

Hard8 of 10

Topics
Number theory, Math, Geometry, Binary search
Solved
No attempts yet

Problem

Bessie is returning from a long trip abroad, and Farmer John wants to mount a nice "Welcome Home" banner for her arrival. Farmer John's field has integer dimensions M×NM \times N (1≤M,N≤100,0001 \le M, N \le 100{,}000). He has installed a post at every point with integer coordinates, using a coordinate system in which (0,0)(0, 0) is the lower-left corner and (M,N)(M, N) is the upper-right corner. Of these (M+1)×(N+1)(M + 1) \times (N + 1) posts, Farmer John must choose two as the endpoints of the banner.

Being a perfectionist, Farmer John insists that the banner be perfectly straight: for the two chosen posts, no other post may lie on the straight line segment between them. For instance, posts (0,0)(0, 0) and (2,0)(2, 0) cannot both be chosen, because post (1,0)(1, 0) lies between them.

In addition, the banner's length must be at least LL and at most HH (1≤L≤H≤150,0001 \le L \le H \le 150{,}000), where the length is the Euclidean distance between the two endpoints.

The banner is reversible, so swapping its two endpoints yields the same way of hanging it. Count the number of distinct ways Farmer John can hang the banner. Because this number can be very large, report it modulo BB (1≤B≤1,000,000,0001 \le B \le 1{,}000{,}000{,}000).

Input

A single line with five space-separated integers: MM, NN, LL, HH, and BB.

Output

A single line with one integer: the number of valid ways to hang the banner, taken modulo BB.

Examples2

  1. Example 1

    Input
    2 2 1 3 100
    
    Expected output
    28
    
  2. Example 2

    Input
    1 1 1 2 1000000000
    
    Expected output
    6