CodeChef: Codechef
Site under maintenance
  • Register
  • Forgot Password?
  • PRACTICE
    • Easy
    • Medium
    • Hard
  • COMPETE
    • August Mini Challenge
    • August Algorithm Challenge
    • July Algorithm Challenge
    • June Algorithm Challenge
    • May Gamers Challenge
  • DISCUSS
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef TechTalks
    • CodeChef Meetups
    • Campus Chapters
    • Host Your Contest
    • User Groups
  • HELP
    • Frequently Asked Questions
    • FAQ for Problem Setters
    • Tutorial: Paying Up
    • Tutorial: Small Factorials
    • Tutorial: Input and Output(I/O)
    • Tutorial: Your First Non-Trivial Problem
  • ABOUT
    • About CodeChef
    • About Directi
    • CEO's Corner
    • Press Room
    • Careers

Home » Problems (hard) » Lame Queen Game

Lame Queen Game

Problem code: LAMQUGAM

  • Submit
  • All submissions

All submissions for this problem are available.

Code and Chef play a game on the set of all points with positive integer coordinates of the usual coordinate plane. Initially the lame queen stands in the point with coordinates (x0, y0) or shortly: in the point (x0, y0). Players alternate moves. Code moves first. During each move the player can move the queen from the point (x, y) to the point (x - a, y), (x, y - a) or (x - a, y - a) for some positive integer a, provided that the new point has positive coordinates. But since the queen is lame she possibly can't make all diagonal moves. Namely she can move from the point (x, y) to the point (x - a, y - a) only if a is divisible by d, where d is some positive integer that is called the queen lameness. So if d = 2 the queen can move diagonally from the point (x, y) only to the points (x - 2, y - 2), (x - 4, y - 4), (x - 6, y - 6) and so on. However if d = 1 then the queen becomes the usual chess queen. The first player who can't make a valid move loses the game. Both players play optimally in their respective moves.

After playing this game for some time Chef realizes that he loses almost always. Now he wants to know what is the probability that he wins if initial point (x0, y0) is chosen randomly from the rectangle x1 <= x0 <= x2, y1 <= y0 <= y2. But I should warn that he wants to know this for a large number of rectangles.

Input

The first line contains two space separated positive integers d and T, the queen lameness and the number of rectangles for which Chef wants to know the answer. Each of the next T lines contains four space separated positive integers x1, y1, x2, y2 the coordinates of rectangle corners.

Output

For each rectangle from the input file, output a single line containing the probability in the form of an irreducible fraction that Chef wins if the initial cell is chosen randomly from this rectangle.

Constraints

1 <= d <= 20
1 <= T <= 100000
1 <= x1 <= x2 <= 200000
1 <= y1 <= y2 <= 200000

Example

Input:
1 4
1 1 4 4
2 4 5 8
3 7 10 10
5 8 5 8

Output:
3/16
1/10
1/32
1/1

Explanation

For the first rectangle Chef wins in three cases out of 16. Namely when the starting point is (1,1), (2,3) or (3,2).
The fourth rectangle is lucky. It contains only one point and this one is winning for Chef.


Date:2011-10-13
Time limit:6s
Source limit:50000B
Languages:All


  • Submit

Comments

Loading Comments...

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Loading Submissions...

RECENT ACTIVITY FOR THIS PROBLEM:

Loading Recent Activity...

HELP

Program should read from standard input and write to standard output.

After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.

If you are still having problems, see a sample solution here.

  • About CodeChef
  • About Directi
  • CEO's Corner
  • Careers
  • feedback@codechef.com

© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs

Sponsors
The time now is: