首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cf121C. Lucky Permutation(康托展开)

cf121C. Lucky Permutation(康托展开)

作者头像
attack
发布2019-01-30 17:22:26
3150
发布2019-01-30 17:22:26
举报

题意

题目链接

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;
}
/*

*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档