题解 P1029 【最大公约数和最小公倍数问题】
题目名称:最大公约数和最小公倍数问题
来源:2001年NOIP普及组
链接
博客链接
- 博客园
- CSDN
- 洛谷博客
- 洛谷题解
题目链接
- Vijos
- 洛谷(P1029)
- 入门OJ(1172)
题解
约定
情况1:
显然,无解。
情况2:
推一波
当然,暴力枚举
现将
所以说其中每种质因子都有两种可能,则答案是
时间复杂度
//C++
#include<bits/locale_facets.h>
#include<stdio.h>
#define forto(name,i,d,u) for(name i=d;i<=u;i++)
inline void output(long long o);
inline long long input();
int main()
{
short numeral=0;
int x=input(),y=input();
if(y%x)return putchar('0'),0;
y/=x;
forto(int,i,2,y/i)
if(!(y%i))
{
numeral++;
while(!(y%i))y/=i;
}
if(y>1)numeral++;
output(1<<numeral);
return 0;
}
inline void output(long long o)
{
if(o<0)putchar('-'),o=-o;
if(o>=10)output(o/10);
putchar(o%10^'0');
}
inline long long input()
{
bool minus=false;
char now=getchar();
long long i=0;
for(;!isdigit(now);now=getchar())
if(now=='-')minus=!minus;
for(;isdigit(now);now=getchar())i=(i<<3)+(i<<1)+(now^'0');
return minus?-i:i;
}
//pascal
var
numeral:1..30;
x:2..100000;
i,y:1..1000000;
begin
readln(x,y);
if y mod x>0 then
begin
write('0');
halt;
end;
y:=y div x;
i:=2;
while i<=y div i do
begin
if y mod i=0 then
begin
inc(numeral);
while y mod i=0 do y:=y div i;
end;
inc(i);
end;
if y>1 then inc(numeral);
write(1 shl numeral);
end.