Problem:
Problem E
Bringing Order to Disorder
A sequence of digits usually represents a number, but we may define an alternative interpretation. In this problem we define a new interpretation with the order relation $\prec$ among the digit sequences of the same length defined below.
Let $s$ be a sequence of $n$ digits, $d_1d_2 ... d_n$, where each $d_i$ $(1 \leq i \leq n)$ is one of 0, 1, ... , and 9. Let sum($s$), prod($s$), and int($s$) be as follows:
sum($s$) = $d_1 + d_2 + ... + d_n$
prod($s$) = $(d_1 + 1) \times (d_2 + 1) \times  ... \times (d_n + 1)$
int($s$) = $d_1 \times 10^{n-1} + d_2 \times 10^{n-2} + ... + d_n \times 10^0$
int($s$) is the integer the digit sequence $s$ represents with normal decimal interpretation.
Let $s_1$ and $s_2$ be sequences of the same number of digits. Then $s_1 \prec s_2$ ($s_1$ is less than $s_2$) is satisfied if and only if one of the following conditions is satisfied.
sum($s_1$) $<$ sum($s_2$)
sum($s_1$) $=$ sum($s_2$) and prod($s_1$) $<$ prod($s_2$)
sum($s_1$) $=$ sum($s_2$), prod($s_1$) $=$ prod($s_2$), and int($s_1$) $<$ int($s_2$)
For 2-digit sequences, for instance, the following relations are satisfied.
$00 \prec 01 \prec 10 \prec 02 \prec 20 \prec 11 \prec 03 \prec 30 \prec 12 \prec 21 \prec ... \prec 89 \prec 98 \prec 99$
Your task is, given an $n$-digit sequence $s$, to count up the number of $n$-digit sequences that are less than $s$ in the order $\prec$ defined above.
Input
The input consists of a single test case in a line.
$d_1d_2 ... d_n$
$n$ is a positive integer at most 14. Each of $d_1, d_2, ...,$  and $d_n$ is a digit.
Output
Print the number of the $n$-digit sequences less than $d_1d_2 ... d_n$ in the order defined above.
Sample Input 1
20
Sample Output 1
4
Sample Input 2
020
Sample Output 2
5
Sample Input 3
118
Sample Output 3
245
Sample Input 4
11111111111111
Sample Output 4
40073759
Sample Input 5
99777222222211
Sample Output 5
23733362467675

Submitted solutions:

code_snippet: #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <queue>
#include <cmath>
#include <vector>
#define CLR0(a) (memset(a, 0, sizeof(a)))
#define CLR1(a) (memset(a, -1, sizeof(a)))
#define CLRf(a) (memset(a, 0x3f, sizeof(a)))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;


map<ll, ll> dp[20][140];
char digit[20];
ll inn, inm;
int len, ins;
ll dfs(int le, int sum, ll mu, bool fp)
{
	if (le >= len)
	{
		if (sum != ins)return 0;
		if (mu <= inm) { return 1; }
		return 0;
	}
	if (!fp && dp[le][sum].find(mu) != dp[le][sum].end())return dp[le][sum][mu];
	ll ret = 0;
	int fpmax = fp ? digit[le] - '0' : 9;
	for (int i = 0; i <= fpmax; i++)
	{
		if (sum + i > ins)continue;
		if (mu*(1 + i) > inm)continue;
		ret += dfs(le + 1, sum + i, mu*(1 + i), fp && (i == fpmax));
	}
	if (!fp)dp[le][sum][mu] = ret;
	return ret;
}

ll C(int n, int m)
{
	if (m < 0) return 0;
	if (n < m) return 0;
	if (n == m || m == 0) return 1;
	ll s = 1;
	for (int i = n, j = 1; j <= m; --i, ++j)
		s = s*i / j;
	return s;
}

map<ll, ll> dp2[20][140];
ll dfs2(int le, int sum, ll mu, bool fp)
{
	if (le >= len)
	{
		if (sum != ins)return 0;
		if (mu < inm) { return 1; }
		return 0;
	}
	if (!fp && dp2[le][sum].find(mu) != dp2[le][sum].end())return dp2[le][sum][mu];
	ll ret = 0;
	int fpmax = fp ? digit[le] - '0' : 9;
	for (int i = 0; i <= fpmax; i++)
	{
		if (sum + i > ins)continue;
		if (mu*(1 + i) >= inm)continue;
		ret += dfs2(le + 1, sum + i, mu*(1 + i), fp && (i == fpmax));
	}
	if (!fp)dp2[le][sum][mu] = ret;
	return ret;
}

int main()
{
	scanf("%s", digit);
	len = strlen(digit);
	inn = 0; inm = 1; ins = 0;
	ll ans = 0;
	for (int i = 0; i < len; i++)
	{
		inm *= (1 + digit[i] - '0');
		ins += digit[i] - '0';
		inn *= 10;
		inn += digit[i] - '0';
	}

	for (int i = 0; i < ins; i++)
	{
		for (int j = 0; j <= len; j++)
			ans += C(i + len - 1 - 10 * j, len - 1)*C(len, j)*(j % 2 ? -1 : 1);
	}
	ans += dfs(0, 0, 1, 1) - 1;

	ll a1 = dfs2(0, 0, 1, 1);
	for (int i = 0; i < len; i++)digit[i] = '9';
	ll a2 = dfs2(0, 0, 1, 1);
	ans += a2 - a1;
	printf("%lld\n", ans);
	return 0;
}
accuracy: 1
time_ms: 690
memory_kb: 51656
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, ll> pr;

ll dp[20][150];
int n;
map<ll,ll>dp2[20][140];
map<ll,ll>dp3[20][140];
ll ten[20];
ll limit;
char s[20];

