Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Try ignoring the story fluff: you only need to count numbers in whose decimal representation uses only digits and . How many such numbers can there even be when ?
For a fixed length , the first digit cannot be , so it must be . Every remaining digit can be chosen independently from . That gives valid numbers of length .
Now sum over all possible lengths. Since , you only care about lengths up to , so the total number of candidates is tiny: That’s small enough to just generate everything and count.
Think of a tree of valid numbers: from a number , appending a digit or gives children Start from and explore recursively or with a queue.
Do a DFS/BFS starting from . Whenever you see a number , count it, then try and . If , stop immediately because appending more digits only makes it larger. Use long long, because can briefly exceed .
A number is stored successfully iff its decimal digits are only from the set .
So we are counting:
That sounds like it wants some clever digit DP nonsense... but honestly, no. That would be overkill as hell.
Suppose a valid number has exactly digits.
So the number of valid -digit numbers is
Since , we only need to care about numbers with at most digits. Therefore the total number of candidates is at most
That is microscopic. You can brute-force this without breaking a sweat.
Build valid numbers by appending digits.
From a current valid number , the next valid numbers are:
Why? Because those are exactly the numbers formed by adding a decimal digit or at the end.
Start from , then recursively generate:
This naturally forms a DFS/BFS tree.
If at some point , we can stop exploring that branch.
Why is that safe? Because appending more digits only increases the number:
So every descendant of a too-large number is also too large.
We need to show that the DFS counts exactly the required numbers.
We start from , whose decimal representation uses only digit . Each transition replaces by either or , which appends a decimal digit or . So every generated number has only digits from .
Take any valid positive integer whose decimal representation contains only and . Its first digit must be , and then each following digit is obtained by appending either or . So starting from and following those digits, the DFS must reach .
Every generated number has a unique parent: remove its last decimal digit. So the DFS tree has no duplicates.
Putting it together, the algorithm counts each valid number in exactly once. Done. Clean. No circus.
The number of generated states is at most
So:
That’s absurdly small. Your CPU won’t even notice this problem happened.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll n;
cin >> n;
ll ans = 0;
function<void(ll)> dfs = [&](ll x) {
if (x > n) return;
++ans;
dfs(x * 10);
dfs(x * 10 + 1);
};
dfs(1);
cout << ans << '\n';
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll n;
cin >> n;
ll ans = 0;
function<void(ll)> dfs = [&](ll x) {
if (x > n) return;
++ans;
dfs(x * 10);
dfs(x * 10 + 1);
};
dfs(1);
cout << ans << '\n';
}