Problem:


Score : 300 points

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 \leq N < 10^9

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.

Sample Input 3

999999999

Sample Output 3

26484



Sample input:
575


Sample output:
4

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
LET=('3','5','7')
n=int(input())

def dfs(s):
  cnt = 0
  if len(s) >0 and int(s) > n:
    return cnt
  if s.count('3') > 0 and s.count('5')>0 and s.count('7')>0:
    cnt += 1
  for l in LET:
    cnt += dfs(s+l)
  return cnt

print(dfs(''))

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
num753 = set()
q = deque([3,5,7])

#753数(準753も含む)の全列挙
while q:

    #右端の値を取り出す
    x = q.pop()

    #xが10**9以上になったら終了
    if x <= 10**9:

        #集合型のnum753にxを追加
        num753.add(x)

        #xを10倍して7,5,3をそれぞれ足した数をキューに追加
        q.append(x*10+3)
        q.append(x*10+5)
        q.append(x*10+7)
count = 0

#num753に格納してある値からn以下の753数を判定
for i in num753:
    if i <= n:
        lst = list(str(i))
        if "3" in lst and "5" in lst and "7" in lst:
            count += 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())

q = ["3", "5", "7"]
ans = 0
while q:
    now = q.pop(0)
    if int(now) <= N:
        for c in ("3", "5", "7"):
            q.append(now + c)
        if len(set(now)) == 3:
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
from itertools import product
N = int(input())
STR_N = str(N)
all_pat =[]
for i in range(3,len(STR_N)+1):
    pattern = list(product(["7","5","3"],repeat=i))
    for p in pattern:
        if len(set(p))==3:
            all_pat.append(int(''.join(p)))
all_pat = list(set(all_pat))
all_pat.sort()
cnt = 0
for a in all_pat:
    if a<=N:
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
strn = input()
n = int(strn)
shichigolist = ["3","5","7"]
shichigo = [[] for _ in range(len(strn))]
for i in range(len(strn)):
	if i == 0:
		for j in shichigolist:
			shichigo[i].append(j)
	else:
		for j in shichigo[i-1]:
			for k in shichigolist:
				shichigo[i].append(j+k)
target = len(strn)-1
for i in range(len(strn)):
	for j in shichigo[i][:]:
		if i == target:
			if int(j) > n:
				shichigo[target].remove(j)
			elif j.count("3")==0 or j.count("5")==0 or j.count("7")==0:
				shichigo[target].remove(j)
		else:
			if j.count("3")==0 or j.count("5")==0 or j.count("7")==0:
				shichigo[i].remove(j)

print(sum([len(shichigo[i]) for i in range(len(strn))]))

Submission status: Accepted

Solution code:
n=int(input())
c=0
def a(i):
    if int(i)>n:
        return 0
    r=1 if all(i.count(c)>0for c in '753') else 0
    for c in '753':
        r+=a(i+c)
    return r
print(a('0'))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
N_list = list(str(N))
number = ['3', '5', '7']
count = 0

def iter_p_adic(p, n):
    from itertools import product
    tmp = [range(p)] * n
    return product(*tmp)

for i in range(len(N_list)):
    iterator = iter_p_adic(3, i+1)
    for idxs in iterator:
        num = []
        for j in range(i+1):
            num.append(number[idxs[j]])
        n = int(''.join(num))
        if len(list(Counter(num))) == 3 and n <= N:
            count += 1

print(count)

Submission status: Accepted

Solution code:
import collections
n = int(input())
dic = collections.defaultdict(int)
ans = 0
if n < 357:
    print(0)
else:
    ls = ["357","375","537","573","735","753"]
    ls_next = []
    for i in range(len(ls)):
        if int(ls[i]) <= n:
            ls_next.append(ls[i])
    ls = ls_next
    ans += len(ls_next)
    for i in range(10):
        ls_next = []
        for j in range(len(ls)):
            for k in range(len(ls[0])):
                if int(ls[j][:k]+"3"+ls[j][k:]) <= n:
                    ls_next.append(ls[j][:k]+"3"+ls[j][k:])
                if int(ls[j][:k]+"5"+ls[j][k:]) <= n:
                    ls_next.append(ls[j][:k]+"5"+ls[j][k:])
                if int(ls[j][:k]+"7"+ls[j][k:]) <= n:
                    ls_next.append(ls[j][:k]+"7"+ls[j][k:])
        for j in range(len(ls_next)):
            dic[ls_next[j]] = 1
        ls_next = list(dic.keys())
        #print(ls_next)
        if len(ls_next) == 0:
            break

        ans += len(ls_next)
        ls = ls_next
        dic.clear()
    print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

