牛客练习赛13D 幸运数字4

题目链接:https://ac.nowcoder.com/acm/contest/70/D

题目大意:

  略

分析:

  注意到12! < 10^9 < 13!,于是当n > 13时,第k号排列的前n - 13位是确定的。比如n = 15吧,那么无论k取何值,第k号排列都是形如:“12xxxxxxxxxxxxx”,后面的x代表其他数字,因为后面13个x,有13!种排列,大于10^9。于是平移一下就退化成n = 13的情况了,同时不要忘了把前n - 13位种满足条件的幸运数算一下。

  对于n <= 13的情况,如果直接从第一个排列生成到第k个排列势必会超时,这里就要用到康托展开求出排列,然后遍历一遍即可。

  关于康托展开是什么,看大佬链接:http://www.cnblogs.com/linyujun/p/5205760.html

代码如下:

  1 #pragma GCC optimize("Ofast")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4  
  5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 13  
 14 #define pr(x) cout << #x << " = " << x << "  "
 15 #define prln(x) cout << #x << " = " << x << endl
 16  
 17 #define LOWBIT(x) ((x)&(-x))
 18  
 19 #define ALL(x) x.begin(),x.end()
 20 #define INS(x) inserter(x,x.begin())
 21  
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,inf,sizeof(a))
 24 #define msM(a) memset(a,-1,sizeof(a))
 25  
 26 #define pii pair<int,int>
 27 #define piii pair<pair<int,int>,int>
 28 #define MP make_pair
 29 #define PB push_back
 30 #define ft first
 31 #define sd second
 32  
 33 template<typename T1, typename T2>
 34 istream &operator>>(istream &in, pair<T1, T2> &p) {
 35     in >> p.first >> p.second;
 36     return in;
 37 }
 38  
 39 template<typename T>
 40 istream &operator>>(istream &in, vector<T> &v) {
 41     for (auto &x: v)
 42         in >> x;
 43     return in;
 44 }
 45  
 46 template<typename T1, typename T2>
 47 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 48     out << "[" << p.first << ", " << p.second << "]" << "\n";
 49     return out;
 50 }
 51  
 52 typedef long long LL;
 53 typedef unsigned long long uLL;
 54 typedef pair< double, double > PDD;
 55 typedef set< int > SI;
 56 typedef vector< int > VI;
 57 const double EPS = 1e-10;
 58 const int inf = 1e9 + 9;
 59 const LL mod = 1e9 + 7;
 60 const int maxN = 1e5 + 7;
 61 const LL ONE = 1;
 62  
 63 // 12! < 1e9 < 13!
 64 LL n, k, ans;
 65 LL b; // 偏移
 66  
 67 LL lucky[1500], len;
 68 LL fac[21] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000};
 69  
 70 /**
 71  *    @brief 康托展开,把数字转换成排列 
 72  *    @param A 所要求的从1~k的一个排列 
 73  *    @param num 排列A对应的数字,范围从0~k!-1
 74  *    @param k 所要求的的排列长度 
 75  *
 76  *    @return 无 
 77  */
 78 void cantor(int A[], LL num, int k) {
 79     int t;
 80     bool vis[k];//0到k-1,表示是否出现过
 81     ms0(vis);
 82     Rep(i, k){
 83         t = num / fac[k - i - 1];
 84         num = num % fac[k - i - 1];
 85         // 找出尚未被标记过的第t大的数 
 86         for(int j = 0, pos = 0; ; ++j, ++pos){
 87             if(vis[pos]) --j;
 88             if(j == t){
 89                 vis[pos] = true;
 90                 A[i] = pos + 1; // 排列是从1开始的,所以要记得加1 
 91                 break;
 92             }
 93         }
 94     }
 95 }
 96 
 97 /**
 98  *    @brief 康托逆展开,把排列转换成数字 
 99  *    @param A 从1~k的一个排列 
100  *    @param num 排列A对应的数字,范围从0~k!-1
101  *    @param k 排列长度 
102  *
103  *    @return 无 
104  */
105 void inv_cantor(int A[], LL &num, int k) {
106     int cnt;
107     num = 0;
108     Rep(i, k) {
109         cnt = 0;
110         // 判断A[i]后面有几个数小于它 
111         For(j, i + 1, k - 1) if(A[i] > A[j]) cnt ++;//判断几个数小于它
112         num += fac[k - i - 1] * cnt;
113     }
114 }
115  
116 // 求所有幸运数
117 inline void dfs(LL x, int cnt) {
118     if(cnt > 9) return;
119     lucky[len++] = x;
120     dfs(x*10 + 4, cnt + 1);
121     dfs(x*10 + 7, cnt + 1);
122 }
123  
124 bool check(LL x) {
125     int tmp = find(lucky, lucky + len, x) - lucky;
126     return tmp < len;
127 }
128  
129 int main(){
130     INIT();
131     cin >> n >> k;
132     dfs(0, 0);
133     sort(lucky, lucky + len);
134      
135     if(n > 13) {
136         b = n - 13;
137         n = 13;
138         ans = upper_bound(lucky, lucky + len, b) - lucky;
139         --ans;
140     }
141     if(k > fac[n]) {
142         cout << -1 << endl;
143         return 0;
144     }
145      
146     //利用康托展开计算排列序列
147     int s[14];
148     cantor(s, k - 1, n);
149     
150     Rep(i, n) if(check(i + b + 1) && check(s[i] + b)) ++ans;
151 
152     cout << ans << endl;
153     return 0;
154 }
View Code

猜你喜欢

转载自www.cnblogs.com/zaq19970105/p/10741918.html