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

Solution (Python):
from itertools import product
N=int(input())
L=[]
for i in range(3,10):
    L+=list(product("753",repeat=i))
ans=0
for l in L:
    if len(set(l))>2 and int("".join(l))<=N:
        ans+=1
print(ans)

Solution (Python):
n = int(input())
def dfs(s):
    if int(s) > n:
        return 0
    ret = 1 if all( (i in s) for i in '753') else 0
    for i in '753':
        ret += dfs(s+i)
    return ret
print(dfs('0'))

Solution (Python):
def base10to(n, b):
    if(int(n/b)):
        return base10to(int(n/b), b) + str(n%b)
    return str(n%b)
def main():
    n = int(input())
    result = 0
    for i in range(1, 3*10**5):
        temp = base10to(i, 4)
        if '0' not in temp and '1' in temp and '2' in temp and '3' in temp:
            temp = temp.replace('3', '7')
            temp = temp.replace('1', '3')
            temp = temp.replace('2', '5')
            if int(temp) <= n:
                
                result += 1
            else:
                break
    print(result)
main()

Solution (Python):



N = int(input())

ans = 0

def dfs(A,use_num):
    global ans
    
    if len(A) > 1:
        if int("".join(map(str, A))) > N:
            return
    if use_num == 0b111:
        ans += 1

    for v in [3,5,7]:
        A.append(v)
        if v == 3:
            dfs(A,use_num | 0b001)
        elif v == 5:
            dfs(A,use_num | 0b010)
        elif v == 7:
            dfs(A,use_num | 0b100)
        A.pop()

dfs([],0b000)

print(ans)

Solution (Python):
import itertools
import bisect
n=int(input())
lst=[]
for i in range(3, len(str(n))+1):
  x=list(itertools.product(["3", "5", "7"], repeat=i))
  z=[int("".join(x[j])) for j in range(len(x))]
  lst+=z
ans=[]
for j in range(len(lst)):
  if len(set(str(lst[j])))==3 and lst[j]<=n:
    ans.append(lst[j])
print(len(ans))

Solution (Python):

printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

h = {}
def dfs(n,i,j,k,f):
    if (n,i,j,k,f) in h:
        return h[(n,i,j,k,f)]
    for e in range(11):
        if 10**e>n:
            break
    p = n//(10**(e-1))
    if e==1:
        s = i+j+k
        if s>1:
            return 0
        elif s==1:
            if i==1 and n>=7 or j==1 and n>=5 or k==1 and n>=3:
                return 1
            else:
                return 0
        else:
            if n>=7:
                return 3
            elif n>=5:
                return 2
            elif n>=3:
                return 1
            else:
                return 0
    sm = 0
    x = 10**(e-1)-1
    y = n-p*(10**(e-1))
    if p>7:
        sm += dfs(x,0,j,k,0)
    elif p==7:
        sm += dfs(y,0,j,k,0)
    if p>5:
        sm += dfs(x,i,0,k,0)
    elif p==5:
        sm += dfs(y,i,0,k,0)
    if p>3:
        sm += dfs(x,i,j,0,0)
    elif p==3:
        sm += dfs(y,i,j,0,0)
    if f>0:
        sm += dfs(x,i,j,k,1)
    h[(n,i,j,k,f)] = sm
    
    return sm

n = inn()
print(dfs(n,1,1,1,1))

Solution (Python):
import itertools
import collections
n=int(input())
l=len(str(n))
cnt=0
if l<3:
  print(0)
  exit()
for i in range(3,l+1):
  for k in itertools.product(['7','5','3'],repeat=i):
    j=int(''.join(k))
    k=list(k)
    x=k.count('7')
    y=k.count('5')
    z=k.count('3')
    if all(vv!=0 for vv in [x,y,z]) and j<=n:
      cnt+=1
print(cnt)

Solution (Python):



from collections import deque

N = int(input())

item = ["3","5","7"]

d = deque([])
Order = 3
for _ in range(0,Order):
    d.append(item[0])

cand = []
cand.append("".join(list(d)))

while True:

    if d == deque([]):
        Order += 1
        for _ in range(0,Order):
            d.append(item[0])

    now = d.pop()

    if now == item[0]:
        d.append(item[1])
        while len(d) < Order:
            d.append(item[0])
        cand.append("".join(list(d)))

    elif now == item[1]:
        d.append(item[2])
        while len(d) < Order:
            d.append(item[0])
        cand.append("".join(list(d)))

    elif now == item[2]:
        continue

    
    if int("".join(list(d))) > N:
        break