ans = []
def dfs(a,m):
    global ans
    if len(a) == m:
        s = ""
        for i in range(m):
            s += str(a[i])
        if "7" in s and "5" in s and "3" in s:
            ans.append(int(s))
        return
    for i in [3,5,7]:
        a.append(i)
        dfs(a,m)
        a.pop()
if __name__ == "__main__":
    n = int(input())
    m = len(str(n))
    a = []
    for i in range(3,m+1):
        dfs(a,i)
    ans.sort()
    cnt = 0
    for i in range(len(ans)):
        if ans[i] <= n:
            cnt+=1
    print(cnt)

Submission status: Accepted

Solution code:
N=int(input())

#753数を列挙してその数を数える
def dfs(s):
  #Nを超えたらカウントしない
  if int(s)>N:
    return 0
  #sに7,5,3の全てが含まれているなら+1
  ret = 1 if all(s.count(c)>0 for c in '753') else 0
  for c in '753':
    #sに7or5or3を加えてdfs
    ret += dfs(s + c)
  return ret

#0から始める
print(dfs('0'))

#難しい...

Submission status: Accepted

Solution code:
N = int(input())
S = str(N)
l = len(S)


ans = 0


def dfs(n):
    global ans
    if len(str(n)) == l:
        if len(set(list(str(n)))) == 3 and n <= N:
            ans += 1
        return
    else:
        if len(set(list(str(n)))) == 3:
            ans += 1

    dfs(int(str(n) + "3"))
    dfs(int(str(n) + "5"))
    dfs(int(str(n) + "7"))


dfs(3)
dfs(5)
dfs(7)

print(ans)

Submission status: Accepted

Solution code:
import queue

n=int(input())

q=queue.Queue()

q.put(3)
q.put(5)
q.put(7)

c=0
num=q.get()
while num<=n:
    if "3" in str(num) and "5" in str(num) and "7" in str(num):
        c+=1
    q.put(num*10+3)
    q.put(num*10+5)
    q.put(num*10+7)
    num=q.get()

print(c)

Submission status: Accepted

Solution code:
N = input()

def dfs(A):
    if int(A) > int(N):
        return 0
    if all(A.count(c) for c in "753") >0:
        ans = 1
    else:
        ans = 0
        
    for i in "357":
        A += i
        ans += dfs(A)
        A = A[:-1]
        
    return ans

print(dfs("0"))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

def saiki(s):
    global cnt,n
    L = ["3","5","7"]
    if eval(s) <= n:
        if len(set(s)) == 3:
            cnt += 1
        for i in L:
            saiki(s+i)

n = int(input())
cnt = 0
for i in ["3","5","7"]:
    saiki(i)
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
ans=set()
def saiki(A):
    if len(A)>0:
        B="".join(A)
        count=set(A)
        #print(count)
        if eval(B)<=N:
            if len(count)==3:
                ans.add(B)
        else:
            return
    for i in [3,5,7]:
        A.append(str(i))
        saiki(A)
        A.pop()
saiki([])
print(len(ans))

Submission status: Accepted

Solution code:
N = int(input())

def dfs(s):
    if len(s) > 0:
        if int(s) > N:
            return 0

    count = 0

    if "3" in s and "5" in s and "7" in s:
        count += 1
    
    for next_n in ["3", "5", "7"]:
        count += dfs(s + next_n)

    return count

print(dfs(""))

Submission status: Accepted

Solution code:
n = int(input())
def dfs(string):
    if len(string) == len(str(n)):
        return 1 if all(k in string for k in '753') and int(''.join(string)) <= n else 0

    rec = 0
    s = '753' if len(string) != 0 and string[-1] != '0' else '0753'
    for num in s:
        string.append(num)
        rec += dfs(string)
        string.pop()

    return rec

