可以先看些资料:http://blog.csdn.net/keyboarderqq/article/details/53388936

参考谷巨巨:http://blog.csdn.net/azx736420641/article/details/50982142

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>PII;
const double eps=1e-5;
const double pi=acos(-1.0);
//const int mod=1e9+7;
const int INF=0x3f3f3f3f; char s[25];
LL f[25];
int a[30]; void init()
{
f[0]=1;
for(int i=1;i<=22;i++)
f[i]=f[i-1]*i;
} int main()
{
init();
int n,T,len,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%s%d",s,&n);
len=strlen(s);
memset(a,0,sizeof(a));
for(int i=0;i<len;i++)
a[s[i]-'a']++;
//计算总的方案数,采用除序法;
LL temp=f[len];
for(int i=0;i<26;i++)
temp/=f[a[i]];
printf("Case %d: ",cas++);
if(temp<n)
{
puts("Impossible");
continue;
} for(int i=0;i<len;i++)
{
for(int j=0; j<26; j++)
{
if(!a[j])
continue;
a[j]--;
LL tmp=f[len-i-1]; //对于第i个位置为j,计算总排列数
for(int k=0; k<26; k++) //除序法计算方案数
tmp/=f[a[k]];
if(n<=tmp) //如果方案数已超,一定是字母 j ;
{
printf("%c",'a'+j);
break;
}
n-=tmp; //减去,肯定是别的字母
a[j]++;
}
}
puts("");
}
return 0;
}

最新文章

  1. Spring Framework------&gt;version4.3.5.RELAESE-----&gt;Reference Documentation学习心得-----&gt;使用spring framework的IoC容器功能-----&gt;方法一:使用XML文件定义beans之间的依赖注入关系
  2. Handler的总结
  3. Java学习第五天
  4. JavaScript中,{}+{}等于多少?
  5. android的intent打开系统程序
  6. Jedis超时时间设置梳理
  7. Windows上Python2与Python3共存
  8. capwap学习笔记——初识capwap(三)(下)
  9. tensorboard 可视化网络运行过程
  10. .net core 2.0 webuploader上传图片
  11. vue监听滚动事件 实现某元素吸顶或者固定位置显示
  12. zookeeper各种报错、原因及解决方法汇总(持续更新)
  13. 编程,计算data段中的第一组数据的3次方,结果保存在后面一组dword单元中
  14. ps和fireworks切图网页优化,jpg为80时
  15. 【基于初学者的SSH】struts2 的拦截器、令牌的简单应用及理解
  16. XP系统下建立WIFI热点让手机、电脑能上网
  17. 三种方案在Windows系统下安装ubuntu双系统
  18. Html5实现iPhone开机界面
  19. 开发者应该了解的API技术清单
  20. Stars(树状数组单点更新)

热门文章

  1. android handler looper
  2. 网络直播流媒体协议的选择讨论,RTSP,RTMP,HTTP,私有协议?
  3. mybatis入门(十)
  4. SpringBoot-(4)-Filter的使用
  5. (转)windows下一分钟配置ngnix实现HLS m3u8点播
  6. Appnium安装-Mac平台
  7. 关于&lt;context:annotation-config/&gt;配置
  8. spring+mybatis项目整合
  9. Gradients渐变属性
  10. hibernate入门(-)