洛谷1072 Hankson 的趣味题

来源:互联网 发布:苹果主屏有什么软件 编辑:程序博客网 时间:2024/05/17 02:30

https://www.luogu.org/problem/show?pid=1072 Hankson 的趣味题

正解:素数筛+因数分解+乘法原理。简单地来说就是我不会!

但是考虑:

1.从b1入手,如果枚举x只需枚举到sqrt(b1),大的部分直接用b1/x即可。

2.考虑求gcd是log级别的,一个数的约数个数也是log级别的,所以枚举+判断的复杂度大概O(n(b1+log2b1))=O(nb1),b1范围2e9,运算量差不多2e920001e8,但是可以加一些像x是不是a1倍数之类的特判,跑不满。

3.洛谷之前的王牌评测姬“香港记者号”虽退役,但还继承其跑得快精神。

综合以上三点。。。

什么叫暴力出奇迹?这就是。

代码:

#include <bits/stdc++.h>using namespace std;typedef long long LL;int a0, a1, b0, b1;int gcd (int a, int b) {return (!b)? a: gcd (b, a%b);}bool jd (int x) {    if (x%a1) return 0;    if (gcd (x/a1, a0/a1) != 1) return 0;    if ((LL)gcd (x, b0)*b1 != (LL)x*b0) return 0;    return 1;}int main () {    int t; scanf ("%d", &t);    while (t--) {        scanf ("%d%d%d%d", &a0, &a1, &b0, &b1);        int ans = 0, stb = sqrt (b1);        for (int x = 1; x <= stb; ++ x) {            if (b1%x) continue;            if (jd (x)) ++ ans;            if (b1/x != x && jd (b1/x)) ++ ans;        }        printf ("%d\n", ans);    }    return 0;}