HDU 4497 GCD and LCM (分解质因数)

来源:互联网 发布:e4a操作数据库 编辑:程序博客网 时间:2024/05/17 20:56
链接 :


http://acm.hdu.edu.cn/showproblem.php?pid=4497

如果G不是L的约数 就不可能找到三个数。

L的所有素因子一定包含G的所有素因子 并且次方数一定大于等于G的。只需要三个数 对于每一个素因子的次方数 三个的最小值是G的,最大值是L的。考虑三个对应的次方数都不一样,那么其中两个是确定的 一个是G的一个是L的 剩下的一个在G和L的之间。算上排列 总共有6种。或者其中两个是一样的,那么也有6种情况。最后可以合并计算。


//#pragma comment(linker, "/STACK:10240000,10240000")#include <algorithm>#include <iostream>#include <sstream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>#include <cmath>#include <queue>#include <stack>#include <set>#include <map>#define mod 4294967296#define MAX 0x3f3f3f3f#define lson o<<1, l, m#define rson o<<1|1, m+1, r#define SZ(x) ((int)ans.size())#define MAKE make_pair#define INFL 0x3f3f3f3f3f3f3f3fLL#define mem(a) memset(a, 0, sizeof(a))const double pi = acos(-1.0);const double eps = 1e-9;const int N = 200005;const int M = 20005;typedef __int64 ll;using namespace std;ll a, b;struct C {    ll num, cnt;} s[N], t[N];int T;int Ini(ll a, C* f) {    int tmp = sqrt(1.0*a + 0.5), e = 0;    for(int i = 2; i <= tmp; i++) {        if(a % i == 0) {            int k = 0;            while(a % i == 0) {                k++;                a /= i;            }            f[e].num = i;            f[e++].cnt = k;        }    }    if(a != 1) {        f[e].cnt = 1;        f[e++].num = a;    }    return e;}int main()  {    //freopen("in.txt","r",stdin);    cin >> T;    while(T--) {        cin >> a >> b;        if(b % a) {            puts("0");            continue;        }        mem(s);        mem(t);        int n = Ini(a, s);        int m = Ini(b, t);        ll ans = 1, x;        int fr = 0;        for(int i = 0; i < m; i++) {            if(t[i].num == s[fr].num) {                x = t[i].cnt - s[fr].cnt;                fr++;                if(x) ans *= x * 6;            } else ans *= t[i].cnt * 6;        }        cout << ans << endl;    }    return 0;}

1 0
原创粉丝点击