- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
#include <stdio.h>
#define int long long
#define mx 1000000
signed main(void) {
int n;
scanf("%lld", &n);
if (n > mx) {
printf("-1");
return 0;
}
int x = n;
int l = n; // чашка с товаром
int r = 0; // другая чашка
int w = 1;
int c = 0;
while (x != 0) {
if (x % 3 == 0) {
x /= 3;
}
else if (x % 3 == 1) {
r += w;
c++;
x /= 3;
}
else { // x % 3 == 2
l += w;
c++;
x = (x + 1) / 3;
}
w *= 3;
}
if (l > mx || r > mx)
printf("-1");
else
printf("%lld", c);
return 0;
}
Комментарии (1) RSS
Добавить комментарий