Educational Codeforces Round 106 (Rated for Div. 2) D. The Number of Pairs
Title
Given three positive integers c, d, xc,d,xc,d,x , ask how many pairs of positive integers(a, b) (a, b)(a,b ) , satisfying
c ⋅ lcm − d ⋅ gcd = xc \cdot lcm-d \cdot gcd = xc⋅lcm−d⋅gcd=x
limit
1 ≤ T ≤ 1 0 4 1 \ le T \ le 10 ^ 4 1≤T≤104
1 ≤ c, d, x ≤ 1 0 7 1 \ le c, d, x \ le 10 ^ 7 1≤c,d,x≤107
Ideas
For the original formula
c ⋅ lcm − d ⋅ gcd = xc \cdot lcm-d \cdot gcd = xc⋅lcm−d⋅gcd=x
bygcd gcdg c d andlcm lcmL C m mathematical properties, it is easy to obtain a conclusion: LCM LCMl c m must begcd gcdThe multiple of g c d .
Then the above formula can be transformed into
lcm = x + d ⋅ gcdc lcm=\frac{x+d\cdot gcd}{c}lcm=cx+d⋅gcd
The right side must be an integer, that is x + d ⋅ gcd x+d\cdot gcdx+d⋅g c d must beccc multiple
Then we can O (x) O(\sqrt x)O (x) Enumxxall factors of x asgcd gcdgcd
Since c, d, xc,d,xc,d,x is known, if the above conditions are met,gcd, c, d, x gcd, c, d, xgcd,c,d,x directly calculate the matchedlcm lcmlcm
The problem has now been transformed into "After the known gcd gcdg c d andlcm lcmUnder the premise of l c m , find how many pairs of integers satisfy the condition"
From a mathematical point of view
g c d ( a , b ) gcd(a,b) gcd(a,b ) isa, ba, ba,the intersection of the prime factors of b
l c m ( a , b ) lcm(a,b) lcm(a,b ) isa, ba, ba,the union of the prime factors of b
Then lcmgcd \frac{lcm}{gcd}gcdlcmIs the product of the extra prime factors
For lcmgcd \frac{lcm}{gcd}gcdlcmOf each quality factor, regardless of the number , should be fully positioned agcd \ frac {a} {gcd }gcdaOr all of them are located in bgcd \frac{b}{gcd}gcdbin
Otherwise, these prime factors are in aaa andbbIf b appears at the same time, it will be counted asgcd gcdIn g c d , the situation does not match at this time
So if we want to construct a, ba, ba,b Two numbers, each prime factor can only pick one of them and multiply it in
设 l c m g c d \frac{lcm}{gcd} gcdlcmThe number of prime factors is nnn , the integer pairs that can be constructed have a total of2 n 2^n2n kinds
Practice can be obtained, the online prime factorization algorithm will be perfect TLE 15
So consider offline processing of the number of prime factor types that each number has
The writing method here is similar to the Ehrlich prime number sieve method, see init init in the code section for detailsi n i t function
注意 x g c d + d c \frac{\frac{x}{gcd}+d}{c} cgcdx+dIn theory, the maximum possible data range is 2 ⋅ 1 0 7 2\cdot 10^72⋅107
Code
(1075ms/2000ms)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e7;
int r[MAXN+50]; //记录每个数字的质因子种类数
void init()
{
for(int i=2;i<=MAXN;i++)
if(!r[i]) //判断i是否为素数
{
for(int j=i;j<=MAXN;j+=i)
r[j]++; //i在范围内的倍数的质因子种类数++
}
}
ll c,d,x,ans;
void add(int gcd)
{
ll lcm=x+d*gcd;
if(lcm%c==0)
{
lcm/=c;
if(lcm%gcd==0)
ans+=1LL<<r[lcm/gcd];
}
}
void solve()
{
ans=0;
cin>>c>>d>>x;
int s=sqrt(x);
for(int i=1;i<=s;i++)
{
if(x%i==0) //枚举x的因子i
{
add(i);
if(i*i!=x)
add(x/i);
}
}
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
init();
int T;cin>>T;while(T--)
solve();
return 0;
}