• 火车进出栈问题(强化版)


    描述 Description
    一列火车n节车厢,依次编号为1,2,3,…,n。每节车厢有两种运动方式,进栈与出栈,问n节车厢出栈的可能排列方式有多少种。
     
     
    输入格式 InputFormat
    一个数,n(n<=50000)
     
     
    输出格式 OutputFormat
    一个数s表示n节车厢出栈的可能排列方式
     
     
    样例输入 SampleInput 
     
     

    样例输出 SampleOutput

     
    计算卡特兰数的第N项。
    先分解质因数(注意同时直接求出P^i),然后高精度乘。
    卡常数,自己试试吧
     
     
    #01: Accepted (0ms, 1316KB)
    #02: Accepted (0ms, 1316KB)
    #03: Accepted (0ms, 1316KB)
    #04: Accepted (0ms, 1316KB)
    #05: Accepted (0ms, 1316KB)
    #06: Accepted (0ms, 1316KB)
    #07: Accepted (0ms, 1316KB)
    #08: Accepted (0ms, 1316KB)
    #09: Accepted (0ms, 1316KB)
    #10: Accepted (6ms, 1316KB)
    #11: Accepted (0ms, 1316KB)
    #12: Accepted (0ms, 1316KB)
    #13: Accepted (0ms, 1316KB)
    #14: Accepted (0ms, 1316KB)
    #15: Accepted (0ms, 1316KB)
    #16: Accepted (0ms, 1316KB)
    #17: Accepted (0ms, 1316KB)
    #18: Accepted (68ms, 1316KB)
    #19: Accepted (84ms, 1316KB)
    #20: Accepted (146ms, 1316KB)
    #21: Accepted (303ms, 1316KB)
    #22: Accepted (381ms, 1316KB)
    #23: Accepted (475ms, 1316KB)
    #24: Accepted (678ms, 1316KB)

    Accepted / 100 / 2143ms / 1316KB
     
    program stack_new;
    
    uses math;
    
    Const
     maxlen=8400;
     bit=10;
     mbit=10000000000;
     size=8;
    
    Type
     gjd=record
       d:array[0..maxlen] of qword;
         len:longint;
         zf:boolean;
     end;
    
     TTable=array[0..100000] of longint;
    
    Var
     a:TTable;
     ct,n,i,j:longint;
     x,y,z,s,p:qword;
     ans:gjd;
    
    
    Procedure print(p:gjd);forward;
    
    Function len(P:qword):longint;
    var
     s:string;
      begin
      str(p,s);
      exit(length(s));
    end;
    
    Function initgjd(var p:gjd;k:qword):gjd;//初始化gjd置为k
    
      begin
        p.len:=0;
        fillchar(p.d,(len(k) div mbit*2+2)*size,0);
        p.zf:=true;
    
        while k>0 do
          begin
          inc(p.len);
          p.d[p.len]:=k mod mbit;
          k:=k div mbit;
        end;
        exit(p);
     end;
    
    Function initgjd(var p:gjd):gjd;//无参数默认为0
      begin
        p.len:=1;
        fillchar(p.d,sizeof(p.d),0);
        exit(p);
    end;
    
    Procedure print(p:gjd);//输出一行高精度
    var
     i,j:longint;
      begin
        if not p.zf then write('-');
        for i:=p.len downto 1 do
          begin
          if i<>p.len then for j:=1 to bit-len(p.d[i]) do write(0);
          write(p.d[i]);
        end;
    end;
    
    procedure fopen;
      begin
      assign(input,'stack.in');
      assign(output,'stack.out');
      reset(input);
      rewrite(output);
    end;
    
    procedure fclose;
      begin
      close(input);
      close(output);
    end;
    
    Procedure GetTable;
    var
     v:array[0..110000] of boolean;
     i,j:longint;
      begin
      fillchar(v,sizeof(v),true);
      v[1]:=false;
      v[2]:=true;
      ct:=0;
      for i:=2 to trunc(sqrt(n*2)) do
        if v[i] then
          begin
          inc(ct);
          a[ct]:=i;
          for j:=2 to n*2 div i do
            v[i*j]:=false;
        end;
      for i:=trunc(sqrt(n*2))+1 to n*2 do
        if v[i] then begin inc(ct);a[ct]:=i; end;
    
    
    end;
    
    
    
      begin
    
      readln(n);
      gettable;
    
     { for i:=1 to ct do
        if fn2[i]>0 then
          writeln(a[i],' ',fn2[i]);     }
      initgjd(ans,1);
      for i:=1 to ct do
          begin
          x:=2*n;y:=n;z:=n+1;s:=1;p:=a[i];
          while x>0 do
            begin
            x:=x div p;y:=y div p;z:=z div p;
            if x>y+z then s:=s*p;
          end;
          if s=1 then continue;
          // writeln(s);
          //writeln('i=',i,' a[i]=',a[i],' ^ ',fn2[i]);
         for j:=1 to ans.len do
           ans.d[j]:=ans.d[j]*s;
         for j:=1 to maxlen do
           begin
           ans.d[j+1]:=ans.d[j+1]+ans.d[j] div mbit;
           ans.d[j]:=ans.d[j] mod mbit;
           if (j>ans.len) and (ans.d[j+1]=0) then begin ans.len:=j;break;end;
         end;
         while (ans.d[ans.len]=0)  and (ans.len>1) do dec(ans.len);
    
    
         // print(ans);
          //writeln;
        end;
      print(ans);
    
    end.
    
  • 相关阅读:
    iscsi: 多路径
    Paxos算法分析
    ceph实践: 搭建环境
    ocfs2: 搭建环境
    设计模式:Context模式
    Ceph剖析:Leader选举
    Ceph剖析:定时器safetimer的实现
    nfs:环境搭建
    Ceph剖析:数据分布之CRUSH算法与一致性Hash
    Linux命令小结:crontab/netstat/iostat/sar
  • 原文地址:https://www.cnblogs.com/htfy/p/2738585.html
Copyright © 2020-2023  润新知