#P13392. [GCJ 2010 #1A] Number Game

[GCJ 2010 #1A] Number Game

题目描述

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

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

  • Arya replaces 5151 with 513×12=1551 - 3 \times 12 = 15, leaving (12,15)(12, 15) on the blackboard.
  • Bran replaces 1515 with 151×12=315 - 1 \times 12 = 3, leaving (12,3)(12, 3) on the blackboard.
  • Arya replaces 1212 with 123×3=312 - 3 \times 3 = 3, leaving (3,3)(3, 3) on the blackboard.
  • Bran replaces one 33 with 31×3=03 - 1 \times 3 = 0, and loses.

We will say (A,B)(A, B) is a winning position if Arya can always win a game that starts with (A,B)(A, B) on the blackboard, no matter what Bran does.

Given four integers A1A_1, A2A_2, B1B_1, B2B_2, count how many winning positions (A,B)(A, B) there are with A1AA2A_1 \leq A \leq A_2 and B1BB2B_1 \leq B \leq B_2.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow, one per line. Each line contains the four integers A1A_1, A2A_2, B1B_1, B2B_2, separated by spaces.

输出格式

For each test case, output one line containing "Case #x: y", where xx is the case number (starting from 1), and yy is the number of winning positions (A,B)(A, B) with A1AA2A_1 \leq A \leq A_2 and B1BB2B_1 \leq B \leq B_2.

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

提示

Limits

  • 1T100.1 \leqslant T \leqslant 100.
  • 1A1A21,000,000.1 \leqslant A_1 \leqslant A_2 \leqslant 1,000,000.
  • 1B1B21,000,000.1 \leqslant B_1 \leqslant B_2 \leqslant 1,000,000.

Small dataset (16 Pts, Test set 1 - Visible)

  • Time limit: 30 3 seconds.
  • A2A130.A_2 - A_1 \leqslant 30.
  • B2B130.B_2 - B_1 \leqslant 30.

Large dataset (25 Pts, Test set 2 - Hidden)

  • Time limit: 90 9 seconds.
  • A2A1999,999.A_2 - A_1 \leqslant 999,999.
  • B2B1999,999.B_2 - B_1 \leqslant 999,999.
  • No additional constraints.