QOJ.ac

QOJ

Time Limit: 4 s - 25 s Memory Limit: 1024 MB Total points: 41

#5833. Number Game

统计

Problem

Arya and Bran are playing a game. Initially, two positive integers A and B are written on a blackboard. The players take turns, starting with Arya. On his or her turn, a player can replace A with A - kB for any positive integer k, or replace B with B - kA for any positive integer k. The first person to make one of the numbers drop to zero or below loses.

For example, if the numbers are initially (12, 51), the game might progress as follows:

Arya replaces 51 with 51 - 312 = 15, leaving (12, 15) on the blackboard. Bran replaces 15 with 15 - 112 = 3, leaving (12, 3) on the blackboard. Arya replaces 12 with 12 - 33 = 3, leaving (3, 3) on the blackboard. Bran replaces one 3 with 3 - 13 = 0, and loses. We will say (A, B) is a winning position if Arya can always win a game that starts with (A, B) on the blackboard, no matter what Bran does.

Given four integers A1, A2, B1, B2, count how many winning positions (A, B) there are with A1AA2 and B1BB2.

Input

The first line of the input gives the number of test cases, T. T test cases follow, one per line. Each line contains the four integers A1, A2, B1, B2, separated by spaces.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1), and y is the number of winning positions (A, B) with A1AA2 and B1BB2.

Limits

Memory limit: 1GB.

1 ≤ T ≤ 100.

1 ≤ A1A2 ≤ 1,000,000.

1 ≤ B1B2 ≤ 1,000,000.

Small dataset (Test set 1 - Visible; 16 Points)

Time limit: 30 4 seconds.

A2 - A1 ≤ 30.

B2 - B1 ≤ 30.

Large dataset (Test set 2 - Hidden; 25 Points)

Time limit: 90 25 seconds.

A2 - A1 ≤ 999,999.

B2 - B1 ≤ 999,999.

No additional constraints.

Sample

3
5 5 8 8
11 11 2 2
1 6 1 6
Case #1: 0
Case #2: 1
Case #3: 20
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.