#include #define all(x) begin(x), end(x) using namespace std; using i32 = int; using i64 = long long; using ld = long double; template inline bool chmax(T1 &a, T2 b) {return a < b and (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b and (a = b, true);} const i64 supl = LONG_LONG_MAX / 2 - 100; const i32 supi = INT_MAX / 2 - 100; i64 rec(vector>>>& dp, string& s, vector& pows, i32 dig, bool tight, bool three, i32 mod) { i64& res = dp[dig][tight][three][mod]; if (~res) { return res; } res = 0; if (dig == (i32)s.size()) { return res = three or mod == 0; } i32 up = (tight ? s[dig] - '0' : 9); for (i32 i = 0 ; i <= up ; i++) { res += rec(dp, s, pows, dig + 1, tight and i == up, three or i == 3, (mod + i * pows[dig]) % 3); } return res; } void main_() { i32 p; cin >> p; string s = "1"; s += string(p, '0'); vector pows(p + 1, 1); for (i32 i = 1 ; i <= p ; i++) { pows[i] = pows[i - 1] * 10; pows[i] %= 3; } reverse(all(pows)); vector dp(s.size() + 1, vector(2, vector(2, vector(3, -1LL)))); cout << rec(dp, s, pows, 0, true, false, 0) - 1 << endl; } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); main_(); return 0; }