/* -*- coding: utf-8 -*- * * 1185.cc: No.1185 完全な3の倍数 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_K = 10; /* typedef */ /* global variables */ int ds[MAX_K], es[MAX_K]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); n++; int c2 = 0; for (int i = 1; i <= 9; i++) for (int j = 0; j <= 9; j++) if ((i + j) % 3 == 0) { if (i * 10 + j > n) { printf("%d\n", c2); return 0; } c2++; } //printf("c2=%d\n", c2); int k = 0; for (int m = n; m > 0; m /= 10) ds[k++] = m % 10; //printf("k=%d\n", k); es[0] = 1; for (int i = 1; i < k; i++) es[i] = es[i - 1] * 4; int sum = 0; for (int i = k - 1; i >= 0; i--) { sum += ((ds[i] + 2) / 3) * es[i]; if (ds[i] % 3) break; } sum += c2 - es[2]; printf("%d\n", sum); return 0; }