print(dfs([]))

Submission status: Accepted

Solution code:
N = int(input())
ans = 0

def dfs(x, a, b, c):
    global ans
    if x > N:
        return
    if (a & b & c):
        ans += 1
    dfs(10 * x + 3, 1, b, c)
    dfs(10 * x + 5, a, 1, c)
    dfs(10 * x + 7, a, b, 1)

dfs(0, 0, 0, 0)
print(ans)

Submission status: Accepted

Solution code:
def calc(n):
  dic={0:0,1:3,2:5,3:7}
  ret=0
  for j in range(9):
    ret+=dic[n%4]*(10**j)
    n-=n%4
    n//=4
    if n==0:
      return ret

def check(n):
  cnt=[0]*10
  for c in str(n):
    cnt[int(c)]+=1
  if cnt[0]==0 and cnt[3]>=1 and cnt[5]>=1 and cnt[7]>=1:
    return True
  else:
    return False

n=int(input())
arr=[]
for i in range(4**9):
  tmp=calc(i)
  if check(tmp)==True:
    arr.append(tmp)
arr=sorted(arr)
for i in range(len(arr)):
  if arr[i]>n:
    print(i)
    break
else:
  print(len(arr))

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

N = int(input())

def dfs(s):
    if int(s) > N:
        return 0
    ret = 1 if all(s.count(c) > 0 for c in '753') else 0
    for c in '753':
        ret += dfs(s + c)
    return ret

ans = dfs("0")
print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

N = int(input())

def F(list_):
    if list_ and int("".join(map(str, list_))) > N:
        return
    
    if 3 in list_ and 5 in list_ and 7 in list_:
        yield 1
    
    for v in [3, 5, 7]:
        list_.append(v)
        yield from F(list_)
        list_.pop()
    return

ans = sum(F([]))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

def dfs(val, A):
    if val > n:
        return
    A.append(val)
    dfs(10 * val + 3, A)
    dfs(10 * val + 5, A)
    dfs(10 * val + 7, A)

A = []
dfs(3, A)
dfs(5, A)
dfs(7, A)

cnt = 0
for v in A:
    if len(set(list(str(v)))) == 3:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
cnt = 0

def main():
    n = int(input())
    m = len(str(n))
    nl = len(str(n))

    def cal(A):
        l = len(A) - 1
        ans = 0
        for idx, i in enumerate(A):
            ans += i*10**(l - idx)
        return ans

    def dfs(A):
        global cnt
        if len(A) == m:
            if len(list(set(A) & set([3, 5, 7]))) == 3:
                if (cal(A)) <= n:
                    # print(cal(A))
                    cnt += 1
            return
        for i in (3, 5, 7):
            A.append(i)
            dfs(A)
            A.pop()
    for m in range(nl+1):
         dfs([])
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
num=input()
count = 0
for i in range(3, len(num) + 1):
  for j in itertools.product([3,5,7], repeat=i):
    sumj = 0
    for k in range(i):
      sumj += j[k] * (10 ** (i-k-1))
    
    if '3' in (str)(sumj) and '5' in (str)(sumj) and '7' in (str)(sumj):
      if sumj <= (int)(num):
        count += 1
        #print(sumj)
      else:
        break
print(count)

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
# from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input().split())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input().split()) for _ in range(n) ]

n = I()

# 数字nを10進数からb進数へ変換
def base10to(n,b):
    if (int(n/b)):
        return base10to(int(n/b), b) + str(n%b)
    return str(n%b)

# 数字numをb進数から10進数へ変換
def base10from(num, b):
    n = 0
    numlist = list(num);
    while (numlist):
        n *= b
        n += int(numlist.pop(0))
    return n

count = 0

for i in range(3,10):
    m = 3**i
    for j in range(m):
        s = list(base10to(j,3))
        a = "".join(s)
        b = a.zfill(i)
        c = list(b)
        for k in range(i):
            if c[k]=="0":
                c[k] = "3"
            elif c[k]=="1":
                c[k] = "5"
            elif c[k]=="2":
                c[k] = "7"
            else:
                print("E")
        d = "".join(c)
        e = int(d)
        if e<=n:
            if len(set(c))==3:
                count += 1
            else:
                pass
        else:
            print(count)
            exit()

