cf121C. Lucky Permutation(康托展开)
题意
Sol
由于阶乘的数量增长非常迅速,而\(k\)又非常小,那么显然最后的序列只有最后几位会发生改变。
前面的位置都是\(i = a[i]\)。那么前面的可以直接数位dp/爆搜,后面的部分是经典问题,可以用逆康托展开计算。
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 1, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
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;}
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);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, K, fac[MAXN];
vector<int> res;
int find(int x) {
sort(res.begin(), res.end());
int t = res[x];
res.erase(res.begin() + x);
return t;
}
bool check(int x) {
while(x) {
if((x % 10) != 4 && (x % 10) != 7) return 0;
x /= 10;
}
return 1;
}
int ans;
void dfs(int x, int Lim) {//计算1 - lim中只包含4 7的数量
if(x > Lim) return ;
if(x != 0) ans++;
dfs(x * 10 + 4, Lim);
dfs(x * 10 + 7, Lim);
}
signed main() {
N = read(); K = read() - 1;
int T = -1; fac[0] = 1;
for(int i = 1; i <= N;i++) {
fac[i] = i * fac[i - 1];
res.push_back(N - i + 1);
if(fac[i] > K) {T = i; break;}
}
if(T == -1) {puts("-1"); return 0;}
dfs(0, N - T);
for(int i = T; i >= 1; i--) {
int t = find(K / fac[i - 1]), pos = N - i + 1;
if(check(pos) && check(t)) ans++;
K = K % fac[i - 1];
}
cout << ans;
return 0;
}
/*
*/
cf121C. Lucky Permutation(康托展开)的更多相关文章
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-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 ...
- LightOJ1060 nth Permutation(不重复全排列+逆康托展开)
一年多前遇到差不多的题目http://acm.fafu.edu.cn/problem.php?id=1427. 一开始我还用搜索..后来那时意外找到一个不重复全排列的计算公式:M!/(N1!*N2!* ...
- UESTC 485 Game(康托展开,bfs打表)
Game Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status t ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 用康托展开实现全排列(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)!+...+ ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- OJ 1188 全排列---康托展开
题目描述 求n的从小到大第m个全排列(n≤20). 输入 n和m 输出 输出第m个全排列,两个数之间有一空格. 样例输入 3 2 样例输出 1 3 2 #include<cstdio> # ...
- loj 1165(bfs+康托展开)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...
随机推荐
- Dockerfile数据管理
本文介绍Docker内部以及容器间的数据管理,在容器中管理数据主要有两种方式: 数据卷(Volumes) 挂载主机目录(Bind mounts) 数据卷 数据卷是一个可供一个或则多个目录使用的特殊目录 ...
- [HTML] H5在webApp中的注意事项
常用的meta标签 <!--防止手机中网页放大和缩小--> <meta name="viewport" content="width=device-wi ...
- 深度学习笔记(六)VGG14
Very Deep Convolutional Networks for Large-Scale Image Recognition 1. 主要贡献 本文探究了参数总数基本不变的情况下,CNN随着层数 ...
- c++之sleep函数
c++之sleep函数 c++中使用sleep函数需要导入第三方库,标准库中没有该函数实现. 我们导入window.h使用Sleep()方法,注意:第一个S要大写,括号中的表示的整数倍的毫秒 Slee ...
- django.db.utils.InternalError: (1050, "Table 'tb_content' already exists")
在goods应用里面写了tb_content数据表的模型类(不该写在这里的),进行了数据迁移,还导入了数据. 在contents应用里也写了tb_content数据表的模型类(应该写在这里的), 解决 ...
- EF 一对一、一对多、多对多配置语句小记
数据库实体间的关系无非有这么几种:一对一.一对多.多对多,这些关系在EF框架中分别有不同的创建方式: 1.在"Database First"模式中,这些关系通过SQL语句的方式建立 ...
- Apache解析和绑定域名
转载+修改 如果你想让你上线项目的域名解析的是你本地的IP,该怎么做呢?难道要一个个的改配置文件吗? 例 :域名为 aaa.com 端口默认为80. 我试图修改为8080端口,但是出错了 1.本地h ...
- SQL Function 自定义函数
目录 产生背景(已经有了存储过程,为什么还要使用自定义函数) 发展历史 构成 使用方法 适用范围 注意事项 疑问 内容 产生背景(已经有了存储过程,为什么还要使用自定义函数) 与存储过程的区别(存 ...
- markdown编辑器(颜色、大小、字体)
<font face="黑体">我是黑体字</font> <font face="微软雅黑">我是微软雅黑</font ...
- GBDT多分类示例
相当于每次都是用2分类,然后不停的训练,最后把所有的弱分类器来进行汇总 样本编号 花萼长度(cm) 花萼宽度(cm) 花瓣长度(cm) 花瓣宽度 花的种类 1 5.1 3.5 1.4 0.2 山鸢尾 ...