Hướng dẫn giải của Setnja


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

const base=100000000;
      digit=8;
type bignum=array[0..875] of longint;
var num,re,temp:bignum;

procedure plus(var c:bignum;a,b:bignum);
var i,max,mem:longint; 
begin
     mem:=0;
     if a[0]>b[0] then max:=a[0] else max:=b[0];
     for i:=1 to max do
     begin
          c[i]:=a[i]+b[i]+mem;
          if c[i]<base then mem:=0
          else
          begin
               c[i]:=c[i]-base; mem:=1;
          end;
     end;
     if mem>0 then
     begin
          max:=max+1;
          c[max]:=mem;
     end;
     c[0]:=max;
end;

procedure plus1(var c:bignum;a,b,d:bignum);
var i,max,mem:longint; 
begin
     mem:=0;
     if a[0]>b[0] then max:=a[0] else max:=b[0];
     if max<d[0] then max:=d[0];
     for i:=1 to max do
     begin
          c[i]:=a[i]+b[i]+d[i]+mem;
          if c[i]<base then mem:=0
          else
          begin
               mem:=c[i] div base; c[i]:=c[i] mod base; 
          end;
     end;
     if mem>0 then
     begin
          max:=max+1;
          c[max]:=mem;
     end;
     c[0]:=max;
end;

procedure multi(var c:bignum;a:bignum;b:longint);
var i,max,mem:longint; 
begin
     mem:=0; max:=a[0];
     for i:=1 to max do
     begin
          c[i]:=a[i]*b+mem;
          if c[i]<base then mem:=0
          else
          begin
               mem:=c[i] div base; c[i]:=c[i] mod base; 
          end;
     end;
     if mem>0 then
     begin
          max:=max+1;
          c[max]:=mem;
     end;
     c[0]:=max;
end;

procedure mul2(var c:bignum;a:bignum;b:longint);
var i,max,mem:longint; 
begin
     mem:=0; max:=a[0];
     for i:=1 to max do
     begin
          c[i]:=a[i] shl b+mem;
          if c[i]<base then mem:=0
          else
          begin
               mem:=1; c[i]:=c[i]-base; 
          end;
     end;
     if mem>0 then
     begin
          max:=max+1;
          c[max]:=mem;
     end;
     c[0]:=max;
end;

procedure multi1(var c:bignum;a:bignum;b:longint;d:bignum);
var i,max,mem:longint; 
begin
     mem:=0; 
     if a[0]>=d[0] then max:=a[0] else max:=d[0];
     for i:=1 to max do
     begin
          c[i]:=a[i]*b+d[i]+mem;
          if c[i]<base then mem:=0
          else
          begin
               mem:=c[i] div base; c[i]:=c[i] mod base; 
          end;
     end;
     if mem>0 then
     begin
          max:=max+1;
          c[max]:=mem;
     end;
     c[0]:=max;
end;

procedure mul21(var c:bignum;a:bignum;b:longint;d:bignum);
var i,max,mem:longint; 
begin
     mem:=0; 
     if a[0]>=d[0] then max:=a[0] else max:=d[0];
     for i:=1 to max do
     begin
          c[i]:=a[i] shl b+d[i]+mem;
          if c[i]<base then mem:=0
          else
          begin
               mem:=c[i] div base; c[i]:=c[i] mod base; 
          end;
     end;
     if mem>0 then
     begin
          max:=max+1;
          c[max]:=mem;
     end;
     c[0]:=max;
end;

procedure star;
var i:longint;
begin
     multi1(re,re,5,num);
     multi(num,num,3);
end;

procedure rf;
var c:char;
begin
     num[0]:=1; re[0]:=1; re[1]:=1; num[1]:=1;
     while not eoln do
     begin
          read(c);
          if c='P' then continue;
          if c='*' then star
          else
          begin
               if c='L' then mul2(re,re,1) else mul21(re,re,1,num);
          end;
     end;
end;

procedure wf;
var i,j,t:longint; s:string;
begin
     for i:=re[0] downto 1 do
     begin
          if i<re[0] then
          begin
               str(re[i],s);
               t:=length(s);
               for j:=t+1 to digit do write(0);
          end;
          write(re[i]);
     end;
end;

begin
     rf;
     wf;
end.

Code mẫu của ladpro98

#include <bits/stdc++.h>
const int MOD = 100000000;
const int N = 10010;
using namespace std;
typedef vector<int> big;
char s[N];

big operator + (big a, big b) {
    big c; int carry = 0;
    for(int i = 0; i < a.size() || i < b.size(); i++) {
        if (i < a.size()) carry += a[i];
        if (i < b.size()) carry += b[i];
        c.push_back(carry % MOD);
        carry /= MOD;
    }
    if (carry) c.push_back(carry);
    return c;
}