print(count)
exit()

Submission status: Accepted

Solution code:
from itertools import product
from bisect import bisect_left, bisect_right

n = int(input())
s = []
for i in range(3, 10):
    for v in product('753', repeat=i):
        if len(set(v)) >= 3:
            s.append(int(''.join(v)))

s.sort()

idx = bisect_right(s, n)
print(len(s[:idx]))

Submission status: Accepted

Solution code:
def dfs(s):
    if int(s) > n:
        return 0
    m = 1 if all(s.count(i) > 0 for i in "753") else 0
    for i in "753":
        m += dfs(s + i)
    return m
n = int(input())
print(dfs("0"))

Submission status: Accepted

Solution code:
def cal(N,n_str):
    n_list = ["7","5","3"]

    if len(n_str) != 0:
        if int(n_str) <= N:
            if len(n_str) == len(str(N)) and len(set(list(n_str))) == 3:
                return 1
            elif len(n_str) == len(str(N)) and len(set(list(n_str))) < 3:
                return 0
            elif len(n_str) < len(str(N)) and len(set(list(n_str))) == 3:
                return 1 + cal(N,n_str+"7") + cal(N,n_str+"5") + cal(N,n_str+"3")
            elif len(n_str) < len(str(N)) and len(set(list(n_str))) < 3:
                return cal(N,n_str+"7") + cal(N,n_str+"5") + cal(N,n_str+"3")
        else:
            return 0
    else:
        return cal(N,n_str+"7") + cal(N,n_str+"5") + cal(N,n_str+"3")

def main():
    N = int(input())

    print(cal(N,""))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = input()
t = ["3","5","7"]
for i in range(8):
    s = []
    for j in t:
        s.append(j+"3")
        s.append(j+"5")
        s.append(j+"7")
    t += s
t = set(t)
t = list(t)
t.sort()
count = 0
for i in range(len(t)):
    if len(t[i]) > len(n):
        continue
    if int(t[i]) > int(n):
        continue
    if '3' in t[i] and '5' in t[i] and '7' in t[i]:
        count+=1
print(count)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
q = deque(['3','5','7'])
ans = 0
l = []
while True:
    v = q.popleft()
    if int(v) >= n:
        if int(v) == n and ('3' in v and '5' in v and '7' in v):
            ans += 1
        print(ans)
        exit() 
    if '3' in v and '5' in v and '7' in v:
        ans += 1
        l.append(int(v))
    q.append(v+'3')
    q.append(v+'5')
    q.append(v+'7')

Submission status: Accepted

Solution code:
N = int(input())
def dfs(s):
    if int(s)>N:
        return 0
    ret = 1 if all(s.count(c) > 0 for c in "753") else 0
    for c in "753":
        ret += dfs(s+c)
    return ret
print(dfs("0"))

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = len(str(n))
ary = []
siti = ["3","5","7"]
def dfs (s,t):
  if len(s) == t:
    if s.count("3") > 0 and s.count("5") > 0 and s.count("7") > 0:
      ary.append(int(s))
    return
  for j in siti:
    dfs(s+j,t)
for i in range(3,l+1):
  dfs("",i)
print (bisect.bisect_right(ary,n))

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = len(str(n))
ary = []
siti = ["3","5","7"]
def dfs (s,t):
	if len(s) == t:
		if s.count("3") > 0 and s.count("5") > 0 and s.count("7") > 0:
			ary.append(int(s))
		return
	for j in siti:
		dfs(s+j,t)
for i in range(3,l+1):
	dfs("",i)
# ary.sort()
print (bisect.bisect_right(ary,n))

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = len(str(n))
ary = []
siti = ["3","5","7"]
def dfs (s,t):
	if len(s) == t:
		if s.count("3") > 0 and s.count("5") > 0 and s.count("7") > 0:
			ary.append(int(s))
		return
	for j in siti:
		dfs(s+j,t)
for i in range(3,l+1):
	dfs("",i)
# ary.sort()
print (bisect.bisect_right(ary,n))

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = len(str(n))

ary = []

