The meaning of problems
For a non-negative integer i, when \ (i + (i + 1 ) + (i + 2) \) calculation process does not produce a carry, said number i satisfies the condition, i is less than n required.
Thinking
Digital dp.
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[20][5], n;
int a[20], top;
ll dfs(int pos, int sta, bool limit) {
if(pos==-1) return 1;
if(!limit && dp[pos][sta] != -1) return dp[pos][sta];
int up = limit? min(3, a[pos]): 3;
if(pos==0) up = min(up, 2);
ll ans=0;
for (int i=0; i<=up; ++i) ans+=dfs(pos-1, i, limit&&i==a[pos]);
if(!limit) dp[pos][sta]=ans;
return ans;
}
int main() {
while(scanf("%lld", &n)==1) {
if(n<=0) {
puts("0");
continue;
}
--n;
memset(dp, -1, sizeof(dp));
top=0;
while(n) a[top++]=n%10, n/=10;
a[top] = 0;
ll ans = dfs(top, 0, 1);
printf("%lld\n", ans);
}
return 0;
}