题意

题目链接

Sol

由于阶乘的数量增长非常迅速,而\(k\)又非常小,那么显然最后的序列只有最后几位会发生改变。

前面的位置都是\(i = a[i]\)。那么前面的可以直接数位dp/爆搜,后面的部分是经典问题,可以用逆康托展开计算。

  1. #include<bits/stdc++.h>
  2. #define Pair pair<int, int>
  3. #define MP(x, y) make_pair(x, y)
  4. #define fi first
  5. #define se second
  6. #define int long long
  7. #define LL long long
  8. #define Fin(x) {freopen(#x".in","r",stdin);}
  9. #define Fout(x) {freopen(#x".out","w",stdout);}
  10. using namespace std;
  11. const int MAXN = 1e6 + 1, mod = 1e9 + 7, INF = 1e9 + 10;
  12. const double eps = 1e-9;
  13. template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
  14. template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
  15. template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
  16. template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
  17. template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
  18. template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
  19. template <typename A> inline void debug(A a){cout << a << '\n';}
  20. template <typename A> inline LL sqr(A x){return 1ll * x * x;}
  21. inline int read() {
  22. char c = getchar(); int x = 0, f = 1;
  23. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  24. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  25. return x * f;
  26. }
  27. int N, K, fac[MAXN];
  28. vector<int> res;
  29. int find(int x) {
  30. sort(res.begin(), res.end());
  31. int t = res[x];
  32. res.erase(res.begin() + x);
  33. return t;
  34. }
  35. bool check(int x) {
  36. while(x) {
  37. if((x % 10) != 4 && (x % 10) != 7) return 0;
  38. x /= 10;
  39. }
  40. return 1;
  41. }
  42. int ans;
  43. void dfs(int x, int Lim) {//计算1 - lim中只包含4 7的数量
  44. if(x > Lim) return ;
  45. if(x != 0) ans++;
  46. dfs(x * 10 + 4, Lim);
  47. dfs(x * 10 + 7, Lim);
  48. }
  49. signed main() {
  50. N = read(); K = read() - 1;
  51. int T = -1; fac[0] = 1;
  52. for(int i = 1; i <= N;i++) {
  53. fac[i] = i * fac[i - 1];
  54. res.push_back(N - i + 1);
  55. if(fac[i] > K) {T = i; break;}
  56. }
  57. if(T == -1) {puts("-1"); return 0;}
  58. dfs(0, N - T);
  59. for(int i = T; i >= 1; i--) {
  60. int t = find(K / fac[i - 1]), pos = N - i + 1;
  61. if(check(pos) && check(t)) ans++;
  62. K = K % fac[i - 1];
  63. }
  64. cout << ans;
  65. return 0;
  66. }
  67. /*
  68. */

cf121C. Lucky Permutation(康托展开)的更多相关文章

  1. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  2. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  3. LightOJ1060 nth Permutation(不重复全排列+逆康托展开)

    一年多前遇到差不多的题目http://acm.fafu.edu.cn/problem.php?id=1427. 一开始我还用搜索..后来那时意外找到一个不重复全排列的计算公式:M!/(N1!*N2!* ...

  4. UESTC 485 Game(康托展开,bfs打表)

    Game Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status t ...

  5. hdu.1430.魔板(bfs + 康托展开)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  6. 用康托展开实现全排列(STL、itertools)

    康拓展开: $X=a_n*(n-1)!+a_{n-1}*(n-2)!+\ldots +a_2*1!+a_1*0!$ X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+ ...

  7. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

  8. OJ 1188 全排列---康托展开

    题目描述 求n的从小到大第m个全排列(n≤20). 输入 n和m 输出 输出第m个全排列,两个数之间有一空格. 样例输入 3 2 样例输出 1 3 2 #include<cstdio> # ...

  9. loj 1165(bfs+康托展开)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...

随机推荐

  1. Dockerfile数据管理

    本文介绍Docker内部以及容器间的数据管理,在容器中管理数据主要有两种方式: 数据卷(Volumes) 挂载主机目录(Bind mounts) 数据卷 数据卷是一个可供一个或则多个目录使用的特殊目录 ...

  2. [HTML] H5在webApp中的注意事项

    常用的meta标签 <!--防止手机中网页放大和缩小--> <meta name="viewport" content="width=device-wi ...

  3. 深度学习笔记(六)VGG14

    Very Deep Convolutional Networks for Large-Scale Image Recognition 1. 主要贡献 本文探究了参数总数基本不变的情况下,CNN随着层数 ...

  4. c++之sleep函数

    c++之sleep函数 c++中使用sleep函数需要导入第三方库,标准库中没有该函数实现. 我们导入window.h使用Sleep()方法,注意:第一个S要大写,括号中的表示的整数倍的毫秒 Slee ...

  5. django.db.utils.InternalError: (1050, "Table 'tb_content' already exists")

    在goods应用里面写了tb_content数据表的模型类(不该写在这里的),进行了数据迁移,还导入了数据. 在contents应用里也写了tb_content数据表的模型类(应该写在这里的), 解决 ...

  6. EF 一对一、一对多、多对多配置语句小记

    数据库实体间的关系无非有这么几种:一对一.一对多.多对多,这些关系在EF框架中分别有不同的创建方式: 1.在"Database First"模式中,这些关系通过SQL语句的方式建立 ...

  7. Apache解析和绑定域名

    转载+修改 如果你想让你上线项目的域名解析的是你本地的IP,该怎么做呢?难道要一个个的改配置文件吗? 例 :域名为  aaa.com 端口默认为80. 我试图修改为8080端口,但是出错了 1.本地h ...

  8. SQL Function 自定义函数

    目录 产生背景(已经有了存储过程,为什么还要使用自定义函数) 发展历史 构成 使用方法 适用范围 注意事项 疑问   内容 产生背景(已经有了存储过程,为什么还要使用自定义函数) 与存储过程的区别(存 ...

  9. markdown编辑器(颜色、大小、字体)

    <font face="黑体">我是黑体字</font> <font face="微软雅黑">我是微软雅黑</font ...

  10. GBDT多分类示例

    相当于每次都是用2分类,然后不停的训练,最后把所有的弱分类器来进行汇总 样本编号 花萼长度(cm) 花萼宽度(cm) 花瓣长度(cm) 花瓣宽度 花的种类 1 5.1 3.5 1.4 0.2 山鸢尾 ...