Editorial for Bread Tree


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

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 fi='';
      fo='';
      max=1234567890;
var n,k:int64;
    a:array[1..30,1..45] of int64;
    num:array[2..30] of longint;
    b:array[2..30] of int64;

procedure init;
var i,j:longint;
begin
     a[1,1]:=1; a[1,2]:=3;
     for i:=2 to 30 do
     begin
          a[i,i+1]:=0;
          for j:=1 to i do
          begin
               a[i,j]:=a[i-1,j];
               a[i,i+1]:=a[i,i+1]+a[i,j];
          end;
          a[i,i+1]:=a[i,i+1]+i+1; inc(j);
          while a[i,j]<=max do
          begin
               inc(j);
               a[i,j]:=a[i,j-1]*2-a[i,j-i-1]+1;
          end;
          num[i]:=j; b[i]:=a[i,j];
     end;
end;

begin
     init;
     assign(input,fi); reset(input);
     assign(output,fo); rewrite(output);
     readln(n,k);
     while n<>0 do
     begin
          dec(n);
          if n=0 then writeln(0)
          else
          begin
          if k=0 then
          begin
               if n>max+1 then n:=max+1;
               writeln(n);
          end
          else
          begin
               if k=1 then
               begin
                    if n>49690 then n:=49690;
                    writeln(n*(n+1) div 2);
               end
               else
               begin
                    if (n>=31) and (k>=30) then writeln(a[30,31])
                    else
                    begin
                         if k>=30 then writeln(a[n-1,n])
                         else
                         begin
                              if n>num[k] then writeln(b[k])
                              else writeln(a[k,n]);
                         end;
                    end;
               end;
          end;
          end;
          readln(n,k);
     end;
     close(input); close(output);
end.

Code mẫu của RR

{$R+,Q+}
uses math;
const
  gh=1234567890;

var
  n,k:int64;
  f:array[0..1,0..50] of int64;

procedure solve;
    var
      t,i,now:longint;
      sum:int64;
    begin
      fillchar(f,sizeof(f),0);
      f[1,0]:=1;
      now:=0; sum:=0;
      for t:=2 to n do
        begin
          fillchar(f[now],sizeof(f[now]),0);
          sum:=0;
          for i:=0 to t do
            begin
              if i>0 then f[now,i]:=f[1-now,i-1];
              if i<k then inc(f[now,0],f[1-now,i]);
            end;

          for i:=0 to t do
            inc(sum,f[now,i]*i);

          if sum>gh then begin writeln(sum); exit; end;
          now:=1-now;
        end;
      writeln(sum);
    end;

begin
  read(n,k);
  while (n>0) do
    begin
      if k=0 then writeln(min(n-1,gh+1))
      else if k=1 then
        begin
          if n>49691 then n:=49691;
          writeln(((n-1)*int64(n)) shr 1)
        end
      else solve;
      read(n,k);
    end;
end.

Code mẫu của hieult

#include <stdio.h>
//#include <conio.h>

main()
{
      //freopen("BRTREE.in","r",stdin);
      long long n,k,a[1000],max=1234567890,KQ,lon=49690;
      while(scanf("%lld %lld",&n,&k)>0 && n>0)
      {
           if(k==0)
           {
               if(n-1>=max+1)
                  printf("%lld\n",max+1);
               else printf("%lld\n",n-1);
           }
           else if(k==1)
           {

               if(n>lon)
               {
                   //printf("%lld\n",n*(n-1)/2);
                   printf("%lld\n",lon*(lon+1)/2);
               }
               else printf("%lld\n",n*(n-1)/2);
           }
           else 
           {

                a[1]=0;KQ=0;
                for(int i=2;i<=n;i++)
                {
                    a[i]=i-1;
                    for(int j=i-1;j>=i-k;j--)
                    {
                        if(j==0)
                            break;
                        else a[i]=a[i]+a[j];
                    }
                    if(a[i]>max)
                    {
                        KQ=a[i];
                        break;
                    }
                    if(i==n)
                        KQ=a[i];
                }
                printf("%lld\n",KQ);
           }
      }
     // getch();
}

Code mẫu của khuc_tuan

uses math, sysutils;

var
    res, n, k : int64;
    f : array[1..100] of int64;
    i, j : longint;

begin
    while true do
    begin
        readln(n,k);
        if n=0 then break;
        if n=1 then writeln(0)
        else if k=0 then writeln(min(n-1,1234567891))
        else if k=1 then
        begin
            if n > 10000000 then writeln(1234572895)
            else writeln(min(1234572895, n * (n-1) div 2));
        end
        else
        begin
            res := 0;
            f[1] := 1;
            i := 1;
            while true do
            begin
                res := res + f[i];
                if res > 1234567890 then break;
                if (i+1) = n then break;
                f[i+1] := 1;
                for j:=i downto max(1,i-k+1) do
                    f[i+1] := f[i+1] + f[j];
                inc(i);
            end;
            writeln(res);
        end;
    end;
end.

Comments

Please read the guidelines before commenting.


There are no comments at the moment.