跳到正文

题解

题解 P1072 【Hankson 的趣味题】

aha浮云约 4 分钟
原文
文章信息与版本

文章信息

发布
2018年11月6日
收录
2026年8月27日
更新
2026年11月9日
篇幅
约 4 分钟
题目
P1072

版本

  1. v1当前

话说大家暴力都会吧

(暴力这东西真的难说)我最开始裸暴力70pts

QAQ 然后难受啊

就想怎么优化一下暴力

暴力出奇迹嘛

本来的暴力是这样的

C++
#include<bits/stdc++.h>
#define R register 
#define in inline
using namespace std; 
const long long max_n = 200010;
long long n, a0, a1, b0, b1;
long long factor[max_n], tot;
long long Gcd(long long a, long long b)
{
    if(!b) return a;
    else return Gcd(b, a % b);
}
int main()
{
    cin >> n;
    while(n --)
    {
        long long cnt = 0;
        tot = 0;
        scanf("%lld%lld%lld%lld", &a0, &a1, &b0, &b1);
        for(R long long i = 1;i <= sqrt(b1); i++){
            if(b1 % i == 0) factor[++ tot] = i;
            if(i != b1 / i) factor[++ tot] = b1 / i;
        }
        for(R long long i = 1;i <= tot; i++)
        if(factor[i] * b0 / Gcd(factor[i], b0) == b1 && Gcd(factor[i], a0) == a1)
        cnt ++;
        printf("%lld\n", cnt);
    }
    return 0;
}

然后我好像想到1e9里面约数个数最多的也才1000多个

我这样暴力岂不是很浪费

然后我就分解质因数

然后直接上dfs 美妙的就过了(我不会说dfs还调了我1h)

!!暴力出奇迹!!

Code

C++
#include<bits/stdc++.h>
#define R register 
#define in inline
using namespace std; 
const long long max_n = 200010;
long long n, a0, a1, b0, b1, cnt, m, temp, tot;
int p[max_n], c[max_n], v[max_n];
void dfs(long long now, int pos, int num)
{
    if(pos == m + 1)
    {
        if(__gcd(now, a0) == a1 && now * b0 / __gcd(now, b0) == b1)
    		cnt ++;
    	return;
    }
    if(now > b1) return;
    if(num > c[pos]) return;
    dfs(now, pos + 1, 0);
    dfs(now * p[pos], pos, num + 1);
}
int main()
{
    cin >> n;
    while(n --)
    {
        cnt = 0, m= 0;
        scanf("%lld%lld%lld%lld", &a0, &a1, &b0, &b1);
        temp = b1;
        for(R int i = 2;i <= sqrt(b1); i++)
        {
            if(b1 % i == 0) p[++ m] = i;
            while(b1 % i == 0)
            c[m] ++, b1 /= i;
        }
        if(b1 > 1) p[++ m] = b1, c[m] ++;
        b1 = temp;
        dfs(1, 1, 0);
        printf("%lld\n", cnt);
    }
    return 0;
}
原文