Rhyme scheme

Problem Describe

A rhyme scheme is the pattern of rhymes at the end of each line of a poem or song. It is usually referred to by using letters to indicate which lines rhyme; lines designated with the same letter all rhyme with each other.

e.g., the following "poem'' of 4 lines has an associated rhyme scheme "ABBA''

1 —— 99 bugs in the code A

2 —— Fix one line B

3 —— Should be fine B

4 —— 100 bugs in the code A

This essentially means that line 1 and 4 rhyme together and line 2 and 3 rhyme together.

The number of different possible rhyme schemes for an nnn-line poem is given by the Bell numbers. For example, \(B_3 = 5\), it means there are five rhyme schemes for a three-line poem: AAA, AAB, ABA, ABB, and ABC.

The question is to output the \(k\)-th rhyme scheme in alphabetical order for a poem of nnn lines.For example: the first rhyme scheme of a three-line poem is "AAA'', the fourth rhyme scheme of a three-line poem is ABB''.

InputFile

The first line of the input gives the number of test cases, T(\(1 \leq T \leq 10000\)). T test cases follow.

Each test case contains a line with two integers nnn and \(k\).

\(1 \leq n \leq 26, 1 \leq k \leq B_n\) (\(B_n\) is the \(n\)-th of Bell numbers)

OutputFile

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the rhyme scheme contains uppercase letters.

样例输入

7

1 1

2 1

3 1

3 2

3 3

3 4

3 5

样例输出

Case #1: A

Case #2: AA

Case #3: AAA

Case #4: AAB

Case #5: ABA

Case #6: ABB

Case #7: ABC

题意

就是求第k个排列,但是有些排列是重复的,比如AAB和AAC算一种(如果不能理解可以看看题目)

题解

这里其实都是瞎扯

我一开始想的就是求当\(n=26\)时总共有多少种不同的排列。然后想了个dp,然后想用递归,每次确定第一位选什么,然后再做后面的。但是我的两个队友听到我的想法都说不懂,然后看别的题了,然后我就一个人想啊想,一路上遇到了许多问题,还好最后还是解决了。

首先我们需要发现一些性质:

  1. 长度为n的字符串,只会用n种字母,这个性质很明显也很重要。
  2. 每个字符串的种类会因为前面的不同而变化,比如aaa后面接两种字母a、b,而aab后面可以接a、b、c.
  3. 我们只要前面字符串用过的种类数以及后面字符串的长度,后面字符串的种类也就确定了
  4. 有人可能会觉得要是前面字符串用a、b、c和用a、b、d都是三种但是不一样,但是a、b、d是不会出现的(可以结合第一点想想)

以上的性质可不是那么容易就能明白的,所以请结合具体例子仔细想想。

题解正式开始

先说说dp的预处理吧:

  • \(\Large 定义:\) \(dp[n][m] [h]\)用m种字母组成长度为m的字符串,字符串前面接一个用了h种字母的字符串(不在乎前面的字符串的长度)。
  • \(\Large 转移方程:\)\(dp[n][m][h]=dp[n][m-1][h]+dp[n][m][h]*m\)
  • \(\Large初始化:\) \(dp[0][0][h]=h\)

然后定义\(f[n][h]\)为\(sum(dp[n][1..m][h])\),之后开始从第一位开始枚举,字符从小到大的开始枚举。

我这里是用的的递归,void find(n,k,h)​ 即前面的字符串用了h种字符,求接下n个字母组成的字符串的第k个排列。

然后一层层做下去,直到\(n=0\) 结束

ps:会爆int64,于是我用了int128;还有就是我并没有讲的太清楚,只是讲了主要思想和具体实现,至于怎么想到的,那我也不清楚。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);} __int128 f[28][28],dp[28][28][28],k;
ll n;
void init()//预处理部分
{
for(ll i=0;i<=26;i++)dp[0][i][i]=1;
for(ll h=0;h<=26;h++)
for(ll i=1;i<=26;i++)
for(ll j=1;j<=26;j++)
dp[i][j][h]=dp[i-1][j][h]*j+dp[i-1][j-1][h];
for(ll h=0;h<=26;h++)
for(ll i=0;i<=26;i++)
for(ll j=0;j<=26;j++)
f[i][h]+=dp[i][j][h];
}
void Find(ll n,__int128 k,ll h)//递归求解
{
if (n==0)return;
ll tp=(k-1)/f[n-1][h];
if (tp+1<=h)//格外注意h=0的时候
{
printf("%c",'A'+(int)(tp));
Find(n-1,k-tp*f[n-1][h],h);
}
else
{
printf("%c",'A'+(int)(h));
Find(n-1,k-h*f[n-1][h],h+1);
}
} void work()
{
static int cas=0;
printf("Case #%d: ",++cas);
read(n); read(k);
ll p=0;
Find(n,k,p);
if (cas)printf("\n");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
init();
int T;
read(T);
while(T--)work();
}