siti = ["3","5","7"]
def dfs (s,t):
	if len(s) == t:
		if s.count("3") > 0 and s.count("5") > 0 and s.count("7") > 0:
			ary.append(int(s))
		return
	for j in siti:
		dfs(s+j,t)

for i in range(3,l+1):
	dfs("",i)

ary.sort()
print (bisect.bisect_right(ary,n))

Submission status: Accepted

Solution code:
import itertools

n = input()
ans = 0
l = ["3", "5", "7"]

for i in range(3, len(n) + 1):
    for j in itertools.product(l, repeat = i):
        j = "".join(j)
        if int(j) <= int(n):
            if "3" in str(j):
                if "5" in str(j):
                    if "7" in str(j):
                        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def dfs(s):
  if int(s) > N: return 0
  ret = 1 if all(s.count(c) > 0 for c in '753') else 0
  for c in '753':
    ret += dfs(s+c)
  return ret

print(dfs('0'))

Submission status: Accepted

Solution code:
n = int(input())
def dfs(sss):
  if(int(sss) > n):
    return 0
  r = 1 if(all(sss.count(c) > 0 for c in '753')) else 0
  for c in '357':
    r += dfs(sss+c)
  return r
print(dfs('0'))

Submission status: Accepted

Solution code:
def dfs(n):
    if int(n)>N:
        return
    L.append(int(n))
    dfs(n+"3")
    dfs(n+"5")
    dfs(n+"7")
    
N=int(input())
L=[]
dfs("3")
dfs("5")
dfs("7")

M=[]
for l in L:
    S=str(l)
    if "3" in S and "5" in S and "7" in S and l<=N:
        M.append(l)
print(len(M))

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


def recursive(base, pattern, n):

    for i in ["7", "5", "3"]:

        new_target = "".join(base + str(i))

        # 条件を満たさない場合は、再帰を呼ばない。
        if int(new_target) > n:
            continue

        # 条件を満たす場合、リストに加える
        if new_target.count("7") >= 1 and new_target.count("5") >= 1 and new_target.count("3") >= 1:
            pattern.append(new_target)

        recursive(new_target, pattern, n)