ll dfs(int dep,int sum,ll pro){//深度，剩余和，当前乘积,返回方案数
    if (pro>=limit) return 0;
    if (sum<0) return 0;
    if (sum>9*(n-dep)) return 0;
    if (dep==n)  return 1;
    if (dp2[dep][sum].find(pro)!=dp2[dep][sum].end()){
        return dp2[dep][sum][pro];
    }
    ll res=0;
    for (int i=0;i<=9;i++){
        res+=dfs(dep+1,sum-i,pro*(i+1));
    }
    return dp2[dep][sum][pro]=res;
}

ll dfs2(int dep,int sum,ll pro,bool fp){
    if (pro>ten[n-dep]) return 0;
    if (sum<0) return 0;
    if (sum>9*(n-dep)) return 0;
    if (dep==n) return 1ll;
    if (!fp&&dp3[dep][sum].find(pro)!=dp3[dep][sum].end()){
        return dp3[dep][sum][pro];
    }
    ll res=0;
    int up=fp?s[dep]-'0':9;
    for (int i=0;i<=up;i++){
        if (pro%(i+1)==0){
            res+=dfs2(dep+1,sum-i,pro/(i+1),i==up&&fp);
        }
    }
    if (fp) return res;
    return dp3[dep][sum][pro]=res;
}