big operator * (big a, int b) {
    big c; int carry = 0;
    for(int i = 0; i < a.size(); i++) {
        carry += a[i] * b;
        c.push_back(carry % MOD);
        carry /= MOD;
    }
    if (carry) c.push_back(carry);
    return c;
}

void print(big a) {
    printf("%d", a[a.size() - 1]);
    for(int i = a.size() - 2; i >= 0; i--)
        printf("%08d", a[i]);
    printf("\n");
}

int main() {
    scanf("%s", &s);
    int n = strlen(s);
    big F; F.push_back(1);
    big aster; aster.push_back(1);
    for(int i = 0; i < n; i++) {
        if (s[i] == 'L') F = F + F; else
        if (s[i] == 'R') F = F + F + aster; else
        if (s[i] == '*') {
            F = (F * 5) + aster;
            aster = aster * 3;
        }
    }
    print(F);
    return 0;
}

Code mẫu của RR

//Written by RR
{$ifdef rr}
  {$r+,q+}
  {$mode objfpc}
  {$inline off}
{$else}
  {$r-,q-}
  {$mode objfpc}
  {$inline on}
{$endif}

uses math;
const
  FINP          =       '';
  FOUT          =       '';
  scs           =       400;
  base          =       1000000000000000000;
  lbase         =       18;
type
  big           =       array[0..scs] of qword;
var
  f1,f2         :       text;
  s             :       ansistring;
  n             :       longint;
  skn,val       :       big;

procedure openF;
    begin
      assign(f1,FINP); reset(f1);
      assign(f2,FOUT); rewrite(f2);
    end;
procedure closeF;
    begin
      close(f1);
      close(f2);
    end;
procedure inp;
    begin
      readln(f1,s);
      n:=length(s);
    end;
procedure mul2(var a:big); inline;
    var
      i,nho:longint;
    begin
      nho:=0;
      for i:=1 to a[0] do
        begin
          a[i]:=a[i]<<1+nho;
          if a[i]<base then nho:=0
          else begin nho:=1; dec(a[i],base); end;
        end;
      if nho>0 then
        begin
          inc(a[0]);
          a[a[0]]:=nho;
        end;
    end;
procedure add(var a,b:big); inline;
    var
      i,nho:longint;
    begin
      nho:=0;
      if b[0]>a[0] then a[0]:=b[0];
      for i:=1 to a[0] do
        begin
          a[i]:=a[i]+b[i]+nho;
          if a[i]<base then nho:=0
          else begin nho:=1; dec(a[i],base); end;
        end;
      if nho>0 then
        begin
          inc(a[0]);
          a[a[0]]:=nho;
        end;
    end;
procedure mul3(var a:big); inline;
    var
      i,nho:longint;
    begin
      nho:=0;
      for i:=1 to a[0] do
        begin
          a[i]:=a[i]<<1+a[i]+nho;
          if a[i]<base then nho:=0
          else begin nho:=a[i] div base; a[i]:=a[i] mod base; end;
        end;
      if nho>0 then
        begin
          inc(a[0]);
          a[a[0]]:=nho;
        end;
    end;
procedure mul5(var a:big); inline;
    var
      i,nho:longint;
    begin
      nho:=0;
      for i:=1 to a[0] do
        begin
          a[i]:=a[i]<<2+a[i]+nho;
          if a[i]<base then nho:=0
          else begin nho:=a[i] div base; a[i]:=a[i] mod base; end;
        end;
      if nho>0 then
        begin
          inc(a[0]);
          a[a[0]]:=nho;
        end;
    end;
procedure solve;
    var
      i:longint;
    begin
      skn[0]:=1; skn[1]:=1;
      val[0]:=1; val[1]:=1;
      for i:=1 to n do
        case s[i] of
          'L': mul2(val);
          'R': begin mul2(val); add(val,skn); end;
          '*': begin mul5(val); add(val,skn); mul3(skn); end;
        end;
    end;
procedure print(var a:big);
    var
      s:string[20];
      i:longint;
    begin
      write(f2,a[a[0]]);
      for i:=a[0]-1 downto 1 do
        begin
          str(a[i],s);
          while length(s)<lbase do s:='0'+s;
          write(f2,s);
        end;
      writeln(f2);
    end;

begin
  openF;
  inp;
  solve;
  print(val);
  closeF;
end.

Code mẫu của hieult

//#include <conio.h>
#include <stdio.h>
#include <string.h>
#define du 100000000


struct so
{
    int scs,a[2000];
};

