用质因子去分解质因数
程序员文章站
2022-06-07 17:02:29
...
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 50010;
int a, b, c, d;
int prime[N], cnt;
bool vis[N];
pair <int, int > factor[N];
int fcnt;
int res;
void init(int n)
{
for(int i = 2; i <= n; ++ i)
{
if(!vis[i]) prime[cnt ++] = i;
for(int j = 0;j < cnt && i * prime[j] <= n; ++ j)
{
vis[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
}
void dfs(int deep, int p)
{
if(deep == fcnt)
{
if( __gcd(p,a) == b && p / __gcd(p,c) * c == d) res ++;
return;
}
for(int i = 0; i <= factor[deep].s; ++ i)
{
dfs(deep + 1,p);
p *= factor[deep].f;
}
}
int main()
{
init(N - 1);
int T;
cin >> T;
while(T --)
{
cin >> a >> b >> c >> d;
int t = d;
fcnt = 0;
for(int i = 0; prime[i] * prime[i] <= t; ++ i)
{
if(t % prime[i] == 0)
{
int b = 0;
while(t % prime[i] == 0) t /= prime[i], b ++;
factor[fcnt ++] = {prime[i],b};
}
}
if(t != 1) factor[fcnt ++] = {t,1};
dfs(0,1);
cout << res << endl;
res = 0;
}
return 0;
}