10204. 「一本通 6.3 例 2」Hankson 的趣味题

题意

已知正整数 $a_0,a_1,b_0,b_1$,设某未知正整数 $x$ 满足:
1. $x$ 和 $a_0$ 的最大公约数是 $a_1$;
2. $x$ 和 $b_0$ 的最小公倍数是 $b_1$。
Hankson 的「逆问题」就是求出满足条件的正整数 $x$ 的个数。

思路

先从第二个条件入手。 $$lcm(x,b_0)=b_1$$ 因为$lcm(x,y)=x*y/gcd(x,y)$ 所以$lcm(x,b_0)=x*b_0/gcd(x,b_0)=b_1$ 化简,得: $$x=(b_1/b_0)*gcd(x,b_0)$$ 因为$b_1,b_0$都是已知量,所以只需要枚举$gcd(x,b_0)$即可求解出$x$。 而$gcd(x,b_0)$必须是$b_0$的因数(废话) 所以只需要从$1$枚举到$sqrt(b_0)$就好了。 注意判断$b_0$是完全平方数的情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
using namespace std;
inline int read(){
int res=0,f=1;char ch=getchar();
while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
return res*f;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x+'0');
else{
write(x/10);
putchar(x%10+'0');
}
}
//queue<int> q;
//set<int> s;
//priority_queue<int> q1;
//priority_queue<int,vector<int>,greater<int> > q2;
//list<int> l;
//stack<int> s;
int T;
int a0,a1,b0,b1,ans,x;
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int main(){
T=read();
while(T--){
a0=read();a1=read();b0=read();b1=read();
int bb=b1/b0;ans=0;
for(int i=1;i<=sqrt(b0);i++){//For gcd(x,b0)
if(i==sqrt(b0)&&((int)(sqrt(b0)))*((int)(sqrt(b0)))==b0&&b0%(((int)(sqrt(b0))))==0){
x=bb*((int)(sqrt(b0)));
if(gcd(x,b0)==((int)(sqrt(b0)))&&gcd(x,a0)==a1) ans++;
continue ;
}
if(b0%i==0){
x=bb*i;
if(gcd(x,b0)==i&&gcd(x,a0)==a1) ans++;
x=bb*(b0/i);
if(gcd(x,b0)==b0/i&&gcd(x,a0)==a1) ans++;
}
}
write(ans);putchar('\n');
}
return 0;
}