poj1338

本文提供了一种方法来计算给定序号n的丑数,即只包含质因数2、3、5的整数。
部署运行你感兴趣的模型镜像

【题意】

回答第n个丑数是多少

丑数即为质因数只有2、3、5的数

【输入】

多次询问,询问以一个0结束

每次询问只有一个n

【输出】

回答每次询问


这个预处理一下前1500个丑数,回答的时候直接回答就好了

我挂了个堆来生成丑数,实际上没必要……


program poj1338;
var
  all,tot,n,i,j,k:longint;
  ans:array [0..1501] of int64;
  heap:array [0..10001] of int64;

procedure swap (var a,b:int64)inline;
var
  i:int64;
begin
  i:=a;
  a:=b;
  b:=i;
end;

procedure insert (now:int64);inline;
var
  i:longint;
begin
  inc(tot);
  heap[tot]:=now;
  i:=tot;
  while (i>1)and(heap[i]<heap[i div 2]) do
    begin
      swap(heap[i],heap[i div 2]);
      i:=i div 2;
    end;
end;

procedure delete;inline;
var
  i:longint;
begin
  if tot=1 then
    begin
      heap[1]:=heap[0];
      dec(tot);
      exit;
    end;
  swap(heap[1],heap[tot]);
  heap[tot]:=heap[0];
  dec(tot);
  i:=1;
  while (heap[i*2]<heap[i])or(heap[i*2+1]<heap[i]) do
    if heap[i*2]<heap[i*2+1] then
      begin
        swap(heap[i],heap[i*2]);
        i:=i*2;
      end
                             else
      begin
        swap(heap[i],heap[i*2+1]);
        i:=i*2+1;
      end;
end;

begin
  fillchar(heap,sizeof(heap),63);
  tot:=0;
  ans[1]:=1;
  insert(2);
  insert(3);
  insert(5);
  for i:=2 to 1500 do
    begin
      while heap[1]=ans[i-1] do delete;
      ans[i]:=heap[1];
      delete;
      insert(ans[i]*2);
      insert(ans[i]*3);
      insert(ans[i]*5);
    end;
  repeat
    read(n);
    if n=0 then break;
    writeln(ans[n]);
  until false;
end.


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值