int main() {
    ten[0]=1ll;
    for (int i=1;i<15;i++){
        ten[i]=ten[i-1]*10ll;
    }
    // 替换gets为fgets并处理换行符
    fgets(s, sizeof(s), stdin);
    // 移除可能的换行符
    s[strcspn(s, "\n")] = '\0';
    
    n=strlen(s);
    int sum=0;
    for (int i=0;i<n;i++){
        sum+=s[i]-'0';
    }
    for (int i=0;i<=9;i++){
        dp[0][i]=1;
    }
    for (int i=1;i<n;i++){
        for (int j=0;j<=140&&dp[i-1][j]>0;j++){
            for (int k=0;k<=9;k++){
                dp[i][j+k]+=dp[i-1][j];
            }
        }
    }
    ll ans=0;
    for (int i=0;i<sum;i++){
        ans += dp[n-1][i];
    }
    limit=1;
    for (int i=0;i<n;i++){
        limit*=s[i]-'0'+1;
    }
    ans+=dfs(0,sum,1);
    ans+=dfs2(0,sum,limit,true);
    printf("%lld\n",ans-1);
    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 16416
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
long long fact[20];

pair<long long, long long> score(string S) {
	long long sum = 0;
	long long prod = 1;
	for (int i = 0; i < S.size(); i++) {
		sum += (S[i] - '0');
		prod *= 1LL * (S[i] - '0' + 1);
	}
	return make_pair(sum, prod);
}

long long ways(string S) {
	vector<int> cnt(10, 0);
	for (int i = 0; i < S.size(); i++) cnt[S[i] - '0'] += 1;
	long long ret = fact[S.size()];
	for (int i = 0; i <= 9; i++) ret /= fact[cnt[i]];
	return ret;
}

long long anagram(string S, string Z) {
	long long sum = 0;
	for (int i = 0; i < S.size(); i++) {
		for (int d = 0; d <= (Z[i] - '0'); d++) {
			if (d == (Z[i] - '0')) {
				string T = S.substr(i, S.size() - i);
				bool flag = false;
				for (int j = 1; j < T.size(); j++) {
					if (T[j] == ('0' + d)) { flag = true; swap(S[i], S[i + j]); break; }
				}
			}
			else {
				string T = S.substr(i, S.size() - i);
				bool flag = false;
				for (int j = 0; j < T.size(); j++) {
					if (T[j] == ('0' + d)) { flag = true; swap(T[0], T[j]); break; }
				}
				if (flag == true) sum += ways(T.substr(1, T.size() - 1));
			}
		}
		if (S[i] != Z[i]) break;
	}
	return sum;
}

long long dfs(int remain_dep, string S, string Z, pair<long long, long long> goal) {
	if (remain_dep == 0) {
		pair<long long, long long> ret = score(S);
		return (ret < goal ? ways(S) : (ret == goal ? anagram(S, Z) : 0));
	}
	long long val = 0;
	for (int i = (S.size() == 0 ? 0 : (S.back() - '0')); i <= 9; i++) {
		string S2 = S; S2 += ('0' + i);
		val += dfs(remain_dep - 1, S2, Z, goal);
	}
	return val;
}

int main() {
	// step 1. input
	string S; cin >> S;
	pair<long long, long long> goal = score(S);
	fact[0] = 1;
	for (int i = 1; i <= 18; i++) fact[i] = 1LL * i * fact[i - 1];

	// step 2. output
	cout << dfs(S.size(), "", S, goal) << endl;
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3432
final_rank: 2

code_snippet: #include <iostream>
#include <iomanip>
#include <map>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
namespace ranges = std::ranges;
namespace views = std::views;
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
std::string S;
// 2, 3, 5, 7
using item = std::array<int, 4>;
const item add[10]{
    item{0,0,0,0},item{1,0,0,0},item{0,1,0,0},item{2,0,0,0},item{0,0,1,0},item{1,1,0,0},item{0,0,0,1},item{3,0,0,0},item{0,2,0,0},item{1,0,1,0}
};
long long pows[4][50];
int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cin >> S;
    const int n = std::ssize(S);
    const int s_sum = [&]() {
        int res = 0;
        for (int i = 0 ; i < n ; i++) res += S[i] - '0';
        return res;
    }();
    const long long s_prod = [&]() {
        long long res = 1;
        for (int i = 0 ; i < n ; i++) res *= S[i] - '0' + 1;
        return res;
    }();
    for (int j = 0 ; j < 4 ; j++) pows[j][0] = 1;
    for (int i = 1 ; i <= 3*n ; i++) {
        const int mult[4]{2,3,5,7};
        for (int j = 0 ; j < 4 ; j++) {
            pows[j][i] = pows[j][i - 1] * mult[j];
        }
    }
    auto prod = [&](const item& d) -> long long {
        return pows[0][d[0]] * pows[1][d[1]] * pows[2][d[2]] * pows[3][d[3]];
    };
    auto valid = [&](int state, int sum, const item& d) -> bool {
        if (sum != s_sum) return sum < s_sum;
        const long long prd = prod(d);
        if (prd != s_prod) return prd < s_prod;
        return state == 1;
    };
    std::vector dp(n + 1, std::vector(3, std::vector<std::map<item, long long>>(9*n+1)));
    auto rec = [&](auto rec, int dig, int comp, int sum, const item& d) -> long long {
        const auto it = dp[dig][comp][sum].find(d);
        if (it != dp[dig][comp][sum].end()) return it->second;
        long long& res = dp[dig][comp][sum][d] = 0; 
        if (dig == n) {
            // if (valid(comp, sum, d)) std::cout << comp << ' ' << sum << ' ' << prod(d) << " : " << d[0] << ' ' << d[1] << ' ' << d[2] << ' ' << d[3] << '\n';
            return res = valid(comp, sum, d);
        }
        for (int i = 0 ; i <= 9 ; i++) {
            int nextcomp = comp;
            if (comp == 0 and i < S[dig] - '0') nextcomp = 1;
            if (comp == 0 and i > S[dig] - '0') nextcomp = 2;
            item nextd = d;
            for (int j = 0 ; j < 4 ; j++) nextd[j] += add[i][j];
            res += rec(rec, dig + 1, nextcomp, sum + i, nextd);
        }
        return res;
    };
    std::cout << rec(rec, 0, 0, 0, item{0,0,0,0}) << '\n';
}
accuracy: 1
time_ms: 890
memory_kb: 75008
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){
	string S; cin>>S;
	int N=S.size();

	int dsum=0;
	ll dpr=1;
	for(char c:S)dsum+=c-'0',dpr*=(c-'0')+1;

	ll ans=0;
	{
		int M=N*9;
		vector<vector<ll>>dp(N+1,vector<ll>(M+1));
		dp[0][0]=1;
		for(int i=0;i<N;i++){
			for(int j=0;j<=M;j++){
				for(int k=0;k<10;k++){
					if(j+k<=M){
						dp[i+1][j+k]+=dp[i][j];
					}
				}
			}
		}

		for(int i=0;i<dsum;i++)ans+=dp.back()[i];
	}

	//0 less, 1 eq, 2 greater
	map<tuple<int,int,ll,int>,ll>memo;
	auto rec=[&](auto&&rec,int idx,int sum,ll prod,int state)->ll{
		tuple<int,int,ll,int> now={idx,sum,prod,state};
		if(memo.count(now))return memo[now];
		if(idx==N){
			assert(sum==dsum);
			if(prod>dpr)return 0;
			if(prod==dpr)return state==0;
			return 1;
		}

		int rem=N-idx-1;
		ll ret=0;
		if(state!=1){
			for(int i=0;i<10&&sum+i<=dsum;i++){
				if(sum+i+rem*9<dsum)continue;
				if(prod*(i+1)>dpr)continue;
				ret+=rec(rec,idx+1,sum+i,prod*(i+1),state);
			}
		}else{
			for(int i=0;i<10&&sum+i<=dsum;i++){
				if(sum+i+rem*9<dsum)continue;
				if(prod*(i+1)>dpr)continue;
				int nstate=i<S[idx]-'0'?0:i==S[idx]-'0'?1:2;
				ret+=rec(rec,idx+1,sum+i,prod*(i+1),nstate);
			}
		}
		return memo[now]=ret;
		return ret;
	};

	cout<<ans+rec(rec,0,0,1,1)<<endl;
}
accuracy: 1
time_ms: 550
memory_kb: 31456
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  cin.tie(0)->sync_with_stdio(0);
  using ll=long long;
  string s;cin>>s;
  int n=s.size();

  int sum0=0;
  for(int i=0;i<n;++i){
    sum0+=s[i]-'0';
  }

  ll prod0=1;
  for(int i=0;i<n;++i){
    prod0*=(s[i]-'0')+1;
  }

  ll ans=0;
  {
    vector dp(n+1,vector<unordered_map<ll,ll>>(9*n+1));
    dp[0][0][1]=1;
    for(int i=0;i<n;++i){
      for(int j=0;j<9*n;++j){
        for(auto[key,value]:dp[i][j]){
          if(value==0)continue;
          for(int k=0;k<=9;++k){
            dp[i+1][j+k][key*(k+1)]+=value;
          }
        }
      }
    }
    for(int i=0;i<sum0;++i){
      for(auto[key,value]:dp[n][i])ans+=value;
    }
    for(auto[key,value]:dp[n][sum0]){
      if(key<prod0)ans+=value;
    }
  }
  {
    vector dp(n+1,vector<vector<unordered_map<ll,ll>>>(2,vector<unordered_map<ll,ll>>(9*n+1)));
    dp[0][0][0][1]=1;
    for(int i=0;i<n;++i){
      for(int j=0;j<9*n;++j){
        for(auto[key,value]:dp[i][0][j]){
          if(value==0)continue;
          dp[i+1][0][j+(s[i]-'0')][key*((s[i]-'0')+1)]+=value;
          for(int k=0;k<s[i]-'0';++k){
            dp[i+1][1][j+k][key*(k+1)]+=value;
          }
        }
        for(auto[key,value]:dp[i][1][j]){
          if(value==0)continue;
          for(int k=0;k<=9;++k){
            dp[i+1][1][j+k][key*(k+1)]+=value;
          }
        }
      }
    }
    ans+=dp[n][1][sum0][prod0];
  }
  cout<<ans<<endl;

  /*
  {
    set<ll>dp;
    dp.insert(1);
    for(int i=0;i<14;++i){
      set<ll>ep;
      for(auto x:dp){
        for(int j=1;j<=10;++j){
          ep.insert(x*j);
        }
      }
      swap(dp,ep);
    }
    cout<<dp.size()<<endl;
  }
  */
}
accuracy: 1
time_ms: 230
memory_kb: 28180
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  cin.tie(0)->sync_with_stdio(0);
  using ll=long long;
  string s;cin>>s;
  int n=s.size();

  int sum0=0;
  for(int i=0;i<n;++i){
    sum0+=s[i]-'0';
  }

  ll prod0=1;
  for(int i=0;i<n;++i){
    prod0*=(s[i]-'0')+1;
  }

  ll ans=0;
  {
    vector dp(n+1,vector<map<ll,ll>>(9*n+1));
    dp[0][0][1]=1;
    for(int i=0;i<n;++i){
      for(int j=0;j<9*n;++j){
        for(auto[key,value]:dp[i][j]){
          if(value==0)continue;
          for(int k=0;k<=9;++k){
            dp[i+1][j+k][key*(k+1)]+=value;
          }
        }
      }
    }
    for(int i=0;i<sum0;++i){
      for(auto[key,value]:dp[n][i])ans+=value;
    }
    for(auto[key,value]:dp[n][sum0]){
      if(key<prod0)ans+=value;
    }
  }
  {
    vector dp(n+1,vector<vector<map<ll,ll>>>(2,vector<map<ll,ll>>(9*n+1)));
    dp[0][0][0][1]=1;
    for(int i=0;i<n;++i){
      for(int j=0;j<9*n;++j){
        for(auto[key,value]:dp[i][0][j]){
          if(value==0)continue;
          dp[i+1][0][j+(s[i]-'0')][key*((s[i]-'0')+1)]+=value;
          for(int k=0;k<s[i]-'0';++k){
            dp[i+1][1][j+k][key*(k+1)]+=value;
          }
        }
        for(auto[key,value]:dp[i][1][j]){
          if(value==0)continue;
          for(int k=0;k<=9;++k){
            dp[i+1][1][j+k][key*(k+1)]+=value;
          }
        }
      }
    }
    ans+=dp[n][1][sum0][prod0];
  }
  cout<<ans<<endl;

  /*
  {
    set<ll>dp;
    dp.insert(1);
    for(int i=0;i<14;++i){
      set<ll>ep;
      for(auto x:dp){
        for(int j=1;j<=10;++j){
          ep.insert(x*j);
        }
      }
      swap(dp,ep);
    }
    cout<<dp.size()<<endl;
  }
  */
}
accuracy: 1
time_ms: 480
memory_kb: 39508
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define srep(i, s, t) for(int i = (s); i < (t); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
using i64 = long long;
using f64 = long double;
i64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) <  0 && n % d != 0); }
i64  ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    string str; cin >> str;
    const int n = str.size();
    const int S = 9 * n;
    vector dp(S + 1, vector(3, map<i64, i64>{}));
    dp[0][1][1] = 1;
    for(char ds : str) {
        const int d = ds - '0';
        vector nt(S + 1, vector(3, map<i64, i64>{}));
        for(int s = 0; s <= S; s++) {
            for(auto [p, c] : dp[s][1]) {
                for(int x = 0; x <= 9; x++) {
                    if(x <  d) nt[s + x][0][p * (x + 1)] += c;
                    if(x == d) nt[s + x][1][p * (x + 1)] += c;
                    if(x >  d) nt[s + x][2][p * (x + 1)] += c;
                }
            }
            for(auto [p, c] : dp[s][0])
                for(int x = 0; x <= 9; x++)
                    nt[s + x][0][p * (x + 1)] += c;
            for(auto [p, c] : dp[s][2])
                for(int x = 0; x <= 9; x++)
                    nt[s + x][2][p * (x + 1)] += c;
        }
        dp = move(nt);
    }

    i64 SUM = 0, PROD = 1;
    for(char ds : str) {
        const int d = ds - '0';
        SUM += d;
        PROD *= (d + 1);
    }
    i64 ans = 0;
    for(int s = 0; s <= S; s++) {
        for(int f : {0, 1, 2}) {
            for(auto [p, c] : dp[s][f]) {
                bool ok = [&] {
                    if(s < SUM) return true;
                    if(s == SUM and p < PROD) return true;
                    if(s == SUM and p == PROD and f == 0) return true;
                    return false;
                }();
                if(ok) ans += c;
            }
        }
    }
    cout << ans << "\n";
}
accuracy: 1
time_ms: 450
memory_kb: 44492
final_rank: 15

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    string S;
    cin >> S;
    int N = S.size();
    vector<int> A(N);
    ll SUM = 0, PROD = 1, INT = 0;
    rep(i,0,N) A[i] = S[i] - '0', SUM += A[i], PROD *= (A[i]+1), INT *= 10, INT += A[i];
    vector<vector<map<ll,ll>>> DP(N+1,vector<map<ll,ll>>(N*9+1));
    DP[0][0][1] = 1;
    rep(i,0,N) {
        rep(j,0,i*9+1) {
            for (auto it = DP[i][j].begin(); it != DP[i][j].end(); it++) {
                rep(k,0,10) {
                    DP[i+1][j+k][it->first*(ll)(k+1)] += it->second;
                }
            }
        }
    }
    ll ANS = 0;
    rep(i,0,SUM) {
        for (auto it = DP[N][i].begin(); it != DP[N][i].end(); it++) {
            ANS += it->second;
        }
    }
    for (auto it = DP[N][SUM].begin(); it != DP[N][SUM].end(); it++) {
        if (it->first < PROD) ANS += it->second;
    }
    map<tuple<ll,ll,ll,bool>,ll> mp;
    auto DFS = [&](auto self, ll n, ll s, ll p, bool b) -> ll {
        if (n == N) {
            if (s == 0 && p == 1 && b) return 1;
            else return 0;
        }
        if (mp.count({n,s,p,b})) return mp[{n,s,p,b}];
        ll Ret = 0;
        rep(i,0,10) {
            if (p % (i+1) != 0) continue;
            if (s < i) continue;
            if (!b && A[n] < i) continue;
            if (!b && A[n] == i) {
                Ret += self(self,n+1,s-i,p/(i+1),false);
            }
            else {
                Ret += self(self,n+1,s-i,p/(i+1),true);
            }
        }
        return mp[{n,s,p,b}] = Ret;
    };
    ANS += DFS(DFS,0,SUM,PROD,false);
    cout << ANS << endl;
}
accuracy: 1
time_ms: 310
memory_kb: 44716
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, x) for (auto &e : x)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define eb emplace_back
#define MM << ' ' <<
#define TT template <typename T>
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

