当前位置: 移动技术网 > IT编程>开发语言>C/C++ > cf121C. Lucky Permutation(康托展开)

cf121C. Lucky Permutation(康托展开)

2019年01月07日  | 移动技术网IT编程  | 我要评论

怪物x联盟曼菲斯,george beffry,梅麻吕3d动画全集

题意

题目链接

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

*/

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网