if int(cand[-1]) > N:
    del cand[-1]

cnt = 0
for Num in cand:
    if Num.count("3") != 0 and Num.count("5") != 0 and Num.count("7") != 0:
        cnt += 1

print(cnt)

Solution (Python):
import itertools

N = input()
dig = len(N)
N = int(N)



A = ['3','5','7']

def count_num(A,n):
  Comb = itertools.product(A, repeat=n)
  cnt = 0
  for X in Comb:
    X = list(X)
    X_num = int(''.join(X))
    if(X_num <= N):
      j3,j5,j7 = 0,0,0
      for x in X:   
        if(x == '3'): j3 = 1
        elif(x == '5'): j5 = 1
        elif(x == '7'): j7 = 1
      if(j3*j5*j7 == 1):
        cnt += 1
  return cnt      

ans = 0
for i in range(3,dig+1):
  ans += count_num(A,i)
print(ans)

Solution (Python):
from collections import deque
from itertools import product

N = int(input())
dig = len(str(N))
s = ['3','5','7']
d = deque([''.join(x) for x in product(s,repeat=3)])
ans = 0
while d:
    temp = d.popleft()
    if len(temp) > dig:
        break
    else:
        if int(temp) <= N:
            if len(set(temp)) == 3:
                ans += 1
        d.append(''.join(('3',temp)))
        d.append(''.join(('5',temp)))
        d.append(''.join(('7',temp)))
print(ans)

Solution (Python):
N = int(input())


def dfs(numbers, appeared):
    global count
    if int(numbers) <= N:
        if appeared[0] and appeared[1] and appeared[2]:
            count += 1

        tmp1 = appeared.copy()
        tmp1[0] = True
        dfs(numbers + "3", tmp1)
        tmp2 = appeared.copy()
        tmp2[1] = True
        dfs(numbers + "5", tmp2)
        tmp3 = appeared.copy()
        tmp3[2] = True
        dfs(numbers + "7", tmp3)


count = 0
dfs("3", [True, False, False])
dfs("5", [False, True, False])
dfs("7", [False, False, True])

print(count)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def func(S, char):
    l = len(char)
    if int(S[0]) in char:
        num = 0
        for c in char:
            if int(c) < int(S[0]): num += 1
        if len(S) == 1: return num + 1
        return num * l ** (len(S) - 1) + func(S[1:], char)
    else:
        num = 0
        for c in char:
            if int(c) < int(S[0]): num += 1
        return num * l ** (len(S) - 1)

def main():
    S = input()
    ans = func(S, [3, 5, 7]) - func(S, [3, 5]) - func(S, [5, 7]) - func(S, [3, 7]) + func(S, [3]) + func(S, [5]) + func(S, [7])
    for i in range(1, len(S)):
        n = len(S) - i
        ans += 3 ** n - 3 * 2 ** n + 3
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
ans=0
from itertools import product
s=[]
for i in range(3,10):
  s+=list(product("357",repeat=i))
for i in range(0,len(s)):
  if "3" in s[i] and "5" in s[i] and "7" in s[i]:
    if n>=int("".join(s[i])):
      ans=ans+1
print(ans)

Solution (Python):
import itertools as it
n = int(input())
l = len(str(n))

res = []
for i in range(3, l + 1):
    for gen in it.product("357", repeat=i):
        gen_s = "".join(gen)
        if int(gen_s) <= n:
            res.append(gen_s)

cnt = 0
for r in res:
    if all(r.count(c) for c in "357"):
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
lis = [str(3),str(5),str(7)]
nu = [str(3),str(5),str(7)]
for i in range(8):
    nlis = []
    for j in lis:
        for k in nu:
            nlis.append(j+k)
    lis.extend(nlis)
slis = set(lis)
mlis = []
cnt = 0
for i in slis:
    if "3" not in i or "5"  not in i or "7" not in i:
        continue
    if int(i)>n: continue
    cnt+=1
    mlis.append(i)
print(cnt)

Solution (Python):
n = int(input())
ans = 0
def dfs(m):
  global ans
  if m > n:
    return
  sm = str(m)
  if "3" in sm and "5" in sm and "7" in sm:
    ans += 1
  dfs(m*10+3)
  dfs(m*10+5)
  dfs(m*10+7)
dfs(0)
print(ans)

Solution (Python):
import copy
n = int(input())
ans = 0
for i in range(2,10):
  list = ["3","5","7"]
  for j in range(i):
    dlist = []
    for k in list:
      for l in ("3","5","7"):
        dlist.append(k+l)
    list = copy.copy(dlist)
  for m in list:
    if "3" in m and "5" in m and "7" in m:
      if int(m) <= n:
        ans +=1
      else:
        print(ans)
        exit()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5)