TT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
TT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;

TT void print(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (n == 0) cout << '\n';
}

TT void printn(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << '\n';
}

TT void rearrange(vector<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), end(v));
}

TT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }
TT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main() {
    string s;
    cin >> s;
    int n = sz(s);
    vector<int> ds(n);
    rep(i, n) ds[i] = s[i] - '0';
    int ss = 0;
    ll ps = 1;
    each(e, ds) ss += e, ps *= e + 1;
    vector<vector<unordered_map<ll, ll>>> cnt(n, vector<unordered_map<ll, ll>>(200));
    ll ans = 0;
    vector<ll> fac(n + 1, 1);
    rep(i, n) fac[i + 1] = fac[i] * (i + 1);
    vector<vector<ll>> po(10, vector<ll>(n + 1, 1));
    rep(i, 10) rep(j, n) po[i][j + 1] = po[i][j] * (i + 1);
    auto dfs = [&](int idx, int d, int ns, ll np, ll ifac, bool upd, auto &&dfs) ->void {
        if (idx == n) {
            if (pair(ns, np) < pair(ss, ps))
                ans += fac[n] / ifac;
            return;
        }
        if (upd)
            cnt[idx][ns][np] += fac[idx] / ifac;
        if (d == 10)
            return;
        rep(c, n + 1 - idx) dfs(idx + c, d + 1, ns + d * c, np * po[d][c], ifac * fac[c], c > 0, dfs);
    };
    dfs(0, 0, 0, 1, 1, true, dfs);
    rep(i, n) rep(j, ds[i]) {
        int ns = j;
        ll np = j + 1;
        rep(k, i) ns += ds[k], np *= ds[k] + 1;
        if (ps % np != 0)
            continue;
        ans += cnt[n - 1 - i][ss - ns][ps / np];
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 19908
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()
#define rng(i, l, r) for(int i = int(l); i < int(r); i++)
#define rep(i, n) rng(i, 0, n)
#define sz(v) int(v.size())
#define foa(s, v) for(auto& s : v)

string str;
void solve() {
	int n = sz(str);
	vector<int> ds(n);
	rep(i, n) ds[i] = int(str[i] - '0');

	vector<ll> prods = {1};

	rep(i, n) {
		int s = sz(prods);
		rep(j, s) {
			rep(p, 10) { prods.push_back((p + 1) * prods[j]); }
		}
		sort(all(prods));
		auto mid = unique(all(prods));
		prods.erase(mid, prods.end());
	}
	const int ps = sz(prods);

	auto pidx = [&](ll val) -> int {
		auto it = lower_bound(all(prods), val);
		assert(it != prods.end() && *it == val);
		return int(it - prods.begin());
	};

	const int smax = 9 * n + 1;

	// less: 0, eq: 1, greater: 2
	enum {
		LESS = 0,
		EQ = 1,
		GREATER = 2,
	};
	vector dp(3, vector(ps, vector<ll>(smax, 0LL)));
	dp[EQ][pidx(1LL)][0] = 1;

	foa(d, ds) {
		vector tmp(3, vector(ps, vector<ll>(smax, 0LL)));

		rep(c, 3) rep(p, ps) rep(s, smax) {
			ll pre = dp[c][p][s];
			if(!pre) continue;
			rep(nxt, 10) {
				int nxt_c = c;
				if(c == EQ) {
					if(nxt < d) {
						nxt_c = LESS;
					} else if(nxt > d) {
						nxt_c = GREATER;
					}
				}
				int nxt_p = pidx(prods[p] * (nxt + 1));
				int nxt_s = s + nxt;
				tmp[nxt_c][nxt_p][nxt_s] += pre;
			}
		}
		swap(dp, tmp);
	}

	int sum = accumulate(all(ds), 0);
	ll prod = 1;
	foa(d, ds) prod *= (d + 1);
	int pid = pidx(prod);
	ll ans = 0;
	rep(c, 3) rep(p, ps) rep(s, smax) {
		auto ok = [&]() {
			if(s != sum) return s < sum;
			if(p != pid) return p < pid;
			return c == LESS;
		};
		if(ok()) {
			ans += dp[c][p][s];
		}
	}

	cout << ans << endl;
	return;
}

int main() {
	while(cin >> str) solve();
}
accuracy: 1
time_ms: 520
memory_kb: 137772
final_rank: 20

code_snippet: #include<bits/stdc++.h>

using namespace std;

using ll=long long;

int main(){
    string s;cin>>s;
    int tot=0;
    ll PR=1;
    for(auto &c:s){
        tot+=(int)(c-'0');
        PR*=(ll)((c-'0')+1LL);
    }
    vector dp(tot+1,vector (3,map<ll,ll>()));
    dp[0][0][1]=1;
    for(auto c:s){
        int num=(c-'0');
        vector np(tot+1,vector (3,map<ll,ll>()));
        for(int i=0;i<=tot;i++){
            for(int j=0;j<3;j++){
                for(auto [pr,val]:dp[i][j]){
                    for(int x=0;x<10;x++){
                        ll pr2=pr*(x+1);
                        ll i2=i+x;
                        ll j2=j;
                        if(j==0){
                            if(x>num)j2=2;
                            if(x<num)j2=1;
                        }
                        if(i2>tot)continue;
                        np[i2][j2][pr2]+=val;
                    }
                }
            }
        }
        dp=move(np);
    }
    ll ans=0;
    for(int i=0;i<tot;i++){
        for(int j=0;j<3;j++){
            for(auto [_,val]:dp[i][j]){
                ans+=val;
            }
        }
    }
    for(int j=0;j<3;j++){
        for(auto [pr,val]:dp[tot][j]){
            if(pr<PR){
                ans+=val;
            }
            if(pr==PR){
                if(j==1)ans+=val;
            }
        }
    }
    cout<<ans<<'\n';
}
accuracy: 1
time_ms: 340
memory_kb: 28936
final_rank: 9

code_snippet: #include "bits/stdc++.h"
using namespace std;

typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))
#define ALL(x) (x).begin(), (x).end()