def resolve():

    n = int(input())

    pattern = []

    # 再帰的処理
    for i in ["7", "5", "3"]:
        recursive(i, pattern, n)

    print(len(pattern))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """575"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3600"""
        output = """13"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """999999999"""
        output = """26484"""
        self.assertIO(input, output)


if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Accepted

Solution code:
N = int(input())

def dfs(s):
    if int(s) > N:
        return 0
    ret = 1 if all(s.count(c) > 0 for c in '753') else 0
    for c in '753':
        ret += dfs(s+c)
    return ret

print(dfs('0'))

Submission status: Accepted

Solution code:
from itertools import product
n = int(input())
cnt = 0
a = ['7', '5', '3']
for i in range(3, len(str(n)) + 1):
    for j in product(a, repeat=i):
        word = ''.join(j)
        if all(w in word for w in ['7', '5', '3']) and int(word) <= n:
            cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
# ＤＦＳ, パターン列挙_全探索用

n = int(input())

# DFS_start
stack = [0] # 初期化_ここでは'0'を設置
pre_ans = [] # パターン候補の器_初期化

while stack: # stackが空になるまでループ
    tmp = stack.pop() # パターンの候補を pop
    if tmp <= n: # パターンとしての条件を確認
        pre_ans.append(tmp) # 条件に合えば append
        x = tmp * 10 + 3 # パターンの候補を作成して...
        y = tmp * 10 + 5 # 〃
        z = tmp * 10 + 7 # 〃
        stack += [x, y, z]  # 積む
# DFS_end

ans = 0
for i in pre_ans: # 列挙したパターンを全探索
    if len(set(str(i))) == 3:
        ans += 1

print(ans)


'''
上記は パターンを列挙 → 全探索 だが、
実際には パターンを列挙 しながら 探索 が高効率

n = int(input())

stack = [0]
ans = 0
while stack:
    tmp = stack.pop()
    if tmp <= n:
        if len(set(str(tmp))) == 3:
            ans += 1
        x = tmp * 10 + 3
        y = tmp * 10 + 5
        z = tmp * 10 + 7
        stack += [x, y, z]

print(ans)

'''

Submission status: Accepted

Solution code:
# ＢＦＳ, パターン列挙_全探索用

from collections import deque

n = int(input())

# DFS_start
queue = deque([0]) # 初期化_ここでは'0'を設置
pre_ans = [] # パターン候補の器_初期化

while queue: # queueが空になるまでループ
    tmp = queue.popleft() # パターンの候補を popleft
    if tmp <= n: # パターンとしての条件を確認
        pre_ans.append(tmp) # 条件に合えば append
        x = tmp * 10 + 3 # パターンの候補を作成して...
        y = tmp * 10 + 5 # 〃
        z = tmp * 10 + 7 # 〃
        queue += [x, y, z]  # 積む
# DFS_end

ans = 0
for i in pre_ans: # 列挙したパターンを全探索
    if len(set(str(i))) == 3:
        ans += 1

print(ans)

'''
上記は パターンを列挙 → 全探索 だが、
実際には パターンを列挙 しながら 探索 が高効率

from collections import deque

n = int(input())

# DFS_start
queue = deque([0]) # 初期化_ここでは'0'を設置

ans = 0
while queue: # queueが空になるまでループ
    tmp = queue.popleft() # パターンの候補を popleft
    if tmp <= n: # パターンとしての条件を確認
        if len(set(str(tmp))) == 3: # ここで ansを探索
            ans += 1
        x = tmp * 10 + 3 # パターンの候補を作成して...
        y = tmp * 10 + 5 # 〃
        z = tmp * 10 + 7 # 〃
        queue += [x, y, z]  # 積む

print(ans)

'''

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()

d=deque([3,5,7])
ans=0
while True:
    a=d.popleft()
    if a>n:
        break
    else:
        s=str(a)
        if s.count("3")>0 and s.count("5")>0 and s.count("7")>0:
            ans+=1
        d.append(10*a+3)
        d.append(10*a+5)
        d.append(10*a+7)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from collections import deque
import heapq
import math
import fractions
import bisect
import itertools
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))


n=int(input())
stack=[0]
ans=0
while stack:
    q=stack.pop()
    if q<=n:
        if len(set(str(q)))==3:
            ans+=1
        stack.append(q*10+3)
        stack.append(q*10+5)
        stack.append(q*10+7)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

stack = [0]
ans = 0
while stack:
    tmp = stack.pop()
    if tmp <= n:
        if len(set(str(tmp))) == 3:
            ans += 1
        stack += [tmp * 10 + 3, tmp * 10 + 5, tmp * 10 + 7]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

stack = [0]
ans = 0
while stack:
    tmp = stack.pop()
    if tmp <= n:
        if len(set(str(tmp))) == 3:
            ans += 1
        x = tmp * 10 + 3
        y = tmp * 10 + 5
        z = tmp * 10 + 7
        stack += [x, y, z]

print(ans)

Submission status: Accepted

Solution code:
# ＤＦＳ, パターン列挙_全探索用

n = int(input())

# DFS_start
stack = [0] # 初期化_ここでは'0'を設置
pre_ans = [] # パターン候補の器_初期化

while stack: # stackが空になるまでループ
    tmp = stack.pop() # パターンの候補を pop
    if tmp <= n: # パターンとしての条件を確認
        pre_ans.append(tmp) # 条件に合えば append
        x = tmp * 10 + 3 # パターンの候補を作成して...
        y = tmp * 10 + 5 # 〃
        z = tmp * 10 + 7 # 〃
        stack += [x, y, z]  # 積む
# DFS_end

ans = 0
for i in pre_ans: # 列挙したパターンを全探索
    if len(set(str(i))) == 3:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect

MAX_LIM = 10**9
N = (input())
keta = len(N)
N = int(N)

# print(pow(3, 9))
keta_lis = {1: [3, 5, 7], 2: [], 3: [],
            4: [], 5: [], 6: [], 7: [], 8: [], 9: []}
for i in range(1, keta):
    times = pow(10, i)
    for tmp in keta_lis[i]:
        for num in [3, 5, 7]:
            keta_lis[i+1].append(tmp + times*num)
ans = []
for keta in keta_lis.keys():
    ans.extend(keta_lis[keta])
ans = np.array(sorted(ans))
ans = ans[357 <= ans]
ans = ans[ans <= N]
count = 0
for ans_ in ans:
    if (('3' in str(ans_)) and ('5' in str(ans_)) and ('7' in str(ans_))):
        # print(ans_)
        count += 1
print(count)

# print(ans[357 <= ans])

Submission status: Accepted

Solution code:
n = int(input())

def dfs(num):  
  if(int(num) > n):
    return 0
  res = 1 if(all(num.count(c) > 0 for c in "753")) else 0
  for i in "753":
    res += dfs(num + i)

  return res

print(dfs("0"))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque 

cnt = 0
def include(x):
    global cnt
    s = str(x)
    if ('3' in s) and ('5' in s) and ('7' in s):
        cnt += 1  
     
def main():
    n = int(readline())
    dq = deque()
    dq.append(0)
    cnd = [0]
    dd = (3, 5, 7)
    while len(dq)>0:
        v = dq.popleft()
        for i in dd:
            if v*10+i>n:
                continue
            dq.append(v*10+i)
            cnd.append(v*10+i)
            include(v*10+i)
        if cnd[-1] > n:
            break
    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import product
l = []
for _ in range(3,10):
    tmp = list(product('753', repeat = _))
    l += tmp

m = []
for i in l:
    if len(set(i)) == 3:
       m.append(int(''.join(i)))

n = int(input())
ans = 0
for j in m:
    if j <= n:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())

def change_to_num(ls):
    ls = list(map(str, ls))
    strnum = ''.join(ls)
    return int(strnum)
def dfs(num_ls):
    '''
    引数のnum_ls以降の753numの総数を返す
    '''
    if change_to_num(num_ls) > N:
        return 0
    
    tmp = 0
    # 引数のnum_lsは753か？
    c = Counter(num_ls)
    is753 = True
    for num in [7,5,3]:
        if c[num] == 0:
            is753 = False
    if is753:
        tmp += 1
    
    # 次の状態の753総数を足す
    for num in [7,5,3]:
        num_ls.append(num)
        tmp += dfs(num_ls)
        num_ls.pop()
    return tmp
print(dfs([0]))

Submission status: Accepted

Solution code:
N = int(input())
import itertools
cnt = 0
for i in range(1,10):
    Ls = list(itertools.product(["3","5","7"], repeat=i))
    for L in Ls:
        if ("3" in L) and ("5" in L) and ("7" in L):
            S = int("".join(L))
            if S<=N:
                cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
n= int(input())
def func(cur,use,count):
  if cur > n:
    return
  if use == 0b111:
    count.append(1)
  func(cur*10+3,use|0b001,count)
  func(cur*10+5,use|0b010,count)
  func(cur*10+7,use|0b100,count)
res = []
func(0,0,res)
print(sum(res))

Submission status: Accepted

Solution code:
n = int(input())

from itertools import product

l = []
for i in range(3, 10):
    for prod in product(*[['3', '5', '7'] for _ in range(i)]):
        p = ''.join(list(prod))
        if p.count('3') and p.count('5') and p.count('7'):
            l.append(p)

ans = 0
for ll in l:
    if int(ll) <= n:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
#n,m=map(int,input().split())
#hl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]


l=[7,5,3]
count=0

def dfs(cur,use):
    global count
    if cur>n:
        return
    if use==0b111 :
        count+=1
    dfs(cur*10+3,use | 0b001)
    dfs(cur*10+5,use | 0b010)
    dfs(cur*10+7,use | 0b100)

dfs(0,0b000)
print(count)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

N = int(readline())

stack = [3,5,7]
ans = 0

all = {"3","5","7"}
def isOk(x):
  return len(all - set(str(x))) == 0
  
while stack:
  num = stack.pop()
  if num > N:
    continue
  if isOk(num):
    ans += 1
  for i in (3,5,7):
    stack.append(num * 10 + i)
  
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

N = int(readline())

stack = [3,5,7]
ans = 0

def isOk(x):
  s = str(x)
  for c in "357":
    if c not in s:
      return False
  return True
  
while stack:
  num = stack.pop()
  if num > N:
    continue
  if isOk(num):
    ans += 1
  for i in (3,5,7):
    stack.append(num * 10 + i)
  
print(ans)