AtCoder--755——dfs

简介: 题目描述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:

题目描述


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).


样例输入


575


样例输出


4


提示


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


#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
int n,ans;
void dfs(int sum, unsigned char ss){
    if(sum>n)
        return;
    if(ss==07)
        ans++;
    if(sum<99999999){
        dfs(sum*10+3,ss|1);
        dfs(sum*10+5,ss|2);
        dfs(sum*10+7,ss|4);
    }
}
start{
    /**
    ll x=read;
    if(x==7||x==5||x==3)
        printf("YES\n");
    else
        printf("NO\n");
    string s;
    cin>>s;
    int len=s.size();
    int ans=999;
    for(int i=0;i<=len-3;++i)
    {
        int num=(s[i]-'0')*100+(s[i+1]-'0')*10+(s[i+2]-'0');
        ans=min(abs(num-753),ans);
    }
    cout<<ans;**/
    n=read;
    dfs(0,0);
    cout<<ans;
    end;
}


目录
相关文章
|
5月前
|
算法
DFS算法的实现
DFS算法的实现
77 3
|
7月前
|
算法
DFS算法及应用(一)
DFS(深度优先搜索)是一种图遍历算法,常用于解决穷举问题,如全排列、迷宫问题、图的连通性等。它沿着树的深度分支进行探索,直至达到叶子节点,若无法继续则回溯。例如,将数字6拆分为3个正整数的递增序列问题可以通过DFS实现,类似地,分糖果问题和买瓜问题同样可以用DFS求解。DFS通常涉及递归或栈结构,通过标记已访问节点避免重复。在编程中,会定义递归函数,设定结束条件,然后枚举可能的情况,并处理下一层节点。
|
7月前
|
算法 索引
DFS算法及应用(二)
回溯:回溯就是DFS的一种,在搜索探索过程中寻找问题的解,当发现不满足求解条件时,就回溯返回,尝试其他路径。
|
机器学习/深度学习 人工智能 定位技术
|
C语言 C++
【DFS练习】素数环
【DFS练习】素数环
124 0
|
定位技术
DFS:踏青
DFS:踏青
|
算法
迭代加深(DFS)
复习acwing算法提高课的内容,本篇为讲解算法:迭代加深,关于时间复杂度:目前博主不太会计算,先鸽了,日后一定补上。
157 0
迭代加深(DFS)