#define int long long
int comb(vector<int> go)
{
  int sums = 0;
  REP(i, go.size())
  {
    sums += go[i];
  }
  int ans = 1;
  REP(i, sums)
  {
    ans *= i + 1;
  }
  REP(i, go.size())
  {
    REP(q, go[i])
    {
      ans /= q + 1;
    }
  }
  return ans;
}

string s;
vector<vector<int>> kouho;
void gen_seq(vector<int> cur, int itr, int remaining)
{
  if (itr == 10)
  {
    if (remaining == 0)
      kouho.push_back(cur);
    return;
  }
  cur.push_back(0);
  REP(t, remaining + 1)
  {
    gen_seq(cur, itr + 1, remaining - t);
    cur.back()++;
  }
}

void solve()
{
  cin >> s;
  gen_seq({}, 0, s.length());
  int s_sum = 0;
  int s_mul = 1;
  REP(i, s.length())
  {
    s_sum += s[i] - '0';
    s_mul *= (s[i] - '0' + 1);
  }
  int ans = 0;
  for (auto x : kouho)
  {
    int sums = 0;
    int muls = 1;
    REP(i, 10)
    {
      REP(q, x[i])
      {
        sums += i;
        muls *= i + 1;
      }
    }
    if (sums > s_sum)
      continue;
    if (sums == s_sum)
    {
      if (s_mul < muls)
        continue;
      if (s_mul == muls)
      {
        // calc
        for (int i = 0; i < s.length(); ++i)
        {
          for (int q = 0; q < s[i] - '0'; ++q)
          {
            if (x[q] != 0)
            {
              x[q]--;
              ans += comb(x);
              x[q]++;
            }
          }
          if (x[s[i] - '0'] == 0)
            break;
          x[s[i] - '0']--;
        }
        continue;
      }
    }
    ans += comb(x);
  }
  cout << ans << endl;
}

