题: https://nanti.jisuanke.com/t/41414
#include<bits/stdc++.h> using namespace std; typedef __int128 ll; const int M=30; const int mod=1e9+7; ll dp[M][M]; int a[M]; int n; inline ll read() { ll x = 0, f = 1; char c = getchar(); for (; !isdigit(c);c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } ll dfs1(int pos,int up){ if(pos==n+1) return 1; if(~dp[pos][up]) return dp[pos][up]; ll sum=0; for(int i=0;i<=up;i++){ sum+=dfs1(pos+1,max(up,i+1)); } dp[pos][up]=sum; return sum; } void dfs2(int pos,int up,ll r){ if(pos==n+1){ for(int i=1;i<=n;i++) putchar(a[i]+'A'); puts(""); return ; } for(int i=0;i<=up;i++){ int nowup=max(i+1,up); if(r<=dp[pos+1][nowup]){ a[pos]=i; dfs2(pos+1,nowup,r); break; } else r-=dp[pos+1][nowup]; } } int main(){ int t; scanf("%d",&t); for(int ca=1;ca<=t;ca++){ scanf("%d",&n); ll k=read(); for(int i=0;i<M;i++) for(int j=0;j<M;j++) dp[i][j]=-1; for(int i=0;i<M;i++) dp[n+1][i]=1; // cout<<"!!"<<endl; dfs1(1,0); printf("Case #%d: ",ca); dfs2(1,0,k); } return 0; }