Solution
If you can focus on the two-information G c d ( x , a 0 ) = a 1 , L c m ( x , b 0 ) = b 1 Gcd(x,a0) = a1, Lcm(x, b0) = b1 Gcd(x,a0)=a1,Lcm(x,b0)=b1, it would be very hard…
A vital hint is x ∣ b 1 x | b1 x∣b1, so the solution actually very simple:
int ans = 0;
for( x : $(all Divisors of `b1`){
if( Gcd(x, a0) == a1 && Lcm(x, b0) == b1){
++ ans;
}
}
Cuz the Divisor-Count of int
at most
1600
1600
1600, the Time-Cost of this method is negligible.
The only thing is, how you get All-Divisors for a int
;
+
If the familiar-mean is
O
(
N
)
=
5
e
4
O(\sqrt{N}) = 5e4
O(N)=5e4,
T
(
2000
)
∗
5
e
4
T(2000) * 5e4
T(2000)∗5e4 would Out-Of-Time;
+
Here we use another-mean for get All-Divisors:
.
1
Get the Prime-Factorization using a special-algorithm:
O
(
N
ln
N
)
+
O
(
N
)
O(\frac{\sqrt{N}}{\ln{\sqrt{N}}}) + O(\sqrt{N})
O(lnNN)+O(N) (it is a Template-Algorithm)
.
.
It resembles to the Normal-Method for( i = 2; i <= x / i; ++i)
, while in here,
i
i
i will be replaced by all Prime-Numbers in the range
≤
x
\leq \sqrt{x}
≤x;
.
2
Cuz we have already get the Prime-Factorization of this number, we can use DFS exhaustedly to build all its Divisors; (the Time-Cost equals to the Divisor-Count
1600
1600
1600)
Finally, the Time-Cost is 2000 ∗ ( O ( N ln N ) + 1600 + 1600 ∗ ln N ) + O ( N ) = 2000 ∗ O ( N ln N ) ( 5 e 3 ) = = 1 e 7 2000 * ( O(\frac{\sqrt{N}}{\ln{\sqrt{N}}}) + 1600 + 1600 * \ln{N}) + O(\sqrt{N}) = 2000 * O(\frac{\sqrt{N}}{\ln{\sqrt{N}}})(5e3) = = 1e7 2000∗(O(lnNN)+1600+1600∗lnN)+O(N)=2000∗O(lnNN)(5e3)==1e7
Code
Core-Episode (Prime-Factorization in O ( N ln N ) O(\frac{\sqrt{N}}{\ln{\sqrt{N}}}) O(lnNN))
{ //< @Snippet, Primes_Factorization
auto temp = b1;
for( int power, ind = 0; ind < P->Get_primesCount(); ++ind){
auto prime = P->Primes[ ind];
if( prime > temp / prime){ break;}
if( temp % prime == 0){
power = 0;
while( temp % prime == 0){
++ power;
temp /= prime;
}
PrimeFactors[ M ++] = {prime, power};
}
}
if( temp > 1){
PrimeFactors[ M ++] = {temp, 1};
}
}
Use DFS(O(1600)) to build all-Divisors according to @Name(PrimeFactors);