题目链接
Problem Statement
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
Input is given from Standard Input in the following format:
N
Output
Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).
Sample Input 1
575
Sample Output 1
4
There are four Shichi-Go-San numbers not greater than 575: 357,375,537 and 573.
Sample Input 2
3600
Sample Output 2
13 There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735,753,3357,3375,3537,3557,3573,3575 and 3577.
题目大意:
就是给你一个整数N,要你找出有多少个小于等于N的“357数”,其中所谓的357数是指一个整数中,每个十进制位都仅含有3,5,7这三个数字,比如 35777,37553,75753……这些都是,也就是说“357数”必须包含至少一个3,一个5,一个7,其余的从3,5,7中任选即可。
分析:
我们不妨设缺少3,或5,或7的整数称为“伪357数”(感谢一位博主的分享,这个方法太简单了),比如335,33,557,5757……,我们通过递归去暴力搜索,每次只需要对当前的数值在后面添加3或5或7等即可,如何判断是否是包含至少一个3,一个5和一个7呢?
我们用二进制的或运算来处理(这个思路太好了),如果当前是在末尾添加了3,那么就或1(二进制0001),如果是添加了5,就或2(二进制0010),如果是添加了7,就或4(0100),这样如果是一个“357数”那么最后的结果一定是一个7(0111)
if(flag==07)
ans++;
代码:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <cstring>
#include <stack>
#include <string>
using namespace std;
typedef long long ll;
const int N = 1e5+199;
const double Pi = acos(-1);
int ans;
ll n;
void DFS(ll val,int flag){
if(val<=n){
if(flag==07)
ans++;
DFS(val*10+3,flag|1);
DFS(val*10+5,flag|2);
DFS(val*10+7,flag|4);
}
}
int main()
{
scanf("%lld",&n);
DFS(0,0);
printf("%d",ans);
}