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 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...
随机推荐
- 一:使用maven构建项目
一般情况下:使用maven构建项目有两种情况: 1:用maven构建java项目: 2:用maven构建javaweb项目: 还有一种经常需要使用到的就是用maven构建项目模块:如:一个父项目用来作 ...
- WWDC: Thread Sanitizer and Static Analysis
Thread Sanitizer 过程 编译过程中链接了一个新的库.  也可以通过命令行来操作: $ clang -fsanitize=thread source.c -o executable $ ...
- 错误处理Cannot load JDBC driver class 'oracle.jdbc.drive
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnec是由于<Resource name=&qu ...
- 归并排序的理解和实现(Java)
归并排序介绍 归并排序(Merge Sort)就是利用归并的思想实现的排序方法.它的原理是假设初始序列含有fn个记录,则可以看成是n个有序的子序列,每个子序列的长度为1,然后两两归并,得到[n2\fr ...
- windows7下部署tomcat
需要准备的软件: 1.JDK 安装程序 2.Tomcat 安装程序 JDK安装步骤: 安装方式:使用安装程序进行安装.使用JDK文件夹,免安装,以下步骤使用的是文件夹免安装,只需配置环境变量 配置环境 ...
- TapTap推广统计逻辑
当我们在Taptap上访问某款游戏时,比如https://www.taptap.com/app/34762,taptap会记录下这次访问,它是怎么做的呢. 首先,用记事本打开这个网址,在head部分看 ...
- oauth2.0实现sso单点登录的方式和相关代码
SSO介绍 什么是SSO 百科:SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他 ...
- OkHttp3实现Cookies管理及持久化
转发自:http://www.codeceo.com/article/okhttp3-cookies-manage.html OKHttp3正式版刚发布了没几天,正好重构之前的代码,于是第一时间入坑了 ...
- 关于符号Symbol第一篇
Symbol类的一个实例代表一个符号.对于语法树来说,并不是每个节点都有一个符号实例.下面列举了哪些语法树节点具有符号的引用,如下表格: 其中JCNewClass.JCAssignOp.JCUnary ...
- 删除none 的images报错 image has dependent child images 解决办法
这个错是因为在要删除的images之后创建了该images的父images 方法: docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Pa ...