N = int(input())

def check(p):
  return ("3" in p and "5" in p and "7" in p)
  
cnt = 0
def dfs(n):
  if int(n) > N:
    return
  global cnt
  if check(n):
    cnt += 1
  for c in ("3","5","7"):
    dfs(n + c)

dfs("0")
print(cnt)

Solution (Python):
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"))

Solution (Python):
from collections import deque
n = int(input())
d = deque()
d.append("3")
d.append("5")
d.append("7")
p = ["3","5","7"]
ans = 0
while d:
    now = d.popleft()
    for i in p:
        d.append(now+i)
    if now.count("3")*now.count("5")*now.count("7") > 0 and int(now) <= n:
        ans += 1
    if int(now) > n:
        break
print(ans)

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def bfs(N):
    q = deque(["7", "5", "3"])
    res = []

    while q:
        s = q.popleft()

        for c in ["7", "5", "3"]:
            new = s + c

            if int(new) > N:
                continue

            if "7" in new and "5" in new and "3" in new:
                res.append(new)

            q.append(new)

    return len(res)


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

    ans = bfs(N)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
n=int(input())

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

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
ns = lambda: stdin.readline().rstrip()
na = lambda: list(map(int, stdin.readline().split()))


N = ni()
ans = 0
def dfs(n):
    global ans
    if n > N:
        return

    if '3' in str(n) and '5' in str(n) and '7' in str(n):
        ans += 1

    dfs(n*10+3)
    dfs(n*10+5)
    dfs(n*10+7)

dfs(0)
print(ans)

Solution (Python):
N = int(input())
ans = set()
for length in range(3, len(str(N)) + 1):
    for t in range(3 ** length):
        xs = [[3, 5, 7][t // 3**i % 3] for i in range(length)]
        if 3 in xs and 5 in xs and 7 in xs:
            n = int(''.join(map(str, xs)))
            if 1 <= n <= N:
                ans.add(n)
print(len(ans))

Solution (Python):
N=int(input())
def main(number):
   if int(number) > N:
      return 0
   ret = 1 if all (number.count(c) > 0 for c in '753') else 0
   for c in "753":
      ret+=main(number+c)
   return ret
print(main("0"))

Solution (Python):
n=int(input())
def f(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+=f(s+c)
    return ret
print(f('0'))

Solution (Python):
n = int(input())
inf = 1001001001
lis = [7, 5, 3]
ans = 0
def dfs(i):
    if i > inf:
        return
    else:
        if i <= n and "7" in str(i) and "5" in str(i) and "3" in str(i):
            global ans
            ans += 1
        for j in lis:
            dfs(10*i + j)
dfs(0)
print(ans)

Solution (Python):
N = int(input())
 
def dfs(s):
    if int(s) > N:
        return 0
    if all(s.count(i) > 0 for i in '753'):
        score = 1
    else:
        score = 0
    for c in '753':
        score += dfs(s+c)
    return score
 
print(dfs('0'))

Solution (Python):
n = int(input())

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

Solution (Python):
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue



ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def dfs(S, N):
    if int(S) > N:
        return 0

    ret = 1 if all(S.count(s) for s in '753') else 0
    for s in '753':
        ret += dfs(S+s, N)
    return ret


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    print(dfs('0', N))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())

def dfs(s):
  if int(s) > N:
    return 0
  count = 1 if all(s.count(x) > 0 for x in ["7","5","3"]) else 0 

  for i in ["7","5","3"]:
    count += dfs(s+i)

  return count


print(dfs("0"))

Solution (Python):
N = input()
le = len(N)
N = int(N)
ans = 0
L = ['3', '5', '7']


def clear(n):
    global ans

    if len(n) > le:
        return
    if ('3' in n) and ('5' in n) and ('7' in n) and int(n) <= N:
        ans += 1

    for l in L:
        n = n + l
        clear(n)
        n = n[:-1]


clear('')
print(ans)

Solution (Python):
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'))

Solution (Python):
def main():
    N = input()
    n = int(N)

    max_l = len(N) + 1

    s = set()

    def search(i, n, f3, f5, f7):
        if f3 and f5 and f7:
            s.add(n)
        if i < 0:
            return

        i -= 1
        search(i, n * 10 + 3, True, f5, f7)
        search(i, n * 10 + 5, f3, True, f7)
        search(i, n * 10 + 7, f3, f5, True)

    search(max_l, 0, False, False, False)

    count = 0
    for v in s:
        if v <= n:
            count += 1

    print(count)


main()

Solution (Python):
n = int(input())
l = [3, 5, 7]
ans = 0

for i in range(26484):
    x = str(l[i])
    l.append(int(x+"3"))
    l.append(int(x+"5"))
    l.append(int(x+"7"))

for i in l:
    if i > n:
        print(ans)
        break
    
    x = str(i)
    if x.count("3")>0 and x.count("5")>0 and x.count("7")>0:
        ans += 1

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline


def bfs(N):
    res = 0
    queue = deque(["3", "5", "7"])
    while queue:
        num = queue.popleft()
        if int(num) > N:
            break
        res += 1 if all(d in num for d in "357") else 0
        queue.append("".join([num, "3"]))
        queue.append("".join([num, "5"]))
        queue.append("".join([num, "7"]))
    return res


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

    ans = bfs(N)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline


def bfs(N):
    res = 0
    queue = deque(["3", "5", "7"])
    while queue:
        num = queue.popleft()
        if int(num) > N:
            break
        if all(("3" in num, "5" in num, "7" in num)):
            res += 1
        queue.append("".join([num, "3"]))
        queue.append("".join([num, "5"]))
        queue.append("".join([num, "7"]))
    return res


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

    ans = bfs(N)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
def solve():
    N = int(input())
    ans = 0
    d = deque([3,5,7])
    while len(d):
        n = d.popleft()
        for i in range(3,8,2):
            a = n*10+i
            if a<=N:
                if len(set(list(str(a))))==3:
                    ans += 1
                d.append(a)
    return ans
print(solve())

Solution (Python):
n = int(input())
if n < 3:
    print(0)
    exit()
elif n < 5:
    print(1)
    exit()
elif n < 7:
    print(2)
    exit()
sft = []
tmp = ['33','35','37','53','55','57','73','75','77']
while True:
    a = []
    for j in tmp:
        t = set(j)
        if "5" in t and "7" in t: sft.append(j + "3")
        if "3" in t and "7" in t: sft.append(j + "5")
        if "5" in t and "3" in t: sft.append(j + "7")
        a.append(j + "3")
        a.append(j + "5")
        a.append(j + "7")
    tmp = a

    if int(sft[-1]) > n:
        for i in range(len(sft)):
            if int(sft[i]) > n:
                print(i)
                exit()

Solution (Python):
N = int(input())

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

print(dfs('0'))

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))


