GCDLCM(数学)

题目:GCDLCM——http://icpc.upc.edu.cn/problem.php?cid=1745&pid=3

题目描述

In FZU ACM team, BroterJ and Silchen are good friends, and they often play some interesting games.
One day they play a game about GCD and LCM. firstly BrotherJ writes an integer A and Silchen writes an integer B on the paper. Then BrotherJ gives Silchen an integer X. Silchen will win if he can find two integers Y1 and Y2 that satisfy the following conditions:
• GCD(X, Y1) = A
• LCM(X, Y2) = B
• Fuction GCD(X, Y ) means greatest common divisor between X and Y .
• Fuction LCM(X, Y ) means lowest common multiple between X and Y .
BrotherJ loves Silchen so much that he wants Silchen to win the game. Now he wants to calculate how many number of X he can give to Silchen.

 

输入

Input is given from Standard Input in the following format:
A B
Constraints
1 ≤ A, B ≤ 1018
Both A and B are integers.

 

输出

Print one integer denotes the number of X.

 

样例输入

复制样例数据

3 12

样例输出

3

题意:

求满足条件 x%A == 0 且 B%x == 0的正整数x的个数。

思路:

先看B%A是否为0,如果为0,对B/A得到的数分解质因子,设质因子的个数为a1,a2,...,an,则答案为(a1+1)(a2+1)...(an+1)。由于分解质因子的时间复杂度为O(sqrt(n)),题目范围为1e18,所以不能直接分解,可以先把1e6内的质因子找出,用n除去,如果剩下的n>1,那么一定有n>1e6,此时有几种情况:1、n为素数,ans*=2;2、n为两个相同的素数之积,ans*=3;3、n为两个不同的素数之积,ans*=4。需要用到判断大素数。Miller-Rabin算法。
或者直接用Pollard-Rho分解出素因子。

代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std ;
#define rd(x) (rand()%(x))
typedef unsigned long long ll;
const int maxn = 2e6+5;

ll mod_mul(ll a, ll b, ll n) {
    ll res = 0;
    while (b) {
        if(b & 1)
            res = (res + a) % n;
        a = (a + a) % n;
        b >>= 1;
    }
    return res;
}

ll pow_mod(ll a, ll b, ll n) {
    ll res = 1;
    while(b) {
        if(b & 1)
            res = mod_mul(res, a, n);
        a = mod_mul(a, a, n);
        b >>= 1;
    }
    return res;
}

bool test(ll n,ll a,ll d)
{
    if(n==2) return true;
    if(n==a) return false;
    if(!(n&1)) return false;
    while(!(d&1)) d>>=1;
    ll t = pow_mod(a,d,n);
    while(d!=n-1&&t!=n-1&&t!=1){
        t = t*t%n;
        d<<=1;
    }
    return t == n-1||(d&1)==1;
}

bool isprime(ll n)
{
    int a[] = {2,3,5,7, 11,13,17,61,24251};
    for(int i = 0; i < 9; ++i){
        if(n==a[i]) return true;
        if(!test(n,a[i],n-1)) return false;
    }
    return true;
}

ll prime[maxn];
void getprime(){
    memset(prime, 0, sizeof(prime));
    for(int i=2; i<=maxn; i++){
        if(!prime[i]) prime[++prime[0]] = i;
        for(int j=1; j<=prime[0]&&prime[j]<=maxn/i; j++){
            prime[prime[j]*i] = 1;
            if(i%prime[j] == 0) break;
        }
    }
}
void work(ll n){
    ll sum = 1;
    for(int i=1; i<=prime[0]; i++){
        ll cnt=0;
        while(n%prime[i]==0) n/=prime[i], cnt++;
        sum *= cnt+1;
    }
    if(n>1){
        if(isprime(n)) sum *= 2;
        else if((ll)sqrt(n)*(ll)sqrt(n) == n) sum *= 3;
        else sum *= 4;
    }
    cout << sum << endl;
}
int main()
{
    getprime();
    ll n, m;
    int t,ans=0;
    while(cin >> n >> m){
        if(m%n!=0) cout << "0" << endl;
        else if(m==n) cout << "1" << endl;
        else work(m/n);
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值