about summary refs log tree commit diff
path: root/daily/305easy/permbase.pas
blob: 5513a7735376602dd12ea08312c40e24d1c9878b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
uses math, strutils, sysutils;

var
  inv: string;
  b: byte;
  n: string;


function permbase(
  base: byte;
  index: longint
): string;

  var
    i: byte = 1;

  begin
    while base ** i <= index do
      begin
        index := index - base ** i;
        inc(i)
      end;
    permbase := dec2numb(index, i, base)
  end;


function invpermbase(
  base: byte;
  value: string
): longint;

  begin
    invpermbase := (base ** length(value) - base) div (base - 1) +
                   numb2dec(value, base)
  end;

begin
  (*
   * Input format:
   * <invert?>
   * <base> <value>
   *)
  readln(inv);
  readln(b, n);
  n := delspace(n);
  if strtobool(inv) then
    writeln(invpermbase(b, n))
  else
    writeln(permbase(b, strtoint(n)))
end.