题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1060

题解:如果是不重复数的这些操作可以用康托展开的逆来求,如果是有重复数字出现康托展开的逆就要稍微变一下。要除去自身个数的组合数具体看一代码,暴力就行

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll f[30];
char s[30];
int num[30];
void init() {
f[1] = f[0] = 1;
for(int i = 2 ; i <= 20 ; i++) f[i] = f[i - 1] * i;
}
int main() {
int t , Case = 0;
scanf("%d" , &t);
init();
while(t--) {
int n;
scanf("%s %d" , s , &n);
memset(num , 0 , sizeof(num));
int len = strlen(s);
for(int i = 0 ; i < len ; i++) {
num[s[i] - 'a']++;
}
ll Max = f[len];
for(int i = 0 ; i < 26 ; i++) if(num[i]) Max /= f[num[i]];//最多的组合种类
printf("Case %d: " , ++Case);
if(Max < n) { printf("Impossible\n"); continue; }
for(int i = 0 ; i < len ; i++) {
for(int j = 0 ; j < 26 ; j++) {
if(!num[j]) continue;
ll gg = f[len - i - 1];
num[j]--;
for(int l = 0 ; l < 26 ; l++) if(num[l]) gg /= f[num[l]];
if(gg >= n) {
putchar(j + 'a');
break;
}//如果取当第j个字母为当前位的组合数大于n那么肯定符合。有种贪心的思想。
n -= gg;//否则就是下一个字母这里之所以要减去是因为要求递增下去前面的组合都不行到下一组合肯定要减去上一个组合数。
num[j]++;
}
if(i == len - 1) printf("\n");
}
}
return 0;
}

lightoj 1060 - nth Permutation(组合数+贪心)的更多相关文章

  1. Light OJ 1060 - nth Permutation(组合数)

    题目大意: 给你一个字符串,问这个字符串按照特定顺序排列之后,第n个字符串是哪个? 题目分析: 首先我们要会求解总个数.也就是共有len个字符,每个字符有ki个,那么总组合方式是多少种? 总组合方式就 ...

  2. nth Permutation LightOJ - 1060

    nth Permutation LightOJ - 1060 题意:给定一个小写字母组成的字符串,对其中所有字母进行排列(排列组合的排列),将所有生成的排列按字典序排序,求排序后第n个排列. 方法:按 ...

  3. lightoj.1048.Conquering Keokradong(二分 + 贪心)

    Conquering Keokradong Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  4. Codeforces 500B New Year Permutation( Floyd + 贪心 )

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. LightOJ 1096 - nth Term 矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1096 题意:\(f(n)  = a * f(n-1) + b * f(n-3) + c, ...

  6. LightOJ - 1318 - Strange Game(组合数)

    链接: https://vjudge.net/problem/LightOJ-1318 题意: In a country named "Ajob Desh", people pla ...

  7. LightOJ - 1067 - Combinations(组合数)

    链接: https://vjudge.net/problem/LightOJ-1067 题意: Given n different objects, you want to take k of the ...

  8. 5.5 省选模拟赛 B Permutation 构造 贪心

    LINK:Permutation 对于这种构造神题 我自然是要补的.为啥就我没想出来哇. 30分还是很好写的 注意8!实际上很小 不需要爆搜 写bfs记录状态即可.至于判断状态是否出现与否 可以开ma ...

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

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

随机推荐

  1. 利用dockerfile 安装一个nginx-1.14.1

    FROM docker.io/centos MAINTAINER jim 107420988@qq.com ENV TZ "Asia/Shanghai" #ENV TERM xte ...

  2. codeforces 340 A. The Wall

    水水的一道题,只需要找xy的最小公倍数,然后找a b区间有多少个可以被xy的最小公倍数整除的数,就是答案. //============================================ ...

  3. Intent 常用方法总结

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本文主要是总结Intent 常用的方法,并封装成Utils类中 主要涉及以下内容 ...

  4. 最基础的 ant build 脚本

    最基础的 ant build 脚本,根据项目,自行进行修改 <?xml version="1.0" encoding="UTF-8" ?> < ...

  5. 【Java例题】4.4使用牛顿迭代法求方程的解

    4. 使用牛顿迭代法求方程的解:x^3-2x-5=0区间为[2,3]这里的"^"表示乘方. package chapter4; public class demo4 { publi ...

  6. BFS DFS模板

    转载于https://blog.csdn.net/alalalalalqp/article/details/9155419 BFS模板: #include<cstdio> #include ...

  7. ASP.NET Core MVC 之视图组件(View Component)

    1.视图组件介绍 视图组件是 ASP.NET Core MVC 的新特性,类似于局部视图,但它更强大.视图组件不使用模型绑定,并且仅依赖于调用它时所提供的数据. 视图组件特点: 呈块状,而不是整个响应 ...

  8. 利用DoHome APP和音箱控制LED灯实验参考步骤

    准备材料: Arduino Uno 一块 Arduino 扩展板        购买链接 DT-06模块一个       购买链接 安卓手机一个 小度音箱一个 小灯珠一个 杜邦线若干 1.DT-06固 ...

  9. API开发之接口安全(二)-----sign校验

    上一章 我们说了 sign的生成 那么 我们如何确定这个sign的准确性呢 下来 我们说说 校验sign的那些事 在拿到header里面的内容之后 我们首先需要对其内容的基本参数做一个校验 我们补充下 ...

  10. Mac 查找粘贴板记录

    0x00 大落 一件蛮坑爹的事情,复制了找了好久的内容合集,在回别人的信息的时候又进行了复制其他内容的操作,结果吾覆盖了的上一次复制的内容-- 于是开始找找 macOS 有没有粘贴板记录的东西,然后在 ...