so gt,sl;
char s[10001];

void print(so A)
{
     int n = A.scs;
     printf("%d",A.a[n]);
     for(int i = n-1;i>=1;i--)
         printf("%08d",A.a[i]);
} 
         /* Tinh Tong */
void sum(so &tong, so b)
{
     so A = tong;

     if(A.scs > b.scs) for(int i = b.scs+1; i <= A.scs; i++) b.a[i] = 0;
     if(b.scs > A.scs) {for(int i = A.scs+1; i <= b.scs; i++) A.a[i] = 0; tong.scs = b.scs;} 
     int nho = 0;
     for(int i = 1; i <= tong.scs ; i++)
     {
             tong.a[i] = (A.a[i] + b.a[i] + nho)%du;
             nho = (A.a[i] + b.a[i] + nho)/du;
             //printf("%c ",a[i]);
     }    
     if(nho > 0){ tong.scs++ ; tong.a[tong.scs] = nho ;}
}

void tich(so &f,int k)
{
        int nho = 0,t;
        for(int j = 1;j<=f.scs;j++)
        {
            t = f.a[j];    
            f.a[j] = (t*k+nho)%du;
            nho = (t*k+nho)/du;
        }
        if(nho>0) f.a[++f.scs] = nho;
}
int main()
{  
    FILE* f;
    //f = fopen("SETNJA.in","wt");
   // for(int i = 1;i<=10000;i++)
       //fprintf(f,"*");
    //fclose(f);
    //freopen("SETNJA.in","r",stdin);
    scanf("%s",s);
    gt.scs = 1; gt.a[1] = 1;
    sl.scs = 1; sl.a[1] = 1;
    //printf("%d\n",strlen(s));
    for(int i = 0;i<strlen(s);i++)
    {
        if(s[i] == 'L')
            sum(gt,gt);
        else if(s[i]=='R')
        {
             sum(gt,gt);
             sum(gt,sl);
        }
        else if(s[i]=='*')
        {
             tich(gt,5);
             sum(gt,sl);
             tich(sl,3);
        }

    }
    print(gt);
        //getch();
}

Code mẫu của ll931110

{$inline on}
{$MODE DELPHI}
{$H+}
program SETNJA;
const
  input  = '';
  output = '';
  base = 100000000;
  maxd = 900;
type
  arr = array[1..maxd] of longint;
var
  m,res: arr;
  s: string;

procedure init;inline;
var
  f: text;
begin
  assign(f, input);
    reset(f);

  readln(f, s);

  close(f);
end;

procedure mul1(k: integer);inline;
var
  i: integer;
begin
  for i := 1 to maxd do res[i] := res[i] * k;
  for i := 1 to maxd - 1 do if res[i] > base then
    begin
      res[i + 1] := res[i + 1] + res[i] div base;
      res[i] := res[i] mod base;
    end;
end;

procedure mul2(k: integer);inline;
var
  i: integer;
begin
  for i := 1 to maxd do m[i] := m[i] * k;
  for i := 1 to maxd - 1 do if m[i] > base then
    begin
      m[i + 1] := m[i + 1] + m[i] div base;
      m[i] := m[i] mod base;
    end;
end;


procedure ad2;inline;
var
  i: integer;
begin
  for i := 1 to maxd do res[i] := res[i] + m[i];
  for i := 1 to maxd - 1 do if res[i] > base then
    begin
      inc(res[i + 1]);
      res[i] := res[i] - base;
    end;
end;

procedure solve;inline;
var
  i: integer;
begin
  fillchar(res, sizeof(res), 0);
  res[1] := 1;

  fillchar(m, sizeof(m), 0);
  m[1] := 1;

  for i := 1 to length(s) do
    case s[i] of
      'L': mul1(2);
      'R':
        begin
          mul1(2);
          ad2;
        end;
      '*':
        begin
          mul1(5);
          ad2;
          mul2(3);
        end;
    end;
end;

procedure printresult;inline;
var
  f: text;
  st: string;
  i,j,k: integer;
begin
  assign(f, output);
    rewrite(f);

  i := maxd;
  while res[i] = 0 do dec(i);

  write(f, res[i]);
  for j := i - 1 downto 1 do
    begin
      str(res[j],st);
      for k := 1 to 8 - length(st) do write(f, 0);
      write(f, res[j]);
    end;

  close(f);
end;

begin
  init;
  solve;
  printresult;
end.

Code mẫu của khuc_tuan

s = raw_input()
t = 1
k = 1
for c in s:
    if c=='L':
        t *= 2
    if c=='R':
        t = 2 * t + k
    if c=='*':
        t = 5 * t + k
        k *= 3
print t

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.