时间限制: 1.000 Sec  内存限制: 128 M

题目描述

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.
Constraints
1≤N<109
N is an integer.

输入

Input is given from Standard Input in the following format:

N

输出

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

样例输入 Copy
575
样例输出 Copy
4
提示

There are four Shichi-Go-San numbers not greater than 575: 357,375,537 and 573.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll N;
int ans = 0;

void dfs(ll n, bool use3, bool use5, bool use7)
{
    if (n > N) return;
    if (use3 && use5 && use7) ans++;
    dfs(n * 10 + 3, 1, use5, use7);
    dfs(n * 10 + 5, use3, 1, use7);
    dfs(n * 10 + 7, use3, use5, 1);
}

int main()
{
    cin >> N;
    dfs(0, 0, 0, 0);
    cout << ans << '\n';
    return 0;
}
05-08 08:54