#include #include #include #include #include using namespace std; using i64 = int64_t; vector conv(i64 x, int base) { assert(base % 2 == 1); vector v; while(x != 0) { int rem = static_cast(x % base); if(rem > base / 2) { rem -= base; } v.push_back(rem); x -= rem; assert(x % base == 0); x /= base; } reverse(v.begin(), v.end()); return v; } int cmp(int x, int y) { if(x < y) { return 0; } if(x == y) { return 1; } return 2; } // 下から i64 f(i64 x) { vector a = conv(x, 5); int n = static_cast(a.size()); // dp[未満/丁度/超過][先行ゼロを除く一番左の桁の数字が正か][荷物側の分銅数][反対側の分銅数] := パターン数 vector>>> dp(3, vector>>(2, vector>(n+1, vector(n+1, 0)))); dp[1][0][0][0] = 1; for(int i=n-1; i>=0; --i) { vector>>> ndp(3, vector>>(2, vector>(n+1, vector(n+1, 0)))); for(int state=0; state<3; ++state) { for(int leftmost=0; leftmost<2; ++leftmost) { for(int cntM=0; cntM<=n; ++cntM) { for(int cntP=0; cntP<=n; ++cntP) { if(!dp[state][leftmost][cntM][cntP]) { continue; } for(int d=-2; d<=2; ++d) { int nstate = cmp(d, a[i]); if(nstate == 1) { nstate = state; } int nleftmost = d == 0 ? leftmost : d > 0, ncntM = cntM + (d < 0 ? -d : 0), ncntP = cntP + (d > 0 ? d : 0); if(ncntM > n) { continue; } if(ncntP > n) { continue; } ndp[nstate][nleftmost][ncntM][ncntP] += dp[state][leftmost][cntM][cntP]; } } } } } dp = ndp; } i64 res = 0; for(int state=0; state<2; ++state) { for(int cnt=1; cnt<=n; ++cnt) { res += dp[state][1][cnt][cnt]; } } return res; } int main(void) { i64 N; scanf("%ld", &N); assert(1 <= N && N <= static_cast(powl(10, 18))); i64 res = f(N); printf("%ld\n", res); return 0; }