def resolve():
    N=input()
    n=len(N)
    N=int(N)
    ans=set()
    numans=0
    stack=[(3,1),(5,2),(7,4)]
    row=(3,5,7)
    while (stack!=[]):
        t,u=stack.pop()
        for n,l in enumerate(row):
            neu=(t*10+l,u|(2**n))
            if neu[0]<=N:
                stack.append(neu)
                if neu[1]==7:
                    ans.add(neu[0])
    print(len(ans))

resolve()

Solution (Python):
N = int(input())

ans = 0
def dfs(s):
    global ans
    if int(s) > N:
        return
    if all(s.count(i) for i in '753'):
        ans += 1
    for c in '753':
        dfs(s+c)
        
dfs('0')
print(ans)

Solution (Python):

import itertools

N = int(input())
ans = 0

for i in range(10):
    for j in itertools.product(['3','5','7'], repeat = i):
        if j.count('3') > 0 and j.count('5') and j.count('7'):
            if int(''.join(map(str,j))) <= N:
                ans += 1

print(ans)

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True




def warshall_floyd(d, n):
    
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d



def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)






def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret



n = int(input())

























m = len(str(n)) 


x = [0 for i in range(9)]
for i in range(3, 9):
    x[i] = 3 ** i  - (2 ** i - 2) * 3 - 3


ans = 0
for i in range(m):
    ans += x[i]

def dfs(cnt, ret, n, m):
    if cnt == m:
        
        cnt7 = 0
        cnt5 = 0
        cnt3 = 0
        for i in ret:
            if i == "7":
                cnt7 += 1
            elif i == "5":
                cnt5 += 1
            else:
                cnt3 += 1
        if cnt7 == 0 or cnt5 == 0 or cnt3 == 0:
            return
        if int(ret) <= n:
            global ans
            ans += 1
        return
    dfs(cnt + 1, ret + "7", n, m)
    dfs(cnt + 1, ret + "5", n, m)
    dfs(cnt + 1, ret + "3", n, m)
    return 

