L1-050 倒数第N个字符串
题目链接-L1-050 倒数第N个字符串
解题思路
进制转换问题
- 把 l 位小写字母看成 l 位26进制数
- 那么倒数第n个字符串用十进制表示就是pow(26,l)-n
- 再将结果转换成26进制还原字母即可
- 因为每个字符串的长度固定为 L,所以位数不够时前面要用 a 补齐(跟十进制数在前面补0是一个道理)
- 具体操作见代码
附上代码
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int l,n,num;
cin>>l>>n;
num=pow(26,l)-n;
vector<char> v;
while(num){
v.push_back(num%26+'a');
num/=26;
}
for(int i=0;i<l-v.size();i++)
cout<<"a";
for(int i=v.size()-1;i>=0;i--)
cout<<v[i];
return 0;
}