题目: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.
input
3 12
output
3
题意:题目要求是是否存在一个这样的X满足:
GCD(X, Y1) = A
LCM(X, Y2) = B
上面这个两个条件。
分析:
可以看出X是A的倍数,是B的因子,那么根据这个已知信息我们可以将B的所有的因子求出(这里的B已经是除完A的B了,当然前提
是需要整除A,否则就输出0),这里可以根据唯一分解定理去将其所有的因子求出,对B/A得到的数分解质因子,设质因子的个数为a1,a2,...,an,则答案为(a1+1)(a2+1)...(an+1);
那么此时就存在一个问题就是这个B很大,解质因子的时间复杂度为O(sqrt(n)),如何去解决其因子呢?
我们可以先将1e6内的因子求出,这个复杂度还是可以接受的,那么剩下的B在除玩其因子后,如果其还是大于1的话
一定是大于1e6的数了,那么就有三种情况出现:
1.除完因子的B是一个素数,如果是答案就是需要在原来的基础上*2,即ans*=2;(乘上这个数或者不乘这个数嘛,两种情况)
2.除完因子的B是一个合数,并且这个数是一个由两个相同的素数的乘积构成的,那么ans*=3;
3.除完因子的B是一个合数,并且这个数是一个由两个不相同的素数的乘积构成的,那么ans*=4;
其中判断一个大整数是不是素数可以用Miller-Rabin算法来判断
#define cin0 ios::sync_with_stdio(0),cin.tie(0);
#define ull unsigned long long
#define inf 0x3f3f3f3f
#define ll long long
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <bitset>
#include <vector>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int man=1e5+50;
const ll mod=1000000007;
ll add_mod(ll a,ll b,ll mod){
ll ans=0;
while(b){
if(b&1)
ans=(ans+a)%mod;
a=a*2%mod;
b>>=1;
}
return ans;
}
ll pow_mod(ll a,ll n,ll mod){
if(n>1){
ll tmp=pow_mod(a,n>>1,mod)%mod;
tmp=add_mod(tmp,tmp,mod);
if(n&1) tmp=add_mod(tmp,a,mod);
return tmp;
}
return a;
}
bool Miller_Rabbin(ll n,ll a){
ll d=n-1,s=0,i;
while(!(d&1)){
d>>=1;
s++;
}
ll t=pow_mod(a,d,n);
if(t==1 || t==-1)
return 1;
for(i=0;i<s;i++){
if(t==n-1)
return 1;
t=add_mod(t,t,n);
}
return 0;
}
bool is_prime(ll n){
ll i,tab[4]={3,4,7,11};
for(i=0;i<4;i++){
if(n==tab[i])
return 1;
if(!n%tab[i])
return 0;
if(n>tab[i] && !Miller_Rabbin(n,tab[i]))
return 0;
}
return 1;
}
int main()
{
ll A,B;
scanf("%lld%lld",&A,&B);
if(B%A==0){
B/=A;
ll ans=1,cnt=0;
for(int i=2;i<1000000;i++){
if(B%i==0){
cnt=1;
while(B%i==0){
cnt++;
B/=i;
}
ans*= cnt;
}
}
if(B>1){
if(is_prime(B))
ans*=(ll)2;
else{
ll t=sqrt(B);
if(t*t==B)
ans*=(ll)3;
else
ans*=(ll)4;
}
}
printf("%lld\n",ans);
}else printf("0\n");
return 0;
}