#undef int

// generated by oj-template v4.7.2
// (https://github.com/online-judge-tools/template-generator)
int main()
{
  // Fasterize input/output script
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(100);
  // scanf/printf user should delete this fasterize input/output script

  int t = 1;
  // cin >> t; // comment out if solving multi testcase
  for (int testCase = 1; testCase <= t; ++testCase)
  {
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 100736
final_rank: 17

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

map<pair<int,ll>,ll> dp[15];

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false); 
    string s; cin >> s;
    int n = s.size();
    dp[0][{0,1}]++;
    vector<int> a(n);
    int sum = 0;
    ll pro = 1;
    for(int i=0;i<n;i++){
        a[i] = s[i]-'0';
        sum += a[i];
        pro *= (ll)(a[i]+1);
        for(auto &p:dp[i]){
            for(int j=0;j<10;j++){
                dp[i+1][{p.first.first+j,p.first.second*(j+1)}] += p.second;
            }
        }
    }
    ll res = 0;
    for(auto &p:dp[n]){
        if(p.first.first < sum) res += p.second;
        else if(p.first.first == sum and p.first.second < pro) res += p.second;
    }
    auto dfs=[&](auto self,int i,int rs,ll rp,bool flg)->ll{
        if(i == n){
            if(rs == 0 and rp == 1) return 1LL;
            else return 0LL;
        }
        if(flg){
            return dp[n-i][{rs,rp}];
        }
        ll ans = 0;
        for(int j=0;j<10;j++){
            if(!flg and j>(s[i]-'0'))break;
            if(rs < j) break;
            if(rp % (j+1))continue;
            ans += self(self,i+1,rs-j,rp/(j+1),flg|(j<s[i]-'0'));
        }
        return ans;
    };
    res += dfs(dfs,0,sum,pro,0)-1;
    cout << res << endl;
}
accuracy: 1
time_ms: 280
memory_kb: 39608
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAX_S = 140;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;

    int n = S.size();
    vector<vector<map<ll, ll>>> dp(n + 1, vector<map<ll, ll>>(MAX_S));
    dp[0][0][1] = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < MAX_S; j++) {
            for (auto p : dp[i][j]) {
                for (int k = 0; k < 10; k++) {
                    dp[i + 1][j + k][p.first * (k + 1)] += p.second;
                }
            }
        }
    }

    int sum = 0;
    ll ans = 0, prod = 1;
    for (char& c : S) {
        int x = c - '0';
        sum += x;
        prod *= x + 1;
    }
    for (int j = 0; j <= sum; j++) {
        for (auto p : dp[n][j]) {
            if (j < sum)
                ans += p.second;
            else if (p.first < prod)
                ans += p.second;
        }
    }

    for (int i = 0; i < n; i++) {
        int x = S[i] - '0';
        for (int j = 0; j < x; j++) {
            if (prod % (j + 1) == 0) {
                ans += dp[n - 1 - i][sum - j][prod / (j + 1)];
            }
        }
        sum -= x;
        prod /= x + 1;
    }

    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 39696