dfs(0, "", n, m)

print(int(ans))

Solution (Python):
import sys
sys.setrecursionlimit(2000000)

n=int(input())
three=['3','5','7']


ans=[]
def search(dfs):
    newdfs=[]
    for moto in dfs:
        for i in three:
            new=moto+i
            threeok=[False for k in range(3)]
            if int(new)>n:
                return
            for m in list(new):
                for j in range(3):
                    if m==three[j]:
                        threeok[j]=True
                        break
            if False not in threeok:
                ans.append(new)
            newdfs.append(new)
    search(newdfs)

search(three)

print(len(ans))

Solution (Python):






import sys
input = sys.stdin.readline

import math
import bisect
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def Yes(): print("Yes"); return
def No(): print("No"); return


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count





def Base_10_to_n(X, n):
  if (int(X/n)):
    return Base_10_to_n(int(X/n), n)+str(X%n)
  return str(X%n)

N = inputI()

dic = [[3,5,7]]


for _ in range(8):
  temp = []
  for v in dic[-1]:
    temp.append(v*10+3)
    temp.append(v*10+5) 
    temp.append(v*10+7)
  dic.append(temp)

x = []
for item in dic:
  x += item
nx = []
for i in x:
  if len(set(list(str(i)))) == 3:
    nx.append(i)

print(bisect.bisect(nx,N))

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

N = getN()
rength = len(str(N))
numlist = [3, 5, 7]
cnt = 0

def sevfivthr(i, strint):
    global cnt
    if i == rength:
        return
    else:
        for num in numlist:
            newstr = strint + str(num)
            if ('3' in newstr) and ('5' in newstr) and ('7' in newstr):
                if int(newstr) <= N:
                    cnt += 1
            sevfivthr(i + 1, newstr)
for i in numlist:
    sevfivthr(1, str(i))
print(cnt)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


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

    def f(s):
        if int(s) > N:
            return 0
        p = 0
        if len(set(list(s))) == 3:
            p = 1
        return f(s + "3") + f(s + "5") + f(s + "7") + p

    answer = f("3") + f("5") + f("7")
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    q = deque([3, 5, 7])
    answer = 0
    d = 3
    while d <= N:
        d = q.popleft()
        if len(set(list(str(d)))) == 3 and d <= N:
            answer += 1
        q.append(d * 10 + 3)
        q.append(d * 10 + 5)
        q.append(d * 10 + 7)
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def bfs(N):
    q = deque(["3", "5", "7"])

    cnt = 0
    while q:
        s = q.popleft()

        if int(s) > N:
            continue

        if len(set(s)) == 3:
            cnt += 1

        for c in ["3", "5", "7"]:
            q.append(s + c)

    return cnt


def main():
    N = int(input())
    ans = bfs(N)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
ans = 0

def dfs(current_num, seven, five, three, n):
    if current_num > n:
        return
    
    if seven == 1 & five == 1 & three == 1:
        global ans
        ans += 1
    
    dfs(10 * current_num + 7, 1, five, three, n)
    dfs(10 * current_num + 5, seven, 1, three, n)
    dfs(10 * current_num + 3, seven, five, 1, n)

def solve_C():
    n = int(input())

    dfs(0, 0, 0, 0, n)
    return ans

print(solve_C())

Solution (Python):
N = int(input())
count = 0
item = [3, 5, 7]

def dfs(s):
    global count
    if s > N:
        return
    string = str(s)
    if '3' in string and '5' in string and '7' in string:
        count += 1
    dfs(10*s + 3)
    dfs(10*s + 5)
    dfs(10*s + 7)

dfs(0)
print(count)

Solution (Python):
N = int(input())
import sys
sys.setrecursionlimit(500000)
def dfs(l):
    global N

    if l!=[] and N<int("".join(l)):
        return 0
    ans = 0
    if all(l.count(i)>0 for i in ["3", "5", "7"]):
        ans = 1
    for i in ["3", "5", "7"]:
        l.append(i)
        
        ans += dfs(l)
        l.pop()

    return ans

print(dfs([]))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = input()

a = [3,5,7]

cnt = 0

for i in range(3, len(N)):
	for x in product(a, repeat=i):
		
		if len(set(x)) == 3:
			cnt += 1
			

for x in product(a, repeat=len(N)):
	
	if len(set(x)) == 3:
		
		p = 0
		for i in range(len(N)):
			p += x[-(i+1)]*10**i
		if int(N) < p:
			print(cnt)
			break
		cnt += 1
else:
	print(cnt)