Problem: Example

Input

20


Output

4

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll Asum,Aprod,Aint;
int n;
string A,B;

ll nCr(int n, int r){
  if(r==0||n==r)return 1;
  ll a=1;
  for(int i=0;i<r;i++) a*=(n-i);
  for(int i=0;i<r;i++) a/=(i+1);
  return a;
}

ll Sum(string str){
  ll res=0;
  for(int i=0;i<str.size();i++)res+=str[i]-'0';
  return res;
}

ll Prod(string str){
  ll res=1;
  for(int i=0;i<str.size();i++)res*=str[i]-'0'+1;
  return res;
}

ll Int(string str){
  ll res=0;
  for(int i=0;i<str.size();i++)res=res*10+str[i]-'0';
  return res;
}

ll permutation(string str){
  ll n=str.size(),res=1;
  for(int i=0,cnt=1;i<n;i++){
    if(str[i]==str[i+1])cnt++;
    else res*=nCr(n-i+cnt-1,cnt),cnt=1;
  }
  return res;
}

ll permutation(vector<int> a){
  string str;
  for(int i=0;i<a.size();i++)while(a[i]--)str+=(char)(i+'0');
  return  permutation(str);
}

ll count(){
  ll res=0;
  vector<int>cnt(10);
  for(int i=0;i<n;i++)cnt[B[i]-'0']++;

  for(int i=0;i<n;i++){
    for(int j=0;j<A[i]-'0';j++)
      if(cnt[j])cnt[j]--,res+=permutation(cnt),cnt[j]++;
    if(!cnt[A[i]-'0'])break;
    cnt[A[i]-'0']--;
  }
  return res;
}

ll calc(){
  if(Asum!=Sum(B)) return (Sum(B)<Asum)*permutation(B);
  if(Aprod!=Prod(B))return (Prod(B)<Aprod)*permutation(B);
  return count();
}

ll dfs(int idx,int num,int sum){
  if(idx==n) return calc();
  if(num==10||idx==n) return 0;
  ll res=dfs(idx,num+1,sum);
  B[idx]=num+'0';
  return res+dfs(idx+1,num,sum+num);
}

int main(){
  cin>>A;
  n=A.size();
  Asum=Sum(A),Aprod=Prod(A),Aint=Int(A);
  B.resize(n);
  cout<<dfs(0,0,0)<<endl;
  return 0;
}