codevs天梯 最大公約數與最小公倍數 水題

題目描述 Description

輸入二個正整數x0,y0(2<=x0<100000,2<=y0<=1000000),求出滿足下列條件的P,Q的個數

條件:  1.P,Q是正整數

2.要求P,Q以x0爲最大公約數,以y0爲最小公倍數.

試求:滿足條件的所有可能的兩個正整數的個數.




代碼:

var

  x,y,sum,temp,tot,t,z:longint;


function gcd(a,b:longint):longint;
var
  c:longint;
begin
  repeat
    c:=a mod b;
    a:=b;
    b:=c;
  until c=0;
  gcd:=a;
end;


begin
  readln(x,y);
  sum:=x*y;
  for temp:=x to y do
    begin
      tot:=sum div temp;
      z:=gcd(temp,tot);
      if z=x then
        if temp*tot div z=y then
          inc(t);
   end;
  writeln(t);
end.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章