帮y总发一下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, m, ans;
bool b[20];
int fact(int k)
{
LL res = 1;
for (int i = 1; i <= k; i ++ )
{
res *= i;
if(res > m) return m + 1;
}
return res;
}
bool check(int x)
{
string s = to_string(x);
for(auto c : s)
if(c != '4' && c != '7') return 0;
return 1;
}
int get(int x)
{
int k = 1, r = m;
while(fact(k) < m) k++;
if(x <= n - k) return x;
memset(b, 0, sizeof b);
for(int i = n - k + 1; i <= n; i++)
{
for (int j = 1; j <= k; j ++ )
if(!b[j])
{
int t = fact(n - i);
if(t < r) r -= t;
else
{
if(x == i) return j + (n - k);
b[j] = 1;
break;
}
}
}
return -1;
}
void dfs(LL x)
{
if(x > n) return;
if(x && check(get(x))) ans++;
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
int main()
{
cin >> n >> m;
if(fact(n) < m) printf("-1");
else
{
dfs(0);
printf("%d", ans);
}
return 0;
}