The Number Theory of Hankson's Fun Questions in the Blue Bridge Cup

Problem Description
  Dr. Hanks is a well-known expert in the field of BT (Bio-Tech), his son's name is Hankson. Now, Hankson, who has just returned home from school, is thinking about an interesting question. In class today, the teacher explained how to find the greatest common divisor and least common multiple of two positive integers c1 and c2. Now that Hankson thinks he has mastered this knowledge, he begins to think about the "inverse problem" of such problems as "finding the common divisor" and "finding the common multiple". The problem is this: the positive integers a0, a1, b0 are known, b1. Let an unknown positive integer x satisfy: 1. The greatest common divisor of x and a0 is a1; 2. The least common multiple of x and b0 is b1. Hankson's "inverse problem" is to find a positive integer x that satisfies the condition. But after thinking a bit, he found that such x is not unique and may not even exist. So he turned to consider how to find the number of x that satisfies the condition. Please help him to solve this problem by programming.
Input format
  Enter the first line as a positive integer n, indicating that there are n sets of input data.
  Each group of input data in the next n rows is four positive integers a0, a1, b0, b1, and each two integers are separated by a space. The input data ensures that a0 is divisible by a1 and b1 is divisible by b0.
Output format
  There are n lines of output. The output result of each set of input data occupies one line and is an integer.
  For each set of data: if there is no such x, please output 0; if there is such x, please output the number of x that meets the condition;
Sample input
2
41 1 96 288
95 1 37 1776
Sample output
6
2
Sample description
  The first set of input data, x can be 9, 18, 36, 72, 144, 288, a total of six.
  The second set of input data, x can be 48, 1776, there are 2 in total.
Data size and agreement
  For 50% of the data, it is guaranteed that 1≤a0, a1, b0, b1≤10000 and n≤100.
  For 100% data, it is guaranteed that 1≤a0, a1, b0, b1≤2,000,000,000 and n≤2000.
Mathematics is terrible, and the subject of number theory has not yet begun to learn.
Paste the codes of the two big brothers first, and review them later.
Reprinted from https://blog.csdn.net/IoT_fast/article/details/84843294
 1 #include <iostream>
 2 #include <fstream>
 3 #include <cstring> 
 4 #include <stdio.h>
 5 #include <cmath>
 6 
 7 using namespace std;
 8 
 9 int gcd(int a,int b) //求最大公因数 
10 {
11     return b==0?a:gcd(b,a%b);
12 }
13 
14 int main()
15 {
16     //freopen("input/Hackson.txt","r",stdin);
17     int n,a0,a1,b0,b1,p,q,cnt=0;
18     scanf("%d",&n);
19     while(n--)
20     {
21         scanf("%d%d%d%d",&a0,&a1,&b0,&b1);
22         p=a0/a1;q=b1/b0;
23         for(int i=1;i<=sqrt(b1);i++)
24         {
25             if(b1%i==0)
26             {
27                 if(gcd(i/a1,p)==1
28                 &&gcd(q,b1/i)==1
29                 &&(i%a1==0)) cnt++;
30                 int j=b1/i;
31                 if(i==j) continue;
32                 if(gcd(j/a1,p)==1&&gcd(q,b1/j)==1&&(j%a1==0)) cnt++;
33             }
34         }
35         printf("%d\n",cnt);
36         cnt=0;
37     } 
38     return 0;
39 }
Reprinted from https://www.liuchuo.net/archives/7751
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int gcd(int a, int b) {
 4     if (b == 0) return a;
 5     return gcd(b, a % b);
 6 }
 7 using namespace std;
 8 int main() {
 9     int a0, a1, b0, b1, k;
10     cin >> k;
11     while (k--) {
12         int ans = 0;
13         scanf("%d %d %d %d", &a0, &a1, &b0, &b1);
14         for (int i = 1; i * i <= b1; i++) {
15             if(b1 % i == 0){
16                 int n = i;
17                 if (gcd(a0, n) == a1 && b0 * n == b1 * gcd(b0, n))
18                     ans++;
19                 if(i != b1 / i) {
20                     n = b1 / i;
21                     if (gcd(a0, n) == a1 && b0 * n == b1 * gcd(b0, n))
22                         ans++;
23                 }
24             }
25         }
26         printf("%d\n",ans);
27     }
28     return 0;
29 }

Guess you like

Origin www.cnblogs.com/fx1998/p/12696428.html