Hankson的趣味题
首先,根据lcm(b,x)=d得出x一定是d的约数,但通过试除法求出d的所有约数时间过长
考虑到n次询问均在2 *1e9范围内,选择预处理出1~sqrt(2 *1e9)的所有质数,然后通过递归深搜算法,得到d1的所有约数,再进行判定
这道题,还卡int,如果用int的话只能过6/11(别问我怎么知道的)
#include <bits/stdc++.h>
using namespace std;
long long ans; // 答案展示
long long n, a0, a1, b0, b1; // 题目要求
long long v[200000];//在用埃氏筛法得到2~sqrt(2e9)时用到的数组
vector<long long> sushu, sushu_t;
vector<long long> cnt, cnt_t;//sushu是存埃氏筛法的结果,_t则是,存每次询问中d1对应的素数及其数量
long long gcd(long long a, long long b)//求最大公约数版子
{
return b ? gcd(b, a % b) : a;
}
bool jisuan(long long val)//判断是否符合条件
{
if (gcd(val, a0) == a1 && val * b0 / gcd(val, b0) == b1)//根据最大公约数定理,可得a*b/gcd(a,b)=lcm(a,b)
return true;
return false;
}
void dfs(long long now, long long val)//now指当前递归到第几个素数,val指,前面叠加的结果
{
if (now == sushu_t.size())//当递归结束时的判定
{
// cout << val << '\n';
if (jisuan(val))
ans++;
return;
}
long long t = 1;
for (long long i = 0; i <= cnt_t[now]; i++)
{
dfs(now + 1, val * t);
t *= sushu_t[now];
// cout << t << '\n';
}
}
int main()
{
n = sqrt(2000000000);//这里,懒得定义一个变量了,直接用的n,反正换不换都没影响
for (long long i = 2; i < n; i++)//埃氏筛法
{
if (v[i])
continue;
sushu.push_back(i);
for (long long j = i; j <= n / i; j++)
v[i * j] = i;
}
cnt.resize(sushu.size());
cin >> n;
while (n--)
{
ans = 0;
cin >> a0 >> a1 >> b0 >> b1; // x是b1的约数
long long t = b1;
sushu_t.clear();
cnt_t.clear();
sushu_t.resize(0);
cnt_t.resize(0);//这两个resize没什么用,当时debug的时候,有病乱投医
for (long long i = 0; i < sushu.size(); i++)
{
cnt[i] = 0;
while (t % sushu[i] == 0)
{
t /= sushu[i];
cnt[i]++;
}
if (cnt[i])
{
//cout << sushu[i] << ' ' << cnt[i] << '\n';
sushu_t.push_back(sushu[i]);
cnt_t.push_back(cnt[i]);
}
}//这个for循环是为了得到d1对应的素数及其数量
// for (long long i = 0; i < sushu_t.size(); i++)
// cout << sushu_t[i] << ' ' << cnt_t[i] << '\n';
if (t != 1)
{
sushu_t.push_back(t);
cnt_t.push_back(1);
}//这里,额,我也不知道有没有用,但,不确定,美称,增加鲁棒性(反正加了也无所谓)
dfs(0, 1);//初始状态的深搜
cout << ans << '\n';
}
}