2019上海网络赛 F. Rhyme scheme 普通dp的更多相关文章

  1. 2019 上海网络赛 F Rhyme scheme (字典树DP)

    题目:https://nanti.jisuanke.com/t/41414 题意:求长度为n的第k个bell number  ,  就是第i位的选取范围在 1-(i-1)位的最大值 +1,第一位固定为 ...

  2. [2019上海网络赛F题]Rhyme scheme

    题目链接 题意,求出合法的长度为n的字典序第k小字符串,合法的定义为除了最后一位,每一位的取值范围为'A'到'A'+pos-1,而最后一位的取值范围'A'到当前字符串最大值+1. 队友tql,Orz ...

  3. ACM-ICPC 2019南昌网络赛F题 Megumi With String

    ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...

  4. 2019上海网络赛B题(差分 + 离散化 or 差分 + 思维)

    这题.....队里都没怎么训练差分,导致败北...写了一堆线段树嘤嘤嘤,到最后也是超时,比赛结束后看到了差分的思想于是就去学了一手. 其实了解差分思想的一眼就能看出来是差分了.但是如果对n差分的话很明 ...

  5. [2019上海网络赛J题]Stone game

    题目链接 CSLnb! 题意是求出给定集合中有多少个合法子集,合法子集的定义为,子集和>=总和-子集和$\& \&$子集和-(子集的子集和)<=总和-子集和. 其实就是很简 ...

  6. 2019 上海网络赛 J stone name (01背包)

    题目:https://nanti.jisuanke.com/t/41420 题意:给你一个集合,然后让你拆成两个集合 x,y    求满足  x>y  &&  x-(x集合中最小 ...

  7. 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)

    题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...

  8. ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval

    ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...

  9. ICPC 2019 徐州网络赛

    ICPC 2019 徐州网络赛 比赛时间:2019.9.7 比赛链接:The Preliminary Contest for ICPC Asia Xuzhou 2019 赛后的经验总结 // 比赛完才 ...

随机推荐

  1. 关于VGA显示实验的问题

    今天做了一个关于VGA的显示实验,但是由于产生的25M时钟不正确所以一直没有图像产生.刚开始的程序如下 虽然仿真的时候出现了时序,但是在下载到硬件的时候不正确. 后来改成 结果就正确了. 另外之前关于 ...

  2. Density of Power Network(ZOJ 3708)

    Problem The vast power system is the most complicated man-made system and the greatest engineering i ...

  3. 使用zabbix-proxy

    事情背景: vt上两个vps,只提供ipv6.(因为便宜嘛).而我的zabbix服务器在腾讯云.它丫的没有ipv6. 那么我没法监控它们了呀... 这咋个行呢? 想办法... 我还有另外的vps 可以 ...

  4. Ryu控制器编程开发——packet_in和packet_out简易交换机实现

    Ryu控制器二次开发,实现一个简单的只能够简单地广播数据包的交换机. from ryu.base import app_manager from ryu.controller import ofp_e ...

  5. 黑马vue---61、为什么vue组件的data要是一个函数

    黑马vue---61.为什么vue组件的data要是一个函数 一.总结 一句话总结: 因为js中以函数为变量作用域,所以这样可以保证每个组件的数据不互相影响 二.why components data ...

  6. 性能优化 | JVM性能调优篇——来自阿里P7的经验总结

    VM 调优概述: 性能定义: 吞吐量 - 指不考虑 GC 引起的停顿时间或内存消耗,垃圾收集器能支撑应用达到的最高性能指标. 延迟 - 其度量标准是缩短由于垃圾啊收集引起的停顿时间或者完全消除因垃圾收 ...

  7. 安装SQL server 提示重新启动计算机失败

    SQL Server2008是一款功能强大.实用性强的mysql数据库管理系统,因此很多用户都会在Win7系统中安装SQL Server2008,但是不少用户在安装过程中遇到问题,安装SQL Serv ...

  8. Storm和Hadoop 区别

    Storm - 大数据Big Data实时处理架构   什么是Storm? Storm是:• 快速且可扩展伸缩• 容错• 确保消息能够被处理• 易于设置和操作• 开源的分布式实时计算系统- 最初由Na ...

  9. DSS分发压力实验

    DSS分发压力实验 昨天为验证依托DSS搭建流媒体直播监控系统的可行性,及确定实时流画面出现严重花屏的原因,做了一个压力实验. 网络拓扑如图: 1.DVR上配置4路视频(CIF / 25fps / 1 ...

  10. 升级chrome浏览器导致网站登录功能不能用

    笔者开发一个java web项目,低版本的chrome(74以下)可以正常登录,升级到chrome74不能正常登录,登录成功后url会携带一个jsessionid=xxxxxx. 登录成功那个页面有s ...