final_rank: 7

code_snippet: #define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}

template <int i, typename T> void print_tuple(ostream&, const T&) {}
template <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
}
template <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
const long long MOD = 1000000007;
// const long long MOD = 998244353;

const int MAX_S = 140;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;

    int n = S.size();
    vector<vector<map<ll, ll>>> dp(n + 1, vector<map<ll, ll>>(MAX_S));
    dp[0][0][1] = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < MAX_S; j++) {
            for (auto p : dp[i][j]) {
                for (int k = 0; k < 10; k++) {
                    dp[i + 1][j + k][p.first * (k + 1)] += p.second;
                }
            }
        }
    }

    int sum = 0;
    ll ans = 0, prod = 1;
    for (char& c : S) {
        int x = c - '0';
        sum += x;
        prod *= x + 1;
    }
    for (int j = 0; j <= sum; j++) {
        for (auto p : dp[n][j]) {
            if (j < sum)
                ans += p.second;
            else if (p.first < prod)
                ans += p.second;
        }
    }

    for (int i = 0; i < n; i++) {
        int x = S[i] - '0';
        for (int j = 0; j < x; j++) {
            if (prod % (j + 1) == 0) {
                ans += dp[n - 1 - i][sum - j][prod / (j + 1)];
            }
        }
        sum -= x;
        prod /= x + 1;
    }

    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 39748
final_rank: 8

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  constexpr int D = 10;
  int factor[D][4]{};
  REP(i, D) {
    int tmp = i + 1;
    for (; tmp % 2 == 0; tmp /= 2) ++factor[i][0];
    for (; tmp % 3 == 0; tmp /= 3) ++factor[i][1];
    for (; tmp % 5 == 0; tmp /= 5) ++factor[i][2];
    for (; tmp % 7 == 0; tmp /= 7) ++factor[i][3];
  }
  string s; cin >> s;
  const int n = s.length();
  vector memo(n * 3 + 1, vector(n * 2 + 1, vector(n + 1, vector(n + 1, -1LL))));
  memo[0][0][0][0] = 1;
  auto mul = [&](auto &&mul, int a, int b, int c, int d) -> ll {
    ll &m = memo[a][b][c][d];
    if (m != -1) return m;
    if (a > 0) return m = mul(mul, a - 1, b, c, d) * 2;
    if (b > 0) return m = mul(mul, a, b - 1, c, d) * 3;
    if (c > 0) return m = mul(mul, a, b, c - 1, d) * 5;
    if (d > 0) return m = mul(mul, a, b, c, d - 1) * 7;
    assert(false);
  };
  int sum = 0;
  ll prod = 1;
  for (char c : s) {
    sum += c - '0';
    prod *= c - '0' + 1;
  }
  map<tuple<int, int, int, int, int, int>, ll> dp;
  dp[{0, 0, 0, 0, 0, 1}] = 1;
  REP(digit, n) {
    int digit_v = s[digit] - '0';
    map<tuple<int, int, int, int, int, int>, ll> nx;
    for (auto [k, v] : dp) {
      auto [a, b, c, d, sm, lower] = k;
      for (int i = 0; i < D && sm + i <= sum; ++i) {
        if (i < digit_v) {
          nx[{a + factor[i][0], b + factor[i][1], c + factor[i][2], d + factor[i][3], sm + i, lower == 1 ? 0 : lower}] += v;
        } else if (i > digit_v) {
          nx[{a + factor[i][0], b + factor[i][1], c + factor[i][2], d + factor[i][3], sm + i, lower == 1 ? 2 : lower}] += v;
        } else {
          nx[{a + factor[i][0], b + factor[i][1], c + factor[i][2], d + factor[i][3], sm + i, lower}] += v;
        }
      }
    }
    dp.swap(nx);
  }
  ll ans = 0;
  for (auto [k, v] : dp) {
    auto [a, b, c, d, sm, lower] = k;
    if (sm < sum) {
      ans += v;
    } else if (sm == sum) {
      if (mul(mul, a, b, c, d) < prod) {
        ans += v;
      } else if (mul(mul, a, b, c, d) == prod) {
        if (lower == 0) ans += v;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 490
memory_kb: 38052
final_rank: 14

code_snippet: #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

using ll = long long;
using namespace std;

template<typename A, typename B>
bool chmin(A &a, const B b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template<typename A, typename B>
bool chmax(A &a, const B b) {
  if (a >= b) return false;
  a = b;
  return true;
}

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) {
  oss << "\b\b \b";
}

template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template<typename... T>
string _tostr(T &&...args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1) oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1) oss << "}";
  return oss.str();
}
#endif

constexpr int mod = 1'000'000'007; //1e9+7(prime number)
constexpr int INF = 1'000'000'000; //1e9
constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18
constexpr int SIZE = 20;

int N, sum;
ll prod;
char S[SIZE];

ll calc1() {
  ll dp[15][15 * 9 + 1] = {};

  dp[0][0] = 1;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= i * 9; j++) {
      for (int k = 0; k <= 9; k++) {
        dp[i + 1][j + k] += dp[i][j];
      }
    }
  }

  ll res = 0;
  for (int i = 0; i < sum; i++)
    res += dp[N][i];

  return res;
}

