F. Rhyme scheme (dp)

Posted by xoligy on Mon, 07 Oct 2019 12:37:19 +0200

Title Link
Title:
Output length n of the k smallest Rhyme scheme string.
Rhyme scheme: Number of length n is an example of bell number
Rhyme scheme string: The current letter maximum is the previous letter + 1, that is, AABC is consistent, AACB is not consistent, because there is no B before C.
The Title requires a Rhyme scheme string with a dictionary order k of n.
There are 10,000 samples, each of which enters a string of N and k, representing the smallest K string of length n.
Analysis:
First of all, we must find the law.
1 - Strings of length N use only the first n letters, which is obviously important.
2 The largest letter in front is i, followed by i+1. For example, a a a is followed by two letters A and b, while aba/aab is followed by a, B and c.
3 - The root node is A
Train of thought:

dp[i][j] - denotes the number of dictionary orders with the largest number of I bits in front and the largest number of J bits in the back.
dp[i][j] = i * dp[i][j-1] + dp[i+1][j-1];
Because when I is at the top of the list, we can consider 1, 2, 3 in the j th place. I, there are I cases, and it does not affect the maximum value before I. You can also put i+1, which will change at this time.

The last step is to find the largest sequence by dp [][]. Start first. The farthest one larger than k.
Code

#include<bits/stdc++.h>
#include <stdlib.h>
#include<algorithm>
#include<complex>
#include<iostream>
#include<iomanip>
#include<ostream>
#include<cstring>
#include<string.h>
#include<string>
#include<cstdio>
#include<cctype>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include<map>
#include<cstdlib>
#include<time.h>
#include<ctime>
#include<bitset>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/hash_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define pb push_back
#define _filein freopen("C:\\Users\\zsk18\\Desktop\\in.txt","r",stdin)
#define _fileout freopen("C:\\Users\\zsk18\\Desktop\\out.txt","w",stdout)
#define ok(i) printf("ok%d\n",i)
#define gcd(a,b) __gcd(a,b) ;
typedef double db;
//typedef long long ll;
typedef __int128 ll;
typedef unsigned long long ull;
typedef pair<int,int>PII;
const double PI = acos(-1.0);
const ll MOD=1e9+7;
const ll NEG=1e9+6;
const int MAXN=50;
const int INF=0x3f3f3f3f;
const ll ll_INF=1e15;
const double eps=1e-9;
ll qm(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}
ll inv(ll x){return qm(x,MOD-2);} 
ll dp[MAXN][MAXN];
ll ans[MAXN];
int n;
char s[MAXN];
int main(void)
{
      dp[26][1]=26,dp[26][0]=1;
      for(int i=25;i;i--)dp[i][1]=i+1,dp[i][0]=1;
      for(int j=2;j<=26;j++)
      {
            for(int i=25;i;i--)
            {
                  dp[i][j]=(ll)i*dp[i][j-1]+dp[i+1][j-1];
                 if(dp[i][j]>5e19)
                        dp[i][j]=(ll)5e19;
            }
      }
      // for(int i=1;i<=26;i++)
      //       for(int j=0;j<=10;j++)
      //             printf("dp[%d][%d]=%I64d\n",i,j,dp[i][j]);
      int t;
      scanf("%d",&t);
      int number=0;
      while(t--)
      {
            scanf("%d%s",&n,s);
            ll k=0;
            int len=strlen(s);
            for(int i=0;i<len;i++)
                  k=k*10+(ll)(s[i]-'0');
            int ma=1;
            for(int j=1;j<=n;j++)
            {
                  for(int i=1;i<=26;i++)
                  {
                        ma=max(ma,i);
                        if(k<=dp[ma][n-j])
                        {
                              ans[j]=i;
                              break;
                        }
                        k-=dp[ma][n-j];
                  }
            }
            printf("Case #%d: ",++number);
            for(int i=1;i<=n;i++)
                  printf("%c",'A'+(int)ans[i]-1);
            printf("\n");

      }

      return 0;
}