#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ fast_plus(int /*<<< orphan*/, long long);
int /*<<< orphan*/ fast_power(long long, long long);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int main(int argc, char *argv[]) {
  freopen("multi.in", "r", stdin);
  freopen("multi.out", "w+", stdout);
  int x, i, j;
  char ii[50], jj[50];
  long long iii = 0, jjj = 0, power, base;
  scanf("%i", &x);
  if (x == 1) {
    scanf("%s %s", ii, jj);
    for (i = 0; i < strlen(ii); i++) {
      iii *= 3;
      iii += ii[i] - '0';
    }
    for (i = 0; i < strlen(jj); i++) {
      jjj *= 3;
      jjj += jj[i] - '0';
    }
    power = (jjj - 1) / 9;
    base = jjj % 9;
    printf("%I64d", fast_plus(fast_power(iii, power), base));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
lea    0xe77(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1060 <freopen@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
lea    0xe66(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1060 <freopen@plt>
movq   $0x0,-0x98(%rbp)
movq   $0x0,-0xa0(%rbp)
lea    0xe4a(%rip),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x0,%al
call   1070 <scanf@plt>
cmpl   $0x1,-0x14(%rbp)
jne    1329 <main+0x1c9>
lea    -0x50(%rbp),%rsi
lea    -0x90(%rbp),%rdx
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1070 <scanf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0xb4(%rbp)
lea    -0x50(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0xb4(%rbp),%eax
cmp    %ecx,%eax
jge    125e <main+0xfe>
imul   $0x3,-0x98(%rbp),%rax
03
mov    %rax,-0x98(%rbp)
movslq -0x18(%rbp),%rax
movsbl -0x50(%rbp,%rax,1),%eax
sub    $0x30,%eax
cltq
add    -0x98(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1203 <main+0xa3>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0xb8(%rbp)
lea    -0x90(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0xb8(%rbp),%eax
cmp    %ecx,%eax
jge    12c6 <main+0x166>
imul   $0x3,-0xa0(%rbp),%rax
03
mov    %rax,-0xa0(%rbp)
movslq -0x18(%rbp),%rax
movsbl -0x90(%rbp,%rax,1),%eax
ff
sub    $0x30,%eax
cltq
add    -0xa0(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1265 <main+0x105>
mov    -0xa0(%rbp),%rax
sub    $0x1,%rax
mov    $0x9,%ecx
cqto
idiv   %rcx
mov    %rax,-0xa8(%rbp)
mov    -0xa0(%rbp),%rax
mov    $0x9,%ecx
cqto
idiv   %rcx
mov    %rdx,-0xb0(%rbp)
mov    -0x98(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
call   1080 <fast_power@plt>
mov    %eax,%edi
mov    -0xb0(%rbp),%rsi
call   1050 <fast_plus@plt>
mov    %eax,%esi
lea    0xcfd(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
lea    0xe82(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1060 <freopen@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    0xe71(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1060 <freopen@plt>
lea    0xe6b(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
cmpl   $0x1,0xc(%rsp)
jne    12bd <main+0x15d>
lea    0xe50(%rip),%rdi
xor    %r14d,%r14d
lea    0x50(%rsp),%rbx
lea    0x10(%rsp),%rdx
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1225 <main+0xc5>
xor    %ebx,%ebx
lea    0x50(%rsp),%r15
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
lea    (%r14,%r14,2),%rax
movsbq 0x50(%rsp,%rbx,1),%rcx
lea    (%rcx,%rax,1),%r14
add    $0xffffffffffffffd0,%r14
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1200 <main+0xa0>
lea    0x10(%rsp),%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1267 <main+0x107>
xor    %ebx,%ebx
lea    0x10(%rsp),%r15
xor    %r12d,%r12d
nopl   (%rax)
lea    (%r12,%r12,2),%rax
movsbq 0x10(%rsp,%rbx,1),%rcx
lea    (%rcx,%rax,1),%r12
add    $0xffffffffffffffd0,%r12
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1240 <main+0xe0>
jmp    126a <main+0x10a>
xor    %r12d,%r12d
lea    -0x1(%r12),%rax
movabs $0x1c71c71c71c71c72,%rcx
c7 71 1c
imul   %rcx
mov    %rdx,%rsi
shr    $0x3f,%rsi
add    %rdx,%rsi
mov    %r12,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
add    %rdx,%rax
lea    (%rax,%rax,8),%rax
sub    %rax,%r12
mov    %r14,%rdi
call   1080 <fast_power@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1050 <fast_plus@plt>
lea    0xd6b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"multi.in\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [10 x i8] c"multi.out\00"
@.str.3 = [3 x i8] c"w+\00"
@stdout = global i32 0
@.str.4 = [3 x i8] c"%i\00"
@.str.5 = [6 x i8] c"%s %s\00"
@.str.6 = [6 x i8] c"%I64d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [50 x i8]
%10 = alloca [50 x i8]
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* @stdin
%16 = call i32 @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @stdout
%18 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %17)
store i64 0, i64* %11
store i64 0, i64* %12
%19 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %6)
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %79
22: ; preds = %2
%23 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%24 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%25 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %23, i8* %24)
store i32 0, i32* %7
br label %26
26: ; preds = %43, %22
%27 = load i32, i32* %7
%28 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
%30 = icmp slt i32 %27, %29
br i1 %30, label %31, label %46
31: ; preds = %26
%32 = load i64, i64* %11
%33 = mul nsw i64 %32, 3
store i64 %33, i64* %11
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = sub nsw i32 %38, 48
%40 = sext i32 %39 to i64
%41 = load i64, i64* %11
%42 = add nsw i64 %41, %40
store i64 %42, i64* %11
br label %43
43: ; preds = %31
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %26
46: ; preds = %26
store i32 0, i32* %7
br label %47
47: ; preds = %64, %46
%48 = load i32, i32* %7
%49 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = icmp slt i32 %48, %50
br i1 %51, label %52, label %67
52: ; preds = %47
%53 = load i64, i64* %12
%54 = mul nsw i64 %53, 3
store i64 %54, i64* %12
%55 = load i32, i32* %7
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = sub nsw i32 %59, 48
%61 = sext i32 %60 to i64
%62 = load i64, i64* %12
%63 = add nsw i64 %62, %61
store i64 %63, i64* %12
br label %64
64: ; preds = %52
%65 = load i32, i32* %7
%66 = add nsw i32 %65, 1
store i32 %66, i32* %7
br label %47
67: ; preds = %47
%68 = load i64, i64* %12
%69 = sub nsw i64 %68, 1
%70 = sdiv i64 %69, 9
store i64 %70, i64* %13
%71 = load i64, i64* %12
%72 = srem i64 %71, 9
store i64 %72, i64* %14
%73 = load i64, i64* %11
%74 = load i64, i64* %13
%75 = call i32 @fast_power(i64 %73, i64 %74)
%76 = load i64, i64* %14
%77 = call i32 @fast_plus(i32 %75, i64 %76)
%78 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %67, %2
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"multi.in\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [10 x i8] c"multi.out\00"
@.str.3 = [3 x i8] c"w+\00"
@stdout = global i32 0
@.str.4 = [3 x i8] c"%i\00"
@.str.5 = [6 x i8] c"%s %s\00"
@.str.6 = [6 x i8] c"%I64d\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [50 x i8]
%5 = alloca [50 x i8]
%6 = load i32, i32* @stdin
%7 = tail call i32 @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @stdout
%9 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = bitcast i32* %3 to i8*
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%12 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %58
16: ; preds = %2
%17 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %11, i8* %12)
%18 = call i32 @strlen(i8* %11)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %24, %16
%21 = phi i64 [ 0, %16 ], [ %32, %24 ]
%22 = call i32 @strlen(i8* %12)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %37, label %50
24: ; preds = %16, %24
%25 = phi i64 [ %33, %24 ], [ 0, %16 ]
%26 = phi i64 [ %32, %24 ], [ 0, %16 ]
%27 = mul nsw i64 %26, 3
%28 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 %25
%29 = load i8, i8* %28
%30 = sext i8 %29 to i64
%31 = add i64 %27, -48
%32 = add i64 %31, %30
%33 = add nuw nsw i64 %25, 1
%34 = call i32 @strlen(i8* %11)
%35 = sext i32 %34 to i64
%36 = icmp slt i64 %33, %35
br i1 %36, label %24, label %20
37: ; preds = %20, %37
%38 = phi i64 [ %46, %37 ], [ 0, %20 ]
%39 = phi i64 [ %45, %37 ], [ 0, %20 ]
%40 = mul nsw i64 %39, 3
%41 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 %38
%42 = load i8, i8* %41
%43 = sext i8 %42 to i64
%44 = add i64 %40, -48
%45 = add i64 %44, %43
%46 = add nuw nsw i64 %38, 1
%47 = call i32 @strlen(i8* %12)
%48 = sext i32 %47 to i64
%49 = icmp slt i64 %46, %48
br i1 %49, label %37, label %50
50: ; preds = %37, %20
%51 = phi i64 [ 0, %20 ], [ %45, %37 ]
%52 = add nsw i64 %51, -1
%53 = sdiv i64 %52, 9
%54 = srem i64 %51, 9
%55 = call i32 @fast_power(i64 %21, i64 %53)
%56 = call i32 @fast_plus(i32 %55, i64 %54)
%57 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %50, %2
ret i32 0
}