#P1072. [NOIP 2009 提高组] Hankson 的趣味题

    ID: 72 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>数学2009NOIp 提高组枚举,暴力最大公约数,gcd

[NOIP 2009 提高组] Hankson 的趣味题

Description

Dr. Hanks is a renowned expert in BT (Bio-Tech, 生物技术). His son is named Hankson. Now, just home from school, Hankson is thinking about an interesting problem.

Today in class, the teacher explained how to find the greatest common divisor and least common multiple of two positive integers c1c_1 and c2c_2. Now that Hankson believes he has mastered these topics, he starts considering an "inverse problem" to problems like "finding a common divisor" and "finding a common multiple". The problem is as follows: given positive integers a0,a1,b0,b1a_0, a_1, b_0, b_1, let an unknown positive integer xx satisfy:

  1. The greatest common divisor of xx and a0a_0 is a1a_1.
  2. The least common multiple of xx and b0b_0 is b1b_1.

Hankson's "inverse problem" is to find all positive integers xx that satisfy the conditions. After a little thought, he realizes such xx are not necessarily unique and may even not exist. Therefore, he turns to counting how many xx satisfy the conditions. Please help him write a program to solve this problem.

Input Format

The first line contains a positive integer nn, indicating there are nn sets of input. Each of the next nn lines contains one set of input: four positive integers a0,a1,b0,b1a_0, a_1, b_0, b_1, separated by single spaces. It is guaranteed that a0a_0 is divisible by a1a_1, and b1b_1 is divisible by b0b_0.

Output Format

Output nn lines. For each set of input, output a single integer on one line.

For each set: if no such xx exists, print 00; if such xx exist, print the number of xx that satisfy the conditions.

2 
41 1 96 288 
95 1 37 1776 
6 
2

Hint

  • [Sample Explanation]
    • For the first set, xx can be 9,18,36,72,144,2889, 18, 36, 72, 144, 288, for a total of 66.
    • For the second set, xx can be 48,177648, 1776, for a total of 22.
  • [Constraints]
    • For 50%50\% of the testdata, it is guaranteed that 1a0,a1,b0,b1100001 \leq a_0, a_1, b_0, b_1 \leq 10000 and n100n \leq 100.
    • For 100%100\% of the testdata, it is guaranteed that 1a0,a1,b0,b12×1091 \leq a_0, a_1, b_0, b_1 \leq 2 \times 10^9 and n2000n \leq 2000.

Translated by ChatGPT 5