int counter[10] = {};
ll fac[20];
int digits[20];

ll dp[15][1 << 14][2]; // {同じ, 未満}

ll c = 0;

ll calc3() {
  memset(dp, 0, sizeof(dp));

  dp[0][0][0] = 1;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (1 << N); j++) {
      for (int k = 0; k < N; k++) {
        c++;

        if (j & (1 << k)) continue;

        if (k && digits[k - 1] == digits[k] && !(j & (1 << (k - 1)))) continue;

        int q = S[i] - '0';
        int next = j | (1 << k);

        if (digits[k] < q) {
          dp[i + 1][next][1] += dp[i][j][0] + dp[i][j][1];
        } else if (digits[k] == q) {
          dp[i + 1][next][0] += dp[i][j][0];
          dp[i + 1][next][1] += dp[i][j][1];
        } else {
          dp[i + 1][next][1] += dp[i][j][1];
        }
      }
    }
  }

  // debug(dp[N][(1 << N) - 1][1]);

  return dp[N][(1 << N) - 1][1];
}

ll calc2(int k = 0, int s = 0, int t = 0, ll p = 1) {
  ll res = 0;

  if (k == N) {
    if (p == prod) return calc3();
    if (p >= prod) return 0;
    res = fac[N];
    for (int i = 0; i < 10; i++) res /= fac[counter[i]];
    // debug(s, res);
    return res;
  }

  for (int i = t; i <= 9; i++) {
    if (s + i * (N - k) <= sum && sum <= (s + i) + (N - k - 1) * 9) {
      digits[k] = i;
      counter[i]++;
      res += calc2(k + 1, s + i, i, p * (i + 1));
      counter[i]--;
    }
  }

  return res;
}


int main() {
  scanf("%s", S);
  N = strlen(S);

  fac[0] = 1;
  for (int i = 1; i <= N; i++) fac[i] = fac[i - 1] * i;

  sum = 0;
  prod = 1;
  for (int i = 0; i < N; i++) {
    sum += S[i] - '0';
    prod *= S[i] - '0' + 1;
  }

  ll ans = 0;

  ans += calc1();
  debug(ans);
  ans += calc2();

  printf("%lld\n", ans);

  debug(c);

  return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 7084
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

string S;
int n;
int cnt[10];
ll Sum, Prod;
ll fact[15], power[11][15];
ll ans;

void dfs(ll d, ll num, ll sum, ll prod, ll comb) {
    if (d == 9) {
        cnt[9] = n - num;
        sum += 9 * cnt[9];
        prod *= power[10][cnt[9]];
        comb /= fact[cnt[9]];
        if (sum != Sum) {
            if (sum < Sum)
                ans += comb;
            return;
        }
        if (prod != Prod) {
            if (prod < Prod)
                ans += comb;
            return;
        }
        int cnt2[10] = {};
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < S[i] - '0'; j++) {
                if (cnt2[j] + 1 > cnt[j])
                    continue;
                ll add = comb * (cnt[j] - cnt2[j]) / (n - i);
                ans += add;
            }
            if (cnt2[S[i] - '0'] + 1 > cnt[S[i] - '0'])
                break;
            comb *= cnt[S[i] - '0'] - cnt2[S[i] - '0'];
            comb /= (n - i);
            cnt2[S[i] - '0']++;
        }
        return;
    }
    for (int i = 0; i <= n - num; i++) {
        cnt[d] = i;
        dfs(d + 1, num + i, sum + d * i, prod * power[d + 1][i],
            comb / fact[i]);
    }
}

int main() {
    fact[0] = 1;
    for (int i = 1; i <= 14; i++)
        fact[i] = i * fact[i - 1];
    for (int i = 1; i <= 10; i++) {
        power[i][0] = 1;
        for (int j = 1; j <= 14; j++)
            power[i][j] = i * power[i][j - 1];
    }
    cin >> S;
    n = S.size();
    Prod = 1;
    for (int i = 0; i < n; i++) {
        Sum += S[i] - '0';
        Prod *= (S[i] - '0') + 1;
    }
    dfs(0, 0, 0, 1, fact[n]);
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3460
final_rank: 1

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	string s;
	cin >> s;
	int num = 0;
	long long int by = 1;
	for (auto i : s) {
		num += i - '0';
		by *= i - '0' + 1;
	}
	vector<vector<unordered_map<long long int, long long int>>>dp(s.size() * 9 + 1, vector<unordered_map<long long int, long long int>>(3));
	dp[0][1][1] = 1;
	for (auto i : s) {
		vector<vector<unordered_map<long long int, long long int>>>ndp(s.size() * 9 + 1, vector<unordered_map<long long int, long long int>>(3));
		for (int j = 0; j <= s.size() * 9; j++) {
			for (int k = 0; k < 3; k++) {
				for (auto l : dp[j][k]) {
					for (int n = 0; n < 10; n++) {
						int nk = k;
						if (nk == 1) {
							if (i - '0' > n)nk = 0;
							if (i - '0' < n)nk = 2;
						}
						ndp[j + n][nk][l.first*(n + 1)] += l.second;
					}
				}
			}
		}
		dp = ndp;
	}
	long long int ans = 0;
	for (int i = 0; i < num; i++) {
		for (auto j : dp[i])for (auto k : j)ans += k.second;
	}
	for (int j = 0; j < 3; j++) {
		for (auto k : dp[num][j]) {
			if (k.first < by || (k.first == by && j == 0)) {
				ans += k.second;
			}
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 260
memory_kb: 35572
final_rank: 6