牛客练习赛13 幸运数字Ⅳ

思路

这种题目需要仔细观察,来判断出真正的数据范围,$n$个数排列的情况数,也就是$n!$,随$n$增大是非常快的。因为$15!>1e9$,所以当长度大于15时,只要计算最后15个数的第$k$小排列就可以了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "stdio.h"
#include "cstring"
#include "stdlib.h"
#include "map"
#include "string"
#include "iostream"
#include "set"
#include "queue"
#include "vector"
#define FOP freopen("input.txt","r",stdin)
#define Met(x,y) memset(x,y,sizeof x)
#define L index*2
#define R index*2+1
using namespace std;
typedef long long ll;
const ll MAXN=5e5+100;
const ll MOD=1e9+7;
ll fra[20];
void init()
{
fra[0]=1;
for(int i=1;i<20;++i)
fra[i]=fra[i-1]*i;
// cout<<fra[15]<<endl;
}
ll calu(ll lim)
{
bool ret=0;
ll res=0;
for(int i=1;i<=12 && !ret;++i)
for(int j=0;j<(1<<i) && !ret;++j)
{
ll digit=1;
ll tem=j;
ll cs=0;
for(int k=0;k<i;++k)
{
if(tem%2)
cs+=digit*7;
else cs+=digit*4;
tem/=2;
digit*=10;
}
if(cs<=lim) res++;
if(cs>=lim) ret=0;
}
return res;
}
ll permute[20];
bool judge(ll num)
{
while (num) {
if(num%10!=4 && num%10!=7)
return 0;
num/=10;
}
return 1;
}
int main(int argc, char const *argv[]) {
// FOP;
init();
ll n,kth;
cin>>n>>kth;
ll ans=calu(n- 15);
// cout<<ans<<endl;
ll digit=min(15LL,n);
bool used[20];
Met(used,0);
kth--;
for(int i=0;i<digit;++i)
{
ll tem=kth/fra[digit-i-1];
// printf("%lld %lld\n", kth,fra[digit-i-1]);
kth%=fra[digit-i-1];
// printf("%lld %lld\n", kth,fra[digit-i-1]);
if(tem>=digit-i)
{
puts("-1");
return 0;
}
for(int j=0;j<20;++j)
{
if(used[j])
continue;
if(!tem)
{
permute[i]=j;
used[j]=1;
break;
}
tem--;
}
// for(int j=0;j<20;++j)
// printf("%d ",used[j]);
// puts("");
// printf("%lld\n",permute[i]+1);
}
ll cnt=0;
for(int i=max(1LL,n-14),cnt=0;i<=n;++i,cnt++)
{
// cout<<max(1LL,n-14)+permute[i-max(1LL,n-14)]<<endl;
// cout<<i<<endl;
ans+=judge(max(1LL,n-14)+permute[i-max(1LL,n-14)])&judge(i);
}
cout<<ans<<endl;
